Java:使用Base64编码的AES / CFB / NoPadding加密

时间:2018-04-05 02:04:46

标签: java go aes

我想使用AES / CFB / NoPadding加密Java中的字节。

我在Stackoverflow上发现了以下问题,但它只涵盖了解密函数:AES Encryption in Golang and Decryption in Java

如何在Java中编写类似的加密函数,如下面的Go代码?

func decrypt(key, data []byte) ([]byte, error) {
  blockcipher, err := aes.NewCipher(key)
  if err != nil {
    return nil, err
  }
  if len(data) < aes.BlockSize {
    return nil, errors.New("ciphertext too short")
  }
  iv := data[:aes.BlockSize]
  data = data[aes.BlockSize:]
  cfb := cipher.NewCFBDecrypter(blockcipher, iv)
  cfb.XORKeyStream(data, data)
  return data, nil
}

我的Golang解密函数看起来像这样(它应该返回base64代码):

private byte[] encrypt(byte[] payload) {
    try {
        SecretKeySpec key_spec = new SecretKeySpec(current_encryption_key, "AES");
        Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
        byte[] encoded_payload = Base64.encode(payload, Base64.DEFAULT);
        IvParameterSpec iv = new IvParameterSpec( new byte[16] );
        cipher.init(Cipher.ENCRYPT_MODE, key_spec, iv);
        return cipher.doFinal(encoded_payload);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new byte[0];
}

我当前的Java加密代码(我似乎无法解密)如下所示:

private byte[] decrypt(byte[] payload) {
    try {
        SecretKeySpec key_spec = new SecretKeySpec(current_encryption_key, "AES");
        Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
        int block_size = cipher.getBlockSize();
        IvParameterSpec iv = new IvParameterSpec( Arrays.copyOf(payload, block_size) );
        byte[] decryption_data = Arrays.copyOfRange(payload, block_size, payload.length);
        cipher.init(Cipher.DECRYPT_MODE, key_spec, iv);
        byte[] decrypted_payload = cipher.doFinal(decryption_data);
        return Base64.decode(decrypted_payload, Base64.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new byte[0];
}

我的加密代码看起来像这样(并且在Golang和Java上都能正常工作):

04-13 14:16:48.382 3791-3791/com.domain.interpretest W/System.err: java.lang.IllegalArgumentException: 16 > 9
04-13 14:16:48.388 3791-3791/com.domain.interpretest W/System.err:     at java.util.Arrays.copyOfRange(Arrays.java:3447)

当我用Java加密某些东西,然后尝试在Java中使用我的解密器时,我得到以下解密错误:

var result = string[myArray.Length]

1 个答案:

答案 0 :(得分:1)

Go加密摘要:

  • 使用随机IV(以及您不会描述的密钥)对base64-ed数据进行加密,将IV后跟密文放在一个缓冲区中

Java解密摘要:

  • 从缓冲区中获取第一个块并将其用作IV以解密缓冲区的其余部分,并将其解除基础

这些匹配。

Java加密摘要:

  • 使用固定(全零)IV加密,并返回仅包含密文的缓冲区,而不是IV任何地方

这不匹配。解密试图从没有IV的缓冲区中删除IV。实际上你的密文只有9个字节(虽然我不明白它为什么不是4的倍数);这比一个AES块短,因此Arrays.copyOfRange完全失败。

解决方案:您的Java加密应该使用随机IV并返回一个包含IV后跟密文的缓冲区,就像您的Go加密一样。一种非常接近模仿Go的方法:

// once, during initialization 
SecureRandom rand = new SecureRandom(); // or .getInstance* as you prefer 

// unchanged 
SecretKeySpec key_spec = new SecretKeySpec(current_encryption_key, "AES");
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
byte[] encoded_payload = Base64.encode(payload, Base64.DEFAULT);
// changed 
int block_size = cipher.getBlockSize();
// create random IV
byte[] buffer = new byte[block_size];
rand.nextBytes(buffer);
IvParameterSpec iv = new IvParameterSpec (buffer);
// expand buffer already containing IV to make room for ciphertext
buffer = Arrays.copyOf (buffer, block_size+encoded_payload.length);
// unchanged 
cipher.init(Cipher.ENCRYPT_MODE, key_spec, iv);
// changed
// do encryption into correct part of existing buffer
cipher.doFinal(encoded_payload,0,encoded_payload.length, buffer,block_size);
return buffer;

PS:你为什么要烦扰base64编码和解码你的明文?与所有现代密码一样,AES可以处理任何比特组合。当其他代码无法将它们作为位/二进制处理时,将密文和IV 更为常见。