支持AES算法Java和JavaScript算法、AES、JavaScript、Java

2023-09-04 03:11:45 作者:同你赴远山

我需要加密使用AES算法在Java应用程序中的一些价值观,而在我的应用我的javascript模块解密一样的。

I need to encrypt some values in Java application using AES algorithm, and decrypt the same in my Javascript module of my application.

我看到一些例子在互联网上,但似乎有在兼容性一定的差异。

I saw some examples over the internet but it seems there's some differences in compatibility.

象下面这样的问题:

AES加密在JavaScript和解密在Java

有人可以请点我一些code例子来解决这个问题。

Can someone please point me some code examples to solve this issue.

感谢。

推荐答案

AES是一种完全指定的算法,因此所有的AES的实现必须是兼容。话虽如此,AES是可变密钥长度块密码,在128位的块操作。要真正使用这一款软件,你必须让一帮其他的选择:如何处理输入由超过1块(这就是所谓的模式),在一些你需要一个初始化向量模式,你需要处理的输入不是由块的确切数量(填充),如何连接code字符转换为字节,以及如何重新present字节的情况下(如源文件)没有按'科技支撑的。所有这些东西必须是兼容的。

AES is an exactly specified algorithm, so all AES implementations must be "compatible". Having said that, AES is a variable-key-length block-cipher, operating on 128-bit blocks. To actually use this in a piece of software, you have to make a bunch of other choices: how to deal with input consisting of more than 1 block (this is called the "mode"), in some modes you need an initialization vector, you need to deal with input not consisting of an exact number of blocks (padding), how to encode characters into bytes, and how to represent bytes in a context (like a source file) that doesn't support that. All those things need to be compatible.

下面是一个试验例子。它使用了标准的Java加密功能(和Apache共享codeC)和JavaScript库的密码加密JS。选择如下:128位密钥,密码块链连接模式(这需要一个初始化向量),PKCS5 / 7填充,UTF-8字符编码,Base64的重新present字节数组

Below is a tested example. It uses the standard Java crypto functions (and Apache Commons Codec), and JavaScript crypto library crypto-js. Choices are as follows: 128-bit key, cipher-block-chaining mode (which needs an initialization vector), PKCS5/7 padding, UTF-8 for character encoding, Base64 to represent byte arrays.

这一块的Java将输出的Base64-CN codeD密文:

This piece of Java will output Base64-encoded ciphertext:

String plainText = "Hello, World! This is a Java/Javascript AES test.";
SecretKey key = new SecretKeySpec(
    Base64.decodeBase64("u/Gu5posvwDsXUnV5Zaq4g=="), "AES");
AlgorithmParameterSpec iv = new IvParameterSpec(
    Base64.decodeBase64("5D9r9ZVzEYYgha93/aUK2w==")); 
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
System.out.println(Base64.encodeBase64String(cipher.doFinal(
    plainText.getBytes("UTF-8"))));

这一段JavaScript正确地将其解密:

This piece of JavaScript correctly decrypts it:

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script>
var encrypted = CryptoJS.enc.Base64.parse('3Q7r1iqtaRuJCo6QHA9/GhkTmbl4VkitV9ZsD3K2VB7LuBNg4enkJUA1cF8cHyovUH2N/jFz3kbq0QsHfPByCg==');
var key = CryptoJS.enc.Base64.parse('u/Gu5posvwDsXUnV5Zaq4g==');
var iv = CryptoJS.enc.Base64.parse('5D9r9ZVzEYYgha93/aUK2w==');
document.write(CryptoJS.enc.Utf8.stringify(CryptoJS.AES.decrypt(
    { ciphertext: encrypted },
    key, 
    { mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7, iv: iv,  })));
</script>