3
我試圖在java加密代碼中設置BlockSize和KeySize。
這是我的代碼工作正常,但如何指定aes.BlockSize = 128和aes.KeySize = 128?
我已採取在.NET AES公司AES = AesManaged()的參考,使我們可以設置以下參數如下如何在java中使用AES加密來設置BlockSize和KeySize
aes.BlockSize = 128;
aes.KeySize = 128;
CipherMode.ECB;
aes.Padding = PaddingMode.None;
在下面的代碼我已設置的以下三個參數:
aes.Key = key
aes.Mode = CipherMode.ECB
aes.Padding = PaddingMode.None
但我不能夠設置
aes.BlockSize = 128
aes.KeySize = 128;
public static void main(String args[]) {
byte[] keyForEncription = new byte[16];
byte[] keyForDecription = new byte[16];
long FixedKey = 81985526925837671L;
long VariableKey = 744818830;
for (int i1 = 0; i1 < 8; i1++) {
keyForEncription[i1] = (byte) (FixedKey >> (8 * i1));
keyForEncription[i1 + 8] = (byte) (VariableKey >> (8 * i1));
}
short[] data = new short[96];
data[0] = 2;
data[1] = 0;
data[2] = 0;
data[3] = 0;
data[4] = 0;
data[5] = 6;
data[6] = 6;
data[7] = 81;
data[8] = 124;
data[9] = 23;
data[10] = 3;
SecretKeySpec skeySpec = new SecretKeySpec(keyForEncription, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
CipherOutputStream cos = new CipherOutputStream(bos, cipher);
DataOutputStream dos = new DataOutputStream(cos);
byte[] byteArray_data = new byte[data.length];
for (int i1 = 0; i1 < data.length; i1++)
byteArray_data[i1] = (byte) data[i1];
dos.write(byteArray_data, 0, 16);
dos.close();
byte[] ENCRYPTED_DATA = bos.toByteArray();
for (int i1 = 0; i1 < 8; i1++) {
keyForDecription[i1] = (byte) (FixedKey >> (8 * i1));
keyForDecription[i1 + 8] = (byte) (VariableKey >> (8 * i1));
}
SecretKeySpec skeySpec_decryption = new SecretKeySpec(keyForDecription,
"AES");
Cipher cipher1 = Cipher.getInstance("AES/ECB/NoPadding");
cipher1.init(Cipher.DECRYPT_MODE, skeySpec_decryption);
ByteArrayInputStream bis = new ByteArrayInputStream(ENCRYPTED_DATA);
CipherInputStream cis = new CipherInputStream(bis, cipher1);
DataInputStream dis = new DataInputStream(cis);
byte[] DECRYPTED_DATA = new byte[byteArray_data.length];
dis.readFully(DECRYPTED_DATA, 4, 16);
cis.close();
AES總是128位,只支持128,192和256位密鑰。你對此感到困惑的是Rijndael,它支持更多的密鑰大小和不同的塊大小。顯然,.NET也混淆了它們(但只有當它在選擇AES規範之外的參數時沒有拋出異常,我不能從你的問題中推斷出來)。 – 2016-03-06 12:00:48