我有一些使用 SubtleCrypto 的代码来加密密钥并将其存储在数据库中。
对于另一个功能,我必须能够在 Node 12 中对其进行加密。如果没有 SubtleCrypto,我必须在 Crypto 中重新创建该功能。
我得到相同大小的输出,但它似乎无法被 SubtleCrypto 解密,我试图找出哪里出了问题。
这是在浏览器上使用 SubtleCrypto 运行的代码:
key = getKeyMaterial(password)];
salt = base64ToArraybuffer(optionalSalt)
aesKey = crypto.subtle.deriveKey(
{
name: constants.algorithms.pbkdf2,
salt: salt,
iterations: pbkdf2Iterations,
hash: { name: constants.hash.sha256 },
},
key,
{
name: constants.algorithms.aesGcm,
length: aesWrapKeyBitsLength,
},
true,
['wrapKey', 'unwrapKey']
),
return {
salt: arraybufferTobase64(salt),
aesKey: aesKey,
}
function getKeyMaterial(password) {
var enc = new TextEncoder();
crypto.subtle.importKey(
constants.format.raw,
enc.encode(password),
{
name: constants.algorithms.pbkdf2,
},
false,
['deriveKey']
)
Run Code Online (Sandbox Code Playgroud)
如果没有 SubtleCrypto,在 Node 12 中,我被迫使用 Crypto …
想从文件中解密一个字符串。
但是当我在 fs 的字符串上使用 nodejs 解密时,它给出了错误“输入字符串错误”
var fs = require('fs');
var crypto = require('crypto');
function decrypt(text){
var decipher = crypto.createDecipher('aes-256-ctr', 'password')
var dec = decipher.update(text,'hex','utf8')
dec += decipher.final('utf8');
return dec;
}
fs.readFile('./file.json', 'utf8', function (err,data) {
if (err) return console.log(err);
console.log(decrypt(data));
});
Run Code Online (Sandbox Code Playgroud)
试着做一个像这样的字符串它可以工作
var stringInFile= "encryptedString";
console.log(decrypt(stringInFile));
Run Code Online (Sandbox Code Playgroud)
来自 fs 的 console.log(data) 也给出了 'encryptedString'
加密:
private static String secretKey = "boooooooooom!!!!";
private static String salt = "ssshhhhhhhhhhh!!!!";
public static String encrypt(String strToEncrypt, String secret)
{
try
{
byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
IvParameterSpec ivspec = new IvParameterSpec(iv);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);
SecretKey tmp = factory.generateSecret(spec);
SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))); …Run Code Online (Sandbox Code Playgroud) 尝试使用 crypto-js 加密和解密 json 数据。但不工作。如何找到解决方案。如果有人知道,请帮助找到解决方案。
演示:https : //stackblitz.com/edit/angular-ivy-nwhkgz?file=src%2Fapp%2Fapp.component.ts
storageData=
[
{
"customerInfo": {
"Micheal": 1,
"Milson": 2
},
"mycart": {
"Ol1": 1,
"Ol3": 1
},
"cartItemsList": [
{
"pid": "Ol1",
"name": "Avacota",
"qty": 1
},
{
"pid": "Ol3",
"name": "Kaliflower",
"qty": 1
}
],
"cartTotal": 2
}
];
ngOnInit(){
//Encrypt Info
this.encryptInfo = encodeURIComponent(CryptoJS.AES.encrypt(this.storageData, 'secret key 123').toString());
console.log("encryptInfo");
console.log(this.encryptInfo);
//Decrypt Info
var deData= CryptoJS.AES.decrypt(this.encryptInfo, 'secret key 123');
this.decryptedInfo = decodeURIComponent(deData.toString(CryptoJS.enc.Utf8));
console.log("decryptedInfo");
console.log(this.decryptedInfo);
}
Run Code Online (Sandbox Code Playgroud) node-crypto ×4
encryption ×3
javascript ×3
node.js ×3
angular8 ×1
cryptojs ×1
subtlecrypto ×1
typescript ×1