javascript - AES-GCM 与 Angular 中 Apple 的 CryptoKit 的兼容性
问题描述
此代码使用 Apple 的 CryptoKit 框架在 iOS 平台中使用带有 AES-GCM 的私钥和公钥生成消息字符串的加密和解密版本。
import CryptoKit
func generatePrivateKey() -> P256.KeyAgreement.PrivateKey {
let privateKey = P256.KeyAgreement.PrivateKey()
return privateKey
}
func exportPrivateKey(_ privateKey: P256.KeyAgreement.PrivateKey) -> String {
let rawPrivateKey = privateKey.rawRepresentation
let privateKeyBase64 = rawPrivateKey.base64EncodedString()
let percentEncodedPrivateKey = privateKeyBase64.addingPercentEncoding(withAllowedCharacters: .alphanumerics)!
return percentEncodedPrivateKey
}
func importPrivateKey(_ privateKey: String) throws -> Curve25519.KeyAgreement.PrivateKey {
let privateKeyBase64 = privateKey.removingPercentEncoding!
let rawPrivateKey = Data(base64Encoded: privateKeyBase64)!
return try Curve25519.KeyAgreement.PrivateKey(rawRepresentation: rawPrivateKey)
}
func deriveSymmetricKey(privateKey: P256.KeyAgreement.PrivateKey, publicKey: P256.KeyAgreement.PublicKey) throws -> SymmetricKey {
let sharedSecret = try privateKey.sharedSecretFromKeyAgreement(with: publicKey)
let symmetricKey = sharedSecret.hkdfDerivedSymmetricKey(
using: SHA256.self,
salt: "My Key Agreement Salt".data(using: .utf8)!,
sharedInfo: Data(),
outputByteCount: 32
)
return symmetricKey
}
func encrypt(text: String, symmetricKey: SymmetricKey) throws -> String {
let textData = text.data(using: .utf8)!
let encrypted = try AES.GCM.seal(textData, using: symmetricKey)
return encrypted.combined!.base64EncodedString()
}
func decrypt(text: String, symmetricKey: SymmetricKey) -> String {
do {
guard let data = Data(base64Encoded: text) else {
return "Could not decode text: \(text)"
}
let sealedBox = try AES.GCM.SealedBox(combined: data)
let decryptedData = try AES.GCM.open(sealedBox, using: symmetricKey)
guard let text = String(data: decryptedData, encoding: .utf8) else {
return "Could not decode data: \(decryptedData)"
}
return text
}
catch let error {
return "Error decrypting message: \(error.localizedDescription)"
}
}
我怎样才能在 Angular (10) 中做同样的事情?我写了一些代码,但不幸的是我没有从中获得任何成功。这是我在 Angular 中的相同代码。
import { Component } from '@angular/core';
import cryptokit from "cryptokit";
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'MyEnc';
my_public_key = '';
other_public_key = '';
constructor() { }
async apps() {
const P256Password = "some long random string";
const P256Keys = await cryptokit.P256.generateKeys(P256Password);
console.log("P256 private key (encrypted)= " + P256Keys.privateKey);
console.log("P256 public key = " + P256Keys.publicKey);
}
}
我收到了这个错误
错误错误:未捕获(承诺):TypeError:crypto_1.generateKeyPair不是函数
解决方案
推荐阅读
- database - 如何在 Unity 中设置云数据库?
- android - GridLayoutManager 是否已损坏且未在 RecyclerView-v7:28.0.0 中保留状态?- 发生了什么变化?
- ruby - 在 Ruby 中,我可以从实例方法中访问类实例变量吗?
- ruby - ruby 无法安装 gems,只能手动安装
- java - 浏览多个列表以查找相同的对象
- python - 使用 Python 覆盖 C 程序中的资源部分
- java - Log4j2 通过 Java Webstart 登录客户端系统
- apache-spark - pyspark - 错误的工作目录和阻塞的线程错误
- javascript - 如何拆分组件并保持道具正常工作
- maven - 找不到 org.springframework.security:spring-security-b om:jar:5.0.6.RELEASE