我该怎么做才能使AES256加密结果与Typescript和Java保持相同
我是使用TypeScript和Java加密相同的文本123
,这是打字稿函数:
const handleSubmit1 = () => {
const key = '123'
const iv = '1234567812345678'
const cipher = CryptoJS.AES.encrypt("123", CryptoJS.enc.Utf8.parse(key), {
iv: CryptoJS.enc.Utf8.parse(iv),
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
})
let result= cipher.toString()
// FgMtDtbBHk5MoEfIJLxmIw==
console.log(result)
}
这是执行相同操作的Java函数:
public static String encrypt() {
try {
MessageDigest sha = null;
byte[] bytes = "1234567812345678".getBytes();
ivspec = new IvParameterSpec(bytes);
try {
key = "123".getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-256");
key = sha.digest(key);
String s = new String(key);
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
return Base64.getEncoder().encodeToString(cipher.doFinal("123".getBytes("UTF-8")));
} catch (Exception e) {
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
令我惊讶的是,这两个函数的结果不同,结果不同, Java版本返回wrmbgojratoql5qkcsn/wa ==
和Typescript版本返回fgmtdtbbhk5moeefijlxmiw ==
。为什么使用相同的密钥加密相同的内容,而IV则具有不同的结果?我想念什么吗?我已经从Internet搜索,并告诉我pkcs5padding
和pkcs7padding
没有影响结果。我该怎么办来解决此问题并以相同的动作制作两种语言?我还试图在这样的打字稿中掌握关键:
const handleSubmit1 = () => {
const key = '123'
const iv = '1234567812345678'
const SHA256 = CryptoJS.SHA256(key).toString();
const cipher = CryptoJS.AES.encrypt("123", CryptoJS.enc.Utf8.parse(SHA256), {
iv: CryptoJS.enc.Utf8.parse(iv),
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
})
let result= cipher.toString()
// FgMtDtbBHk5MoEfIJLxmIw==
console.log(result)
}
仍然无法使结果匹配。任何建议将不胜感激。
I am encrypt the same text 123
with AES256 using typescript and java, this is the typescript function:
const handleSubmit1 = () => {
const key = '123'
const iv = '1234567812345678'
const cipher = CryptoJS.AES.encrypt("123", CryptoJS.enc.Utf8.parse(key), {
iv: CryptoJS.enc.Utf8.parse(iv),
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
})
let result= cipher.toString()
// FgMtDtbBHk5MoEfIJLxmIw==
console.log(result)
}
and this is the java function to do the same thing:
public static String encrypt() {
try {
MessageDigest sha = null;
byte[] bytes = "1234567812345678".getBytes();
ivspec = new IvParameterSpec(bytes);
try {
key = "123".getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-256");
key = sha.digest(key);
String s = new String(key);
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
return Base64.getEncoder().encodeToString(cipher.doFinal("123".getBytes("UTF-8")));
} catch (Exception e) {
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
To my surprise, the two function have different results, the java version return wrmBgoJRATOqL5qKCsN/wA==
and the typescript version return FgMtDtbBHk5MoEfIJLxmIw==
. why encrypt the same content with same key and iv have different results? Am I missing something? I have searched from internet and told me that PKCS5Padding
and PKCS7Padding
did not effect the result. what should I do to fix this and make the two language with the same action? I also tried to sha the key in typescript like this:
const handleSubmit1 = () => {
const key = '123'
const iv = '1234567812345678'
const SHA256 = CryptoJS.SHA256(key).toString();
const cipher = CryptoJS.AES.encrypt("123", CryptoJS.enc.Utf8.parse(SHA256), {
iv: CryptoJS.enc.Utf8.parse(iv),
padding: CryptoJS.pad.Pkcs7,
mode: CryptoJS.mode.CBC
})
let result= cipher.toString()
// FgMtDtbBHk5MoEfIJLxmIw==
console.log(result)
}
still could not make the results match. Any sugguestion is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论