如何在 Java 中使用 AES 加密设置 BlockSize 和 KeySize
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15434802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set BlockSize and KeySize using AES encription in java
提问by Jivan
I am trying to set the BlockSize and KeySize in java encryption code.
Here is my code that works fine but how to specify the aes.BlockSize=128 and aes.KeySize=128?
I have taking reference of Aes aes = AesManaged() in .NET in which we can set the following parameter as follows
我正在尝试在 java 加密代码中设置 BlockSize 和 KeySize。
这是我的代码运行良好,但如何指定 aes.BlockSize=128 和 aes.KeySize=128?
我参考了.NET中的Aes aes = AesManaged(),我们可以在其中设置以下参数如下
aes.BlockSize = 128;
aes.KeySize = 128;
CipherMode.ECB;
aes.Padding = PaddingMode.None;
In the code below I have set the following three parameters:
在下面的代码中,我设置了以下三个参数:
aes.Key = key
aes.Mode = CipherMode.ECB
aes.Padding = PaddingMode.None
but I am not able to set
但我无法设置
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();
回答by JB Nizet
Since you're initializing the cipher with a key of 16 bytes (128 bits), it uses that as the key size implicitely.
由于您使用 16 字节(128 位)的密钥初始化密码,因此它隐式地将其用作密钥大小。
And regarding the block size, the JCA specificationsays:
关于块大小,JCA 规范说:
AES is a 128-bit block cipher supporting keys of 128, 192, and 256 bits.
AES 是一种 128 位分组密码,支持 128、192 和 256 位的密钥。
So the block size is always 128-bits.
所以块大小总是 128 位。