如何实现RSA的公钥PK加密一段文字

如何实现RSA的公钥PK加密一段文字

HarmonyOS
2024-06-13 10:34:49
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
LenZhong

可参考如下代码:

// 密钥规格: 
 
  static ASY_KEY_NAME_RSA_3072: string = 'RSA1024'; 
static ALG_NAME_RSA_3072: string = 'RSA|ECB|PKCS1'; 
static priKey: Uint8Array = new Uint8Array();//用于临时保存 
static pubKey: Uint8Array = new Uint8Array();//用于临时保存 
// 生成非对称密钥对 
 
public static async generateRsaKeyPair(): Promise<KeyPair> { 
  let keyPair: KeyPair = new KeyPair(); 
  try { 
  let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator(this.ASY_KEY_NAME_RSA_3072); 
  const tempKeyPair = await asyKeyGenerator.generateKeyPair(); 
  keyPair = { 
    publicKey: base.encodeToStringSync(tempKeyPair.pubKey.getEncoded().data), 
    privateKey: base.encodeToStringSync(tempKeyPair.priKey.getEncoded().data) 
  } 
} catch (err) { 
  console.error(err) 
} 
return keyPair; 
} 
 
 
// 主体方法: 
// 加密 
public static async add(str: string, publicKey: string): Promise<string> { 
  let result = ''; 
  try { 
  let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator(this.ASY_KEY_NAME_RSA_3072); 
  //创建一个 Cipher (解密)对象 
  let cipher = cryptoFramework.createCipher(this.ALG_NAME_RSA_3072); 
  //引入外部的公钥加密 
  const publicKeyDataBlob = { data: base.decodeSync(publicKey) }; 
  let keyGenPromise: cryptoFramework.KeyPair = await asyKeyGenerator.convertKey(publicKeyDataBlob, null); 
  await cipher.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, keyGenPromise.pubKey, null); 
  let put: cryptoFramework.DataBlob = { data: stringToUint8Array(str) }; 
  const finalRes = await cipher.doFinal(put) 
  result = base.encodeToStringSync(finalRes.data); 
} catch (err) { 
  console.log(err.message) 
} 
return result; 
} 
 
// 解密 
 
 
public static async rsaDecrypt(message: string | Uint8Array, privateKey: string): Promise<string> { 
  let result = ''; 
  try { 
  let asyKeyGenerator = cryptoFramework.createAsyKeyGenerator(this.ASY_KEY_NAME_RSA_3072); 
  const privateKeyDataBlob = { data: base.decodeSync(privateKey) }; 
  const keyPair = await asyKeyGenerator.convertKey(null, privateKeyDataBlob); 
  let cipher = cryptoFramework.createCipher(this.ALG_NAME_RSA_3072); //创建一个 Cipher (解密)对象 
  await cipher.init(cryptoFramework.CryptoMode.DECRYPT_MODE, keyPair.priKey, null); 
  let bytes: Uint8Array = null; 
  if (typeof message === 'string') { 
  bytes = base.decodeSync(message); 
} else { 
  bytes = message; 
} 
const finalRes = await cipher.doFinal({ data: bytes }) 
result = uint8ArrayToString(finalRes.data); 
} catch (err) { 
  console.error(err.code) 
} 
return result; 
} 
 
// 公共方法: 
const base = new util.Base64Helper(); 
// 字符串转成字节流 
function  stringToUint8Array(str: string) { 
  return new Uint8Array(buffer.from(str,'utf-8').buffer); 
} 
// 字节流转成可理解的字符串 
function uint8ArrayToString(array:Uint8Array) { 
  // 将UTF-8编码转换成Unicode编码 
  let out: string = ""; 
  let index: number = 0; 
  let len: number = array.length; 
  while (index < len) { 
    let character = array[index++]; 
    switch(character >> 4) { 
      case 0: 
      case 1: 
      case 2: 
      case 3: 
      case 4: 
      case 5: 
      case 6: 
      case 7: 
        out += String.fromCharCode(character); 
        break; 
      case 12: 
      case 13: 
        out += String.fromCharCode(((character & 0x1F) << 6) | (array[index++] & 0x3F)); 
        break; 
      case 14: 
        out += String.fromCharCode(((character & 0x0F) << 12) | ((array[index++] & 0x3F) << 6) | ((array[index++] & 0x3F) << 0)); 
        break; 
      default: 
        break; 
    } 
  } 
  return out; 
}

参考链接

使用RSA非对称密钥(PKCS1_OAEP模式)加解密

使用RSA非对称密钥(PKCS1模式)加解密

分享
微博
QQ
微信
回复
2024-06-13 20:44:20
相关问题
鸿蒙-如何实现播放一段音频
9792浏览 • 2回复 待解决
RSA NoPadding模式加密失败
592浏览 • 1回复 待解决
Scroll初始时自动滚动一段距离
372浏览 • 1回复 待解决
TextField怎么给某文字设置颜色
7789浏览 • 2回复 待解决