function longToByteArray(/*long*/ long: number): Uint8Array {
const byteArray = new Uint8Array(8)
for (let index = 0; index < byteArray.length; index++) {
// tslint:disable-next-line: no-bitwise
const byte = long & 0xff
byteArray[index] = byte
// tslint:disable-next-line: no-bitwise
long = (long - byte) >> 8
}
function arrayBufferToBase64(buffer: ArrayBuffer): string {
let binary = ''
const bytes = new Uint8Array(buffer)
const len = bytes.byteLength
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i])
}
return window.btoa(binary)
}
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.
using var rsaProvider = new RSACng();
// spki is used for browser side encryption
var spki = Convert.ToBase64String(rsaProvider.ExportSubjectPublicKeyInfo());
var encodedPrivateKey = Convert.ToBase64String(rsaProvider.ExportPkcs8PrivateKey());