我正在尝试编写Javascript以匹配此Java代码的输出:
爪哇:
import java.util.Base64;
public class Enc2 {
public static void main (String[] arg) {
System.out.println(encryptSomeNumber("1234567812345678"));
}
public static String encryptSomeNumber(final String SomeNumber){
String encryptedSomeNum = "";
String ALGO = "AES";
try {
String myKey = "DLDiGPqGysAow3II";
byte[] keyBytes = myKey.getBytes("UTF-8");
java.security.Key encryptkey = new javax.crypto.spec.SecretKeySpec(keyBytes, ALGO);
javax.crypto.Cipher c;
c = javax.crypto.Cipher.getInstance(ALGO);
c.init(javax.crypto.Cipher.ENCRYPT_MODE, encryptkey);
byte[] encVal = c.doFinal(SomeNumber.getBytes());
byte[] encodedBytes = Base64.getEncoder().encode(encVal);
String s = new String(encodedBytes);
encryptedSomeNum = s;
} catch (Exception e) {
System.out.println("error when encrypting number");
return encryptedSomeNum;
}
return encryptedSomeNum;
}
}
输出:Wrs66TuAIxYe + M4fqyyxtkyMFkWGwx9i45 + oQfEA4Xs =
我到目前为止的Javascript(nodeJS v8.7.0):
let crypto = require('crypto');
let algorithm = 'aes-128-ecb';
let password = 'DLDiGPqGysAow3II';
function encrypt(buffer){
let cipher = crypto.createCipher(algorithm, password)
let crypted = Buffer.concat([cipher.update(buffer), cipher.final()]);
return crypted;
}
let cyphertext = encrypt(new Buffer("1234567812345678", "utf8"))
console.log(cyphertext.toString('base64'));
输出:m1jnKjBbKu + m / zsf9DBTMo3NL4E035l0EailFjt / qjo =
谁能看到我在这里缺少的东西? PKCS填充的东西?
答案 0 :(得分:2)
不,填充是一样的。问题是有两种createCipher
方法。一个是使用密码和密钥派生函数而不是密码 - 这是您现在使用的密码。另一个使用密钥和IV。当然,欧洲央行不使用IV,因此您可能必须提供未使用的IV值。