如何在android中使用aes-256-gcm加密带有重音的文本



我尝试用重音加密文本,节点js服务器无法解密,我得到响应"错误:不支持的状态或无法验证数据"。我该怎么办?

这是Android文件加密文本。

public static String encrypt(String text, String masterKey) throws InvalidKeySpecException, NoSuchAlgorithmException, IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException {
byte[] iv = generateIv(16);
byte[] salt = generateIv(64);
SecretKey key = getKeyFromPassword(masterKey, salt);
byte[] cipher = encryptHelper("AES/GCM/NoPadding", text, key,  new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] ciphertext = Arrays.copyOfRange(cipher, 0, text.length());
byte[] tag = Arrays.copyOfRange(cipher, text.length(), cipher.length);
outputStream.write(salt);
outputStream.write(iv);
outputStream.write(tag);
outputStream.write(ciphertext);
String base64Encrypted;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
base64Encrypted = Base64.getEncoder().encodeToString(outputStream.toByteArray());
}else{
base64Encrypted = android.util.Base64.encodeToString(outputStream.toByteArray(), android.util.Base64.DEFAULT);
}
return base64Encrypted;
}
public static byte[] encryptHelper(String algorithm, String input, SecretKey key,
GCMParameterSpec gcmParameterSpec) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key, gcmParameterSpec);
return cipher.doFinal(input.getBytes(StandardCharsets.UTF_8));
}

这里是节点js文件解密文本。

function decryptDataTestWork(encdata, masterkey) {
// body...
// base64 decoding
const bData = Buffer.from(encdata, 'base64');
// convert data to buffers
const salt = bData.slice(0, 64);
const iv = bData.slice(64, 80);
const tag = bData.slice(80, 96);
const text = bData.slice(96);
/*console.log(salt);
console.log(iv);
console.log(tag);
console.log(text);*/
// derive key using; 32 byte key length
const key = crypto.pbkdf2Sync(masterkey, salt, 2145, 32, 'sha512');
// AES 256 GCM Mode
const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
decipher.setAuthTag(tag);
// encrypt the given text
return decipher.update(text, 'binary', 'utf8') + decipher.final('utf8');
};

我得到这个响应

Error: Unsupported state or unable to authenticate data

这里是文本和键

"description":"Désolé"
"key":"tIJg9tyj2setkg7gknVreP5PXzRQV5J3"

我该怎么做?谢谢!

问题出在Java代码中,并且错误地分离了密文和标签。

您从明文大小确定密文大小(这对于GCM来说是可能的,因为没有填充),但随后假设字符串text与字节数组text.getBytes(StandardCharsets.UTF_8)具有相同的大小,如果文本字符在多个字节上编码,则通常不会出现这种情况。

下面的实现使用标签大小来分隔这两个部分:

byte[] ciphertext = Arrays.copyOfRange(cipher, 0, cipher.length - GCM_TAG_LENGTH);
byte[] tag = Arrays.copyOfRange(cipher, cipher.length - GCM_TAG_LENGTH, cipher.length);

有了这个改变,在我的机器上使用NodeJS代码解密是成功的,假设相同的密钥(注意,密钥派生不能检查,因为getKeyFromPassword()在Java端缺失)。

修复后建议代码工作正常

public static String encrypt(String text, String masterKey) throws InvalidKeySpecException, NoSuchAlgorithmException, IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException {
byte[] iv = generateIv(16);
byte[] salt = generateIv(64);
SecretKey key = getKeyFromPassword(masterKey, salt);
byte[] cipher = encryptHelper("AES/GCM/NoPadding", text, key,  new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] ciphertext = Arrays.copyOfRange(cipher, 0, cipher.length - GCM_TAG_LENGTH);
byte[] tag = Arrays.copyOfRange(cipher, cipher.length - GCM_TAG_LENGTH, cipher.length);
outputStream.write(salt);
outputStream.write(iv);
outputStream.write(tag);
outputStream.write(ciphertext);
String base64Encrypted;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
base64Encrypted = Base64.getEncoder().encodeToString(outputStream.toByteArray());
}else{
base64Encrypted = android.util.Base64.encodeToString(outputStream.toByteArray(), android.util.Base64.DEFAULT);
}
return base64Encrypted;
}
public static SecretKey getKeyFromPassword(String masterKey, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
KeySpec spec = new PBEKeySpec(masterKey.toCharArray(), salt, 2145, 256);
return new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
}
public static byte[] encryptHelper(String algorithm, String input, SecretKey key,
GCMParameterSpec gcmParameterSpec) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key, gcmParameterSpec);
return cipher.doFinal(input.getBytes(StandardCharsets.UTF_8));
}
public static byte[] generateIv(int N) {
byte[] iv = new byte[N];
new SecureRandom().nextBytes(iv);
return iv;
}

谢谢你@Topaco!你救了我!

最新更新