您的Java代码使用“ Abcdefghijklmnop” .getBytes()作为实际键值,而您的Javascript代码使用“ Abcdefghijklmnop”作为导出实际密钥的密码。
Java AES的默认转换是AES / ECB / PKCS5Padding,而CryptoJS的默认转换是AES / CBC / PKCS7Padding。
解决示例的一种方法是修复Javascript方面:
// this is base64 representation of the Java counterpart// byte[] keyValue = new byte[] { 'A', 'b', 'c', 'd', 'e', 'f', 'g',// 'h', 'i', 'j', 'k','l', 'm', 'n', 'o', 'p'};// String keyForJS = new base64Enprer().enpre(keyValue);var base64Key = "QWJjZGVmZ2hpamtsbW5vcA==";console.log( "base64Key = " + base64Key );// this is the actual key as a sequence of bytesvar key = CryptoJS.enc.base64.parse(base64Key);console.log( "key = " + key );// this is the plain textvar plaintText = "Hello, World!";console.log( "plaintText = " + plaintText );// this is base64-enpred encrypted datavar encryptedData = CryptoJS.AES.encrypt(plaintText, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});console.log( "encryptedData = " + encryptedData );// this is the decrypted data as a sequence of bytesvar decryptedData = CryptoJS.AES.decrypt( encryptedData, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7} );console.log( "decryptedData = " + decryptedData );// this is the decrypted data as a stringvar decryptedText = decryptedData.toString( CryptoJS.enc.Utf8 );console.log( "decryptedText = " + decryptedText );
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)