Skip to content

Instantly share code, notes, and snippets.

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

Qingping Meng QingpingMeng

🏠
Working from home
View GitHub Profile
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
  }
@QingpingMeng
QingpingMeng / encryption.md
Last active June 27, 2020 21:43
RSASHA256AESSHA256Encryption
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)
}
@QingpingMeng
QingpingMeng / EncryptSmallData.md
Last active February 26, 2024 06:13
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());