我正在尝试解密一些使用 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 加密设置...