使用SJCL加密的16byte初始向量解密AES-256 CCM

时间:2016-04-06 20:38:39

标签: java cryptography aes bouncycastle sjcl

我试图解密使用SJCL库加密的一些文本。 我正在使用Java和BouncyCastle库。

加密信息

Cipher: AES
KeySize: 256 bit
Mode: CCM
TagSize: 64
Iterations: 1000

Key: cb2ef7c5d226eb3cf199e528959af8235a4fdba27700ae2f2497dfe7d60f6dc6
Nonce/Initial Vector (Base64): qhjHH5yiBE6TWqOvoK02wA== 
Salt: Rbsu7nnilB8=
Ciphered Text: ajr3OLBXTJ+DaG2WuKl2yt7AWWt+RnbNuvfXeVUwU9INa/YZQrB4j0wz9sVzIF/y9D27MdGGKX79axqvyEHcFoj5rXQHMg+oFkdX9vwimBCGXuKTcqgPnu5YGtVO0tJUklkAAjYGWVLqN8BOlJRNoUWUx+54/29VaWsGOh8jZdRi4teSm/lPixqAF8hNXLaHjJfT8We4F6UOKTgUUfdDniNvY1XzPrcoI2z/jZ8=

Java代码

import java.io.UnsupportedEncodingException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.AESFastEngine;
import org.bouncycastle.crypto.modes.CCMBlockCipher;
import org.bouncycastle.crypto.params.CCMParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Base64;

public class Test {
  public static void main(String[] args) throws IllegalStateException, InvalidCipherTextException, UnsupportedEncodingException {
    String key = "cb2ef7c5d226eb3cf199e528959af8235a4fdba27700ae2f2497dfe7d60f6dc6";
    String iv = "qhjHH5yiBE6TWqOvoK02wA==";
    String cipherText = "ajr3OLBXTJ+DaG2WuKl2yt7AWWt+RnbNuvfXeVUwU9INa/YZQrB4j0wz9sVzIF/y9D27MdGGKX79axqvyEHcFoj5rXQHMg+oFkdX9vwimBCGXuKTcqgPnu5YGtVO0tJUklkAAjYGWVLqN8BOlJRNoUWUx+54/29VaWsGOh8jZdRi4teSm/lPixqAF8hNXLaHjJfT8We4F6UOKTgUUfdDniNvY1XzPrcoI2z/jZ8=";
    String expectedResult = "687073656c357658475367703656652f41475a456a586a47516f585262472f56784439716a5a7264686c553d";


    KeyParameter keyParameter = new KeyParameter(key.getBytes());


    //Tag size
    int tagSize = 64;
    //Nonce/IV
    byte[] nonce = Base64.decode(iv.getBytes()); //16 bytes length


    //Associated text
    byte[] associatedText = new byte[]{}; //No associated text

    //Converts cypherText to byte array
    byte[] ctByteArray = cipherText.getBytes();


    CCMParameters params = new CCMParameters(keyParameter, tagSize, nonce, associatedText);
    CCMBlockCipher ccmMode = new CCMBlockCipher(new AESFastEngine());

    boolean forEncryption = false;
    ccmMode.init(false, params);

    byte[] outputBytes = new byte[ccmMode.getOutputSize(ctByteArray.length)];
    int res = ccmMode.processBytes(ctByteArray, 0, ctByteArray.length, outputBytes, 0);
    ccmMode.doFinal(outputBytes, res);

    //Creates plaintext string with ASCII encoding
    String plainText = new String(outputBytes, "ASCII");


    if(plainText.equals(expectedResult)){
        System.out.println("OK!");
    } else {
        System.err.println("KO!");
    }
}

}

当我运行此代码时,我得到: " nonce的长度必须为7到13个八位字节"

我的nonce / iv有16个字节/八位字节,超过了此代码允许的数量。

关于如何解密这个的任何想法?我不允许更改SJCL加密设置......

0 个答案:

没有答案