function go() {
var pwd =$("#pwd").val();
var key = CryptoJS.enc.Utf8.parse("abcdefgabcdefg12");
var srcs = CryptoJS.enc.Utf8.parse(pwd);
var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
To ensure that both Java and JavaScript are using AES encryption with GCM mode, we need to align the encryption and decryption processes by ensuring they use the same configuration and compatible libraries. Below are examples demonstrating how to perform encryption in JavaScript using AES-GCM mode and decrypt it in Java:
JavaScript Encryption (using CryptoJS)
First, ensure you're using a library that supports AES-GCM. While CryptoJS commonly provides AES-ECB and other modes, AES-GCM might need a different library or additional configuration. However, here's a general example with another library for AES-GCM encryption in JavaScript using Web Crypto API, which natively supports AES-GCM:
javascript
async function encryptMessage(message, keyStr) {
const enc = new TextEncoder();
const key = await crypto.subtle.importKey(
'raw',
enc.encode(keyStr),
{ name: 'AES-GCM' },
false,
['encrypt']
);
const iv = crypto.getRandomValues(new Uint8Array(12)); // create a random IV
const encryptedContent = await crypto.subtle.encrypt(
{
name: 'AES-GCM',
iv: iv
},
key,
enc.encode(message)
);
const encryptedBytes = new Uint8Array(encryptedContent);
const combined = new Uint8Array(iv.length + encryptedBytes.length);
combined.set(iv, 0);
combined.set(encryptedBytes, iv.length);
return combined;
}
// Usage Example
const message = 'Your message here';
const key = 'abcdefgabcdefg12'; // Either 16, 24, or 32 bytes for AES
encryptMessage(message, key).then(encryptedData => {
console.log(Array.from(encryptedData)); // This 'encryptedData' goes to your server and will be used for decryption in Java.
});
Java Decryption
In your Java code, you can use the same method you've already written. Just ensure that when you're encrypting in JavaScript, the IV is appropriately handled (as generated and prefixed in the combined byte array shown above).
java
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class AESGCMDecryptor {
public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
// GCM 的推荐 IV 长度为 12 字节
byte[] iv = new byte[12];
System.arraycopy(encryptBytes, 0, iv, 0, iv.length);
// 提取实际的加密数据
byte[] encryptedBytes = Arrays.copyOfRange(encryptBytes, iv.length, encryptBytes.length);
// 准备密钥
SecretKeySpec secretKeySpec = new SecretKeySpec(decryptKey.getBytes(StandardCharsets.UTF_8), "AES");
// 使用 AES/GCM 模式解密
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec parameterSpec = new GCMParameterSpec(128, iv); // 128 位认证标签
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, parameterSpec);
// 解密数据
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
}
Notes:
The key size for AES must be either 16, 24, or 32 bytes long.
AES-GCM requires a unique IV for every encryption. The IV is crucial for security with GCM mode as using the same IV can lead to security vulnerabilities.
Ensure the JavaScript code runs in an environment that supports the Web Crypto API, typically in most modern browsers.