Skip to content

Instantly share code, notes, and snippets.

View ronnysuero's full-sized avatar
🏠
Working from home

Ronny Zapata ronnysuero

🏠
Working from home
View GitHub Profile
@QingpingMeng
QingpingMeng / EncryptSmallData.md
Last active May 15, 2024 20:38
Encrypt data using JavaScript in browser with RSA public key generated in C# without library

This is an example to demo how you generate the RSA key pair from server side(.NetCore 3.1) and pass the public key to the client(Browser) for encrypting the data that <= 245 bytes.

RSA-2048 can only support to encrypt up to 245 bytes data.

Generate RSA key pair in C# (.Net Core 3.1):

using var rsaProvider = new RSACng();
// spki is used for browser side encryption
var spki = Convert.ToBase64String(rsaProvider.ExportSubjectPublicKeyInfo());
var encodedPrivateKey = Convert.ToBase64String(rsaProvider.ExportPkcs8PrivateKey());