AES ECB Example
128, 192, 256의 의미는 키 길이의 bit수를 의미한다.
128 bit은 16 byte key 길이, 192 bit은 24 byte key 길이, 192 bit은 32 byte key 길이를 의미한다.
32 byte block을 암호화 / 복호화하는 예제
static byte[] aes_128bit_16byte = "ABCDEFGHIJKLMNOP".getBytes(StandardCharsets.UTF_8);
static byte[] aes_192bit_24byte = "ABCDEFGHIJKLMNOPQRSTUVWX".getBytes(StandardCharsets.UTF_8);
static byte[] aes_256bit_32byte = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef".getBytes(StandardCharsets.UTF_8);
public byte[] aesCtrPadEncrypt(byte[] key, byte[] text, byte[] iv) throws InvalidCipherTextException {
if (iv == null) {
iv = new byte[16];
}
// create AES cipher
BlockCipher engine = new AESEngine();
// Padding cipher (adjust input data length to des block size)
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new SICBlockCipher(engine));
// set key
cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv));
int outputSize = cipher.getOutputSize(text.length);
byte[] outBuff = new byte[outputSize];
int offset = cipher.processBytes(text, 0, text.length, outBuff, 0);
cipher.doFinal(outBuff, offset);
return outBuff;
}
public byte[] aesCtrPadDecrypt(byte[] key, byte[] text, byte[] iv) throws InvalidCipherTextException {
if (iv == null) {
iv = new byte[16];
}
// create AES cipher
BlockCipher engine = new AESEngine();
// Padding cipher (adjust input data length to des block size)
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new SICBlockCipher(engine));
// set key
cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv));
int outputSize = cipher.getOutputSize(text.length);
byte[] outBuff = new byte[outputSize];
int offset = cipher.processBytes(text, 0, text.length, outBuff, 0);
int ret = cipher.doFinal(outBuff, offset);
return Arrays.copyOf(outBuff, offset+ret);
}
void aesCtrPadExample() throws InvalidCipherTextException {
byte[] text = "pass".getBytes(StandardCharsets.UTF_8);
byte[] ret;
ret = aesCtrPadEncrypt(aes_128bit_16byte, text, null);
print(ret);
ret = aesCtrPadDecrypt(aes_128bit_16byte, ret, null);
System.out.println(new String(ret, StandardCharsets.UTF_8));
ret = aesCtrPadEncrypt(aes_192bit_24byte, text, null);
print(ret);
ret = aesCtrPadDecrypt(aes_192bit_24byte, ret, null);
System.out.println(new String(ret, StandardCharsets.UTF_8));
ret = aesCtrPadEncrypt(aes_256bit_32byte, text, null);
print(ret);
ret = aesCtrPadDecrypt(aes_256bit_32byte, ret, null);
System.out.println(new String(ret, StandardCharsets.UTF_8));
}
결과: 각 키 길이별 첫번째 라인은 암호화한 결과, 두번째 라인은 복호화한 결과
// 128 bit 16 byte
F5:14:A2:9B:9F:9D:75:D1:A4:69:12:E2:24:F4:91:71
pass
// 192 bit 24 byte
39:94:A7:74:AA:25:13:12:4F:31:D2:7E:0F:2E:35:E6
pass
// 256 bit 32 byte
F0:C4:1A:4E:53:0F:9C:90:13:76:CF:8A:9A:C8:F2:89
pass
코드위치
'bouncy castle' 카테고리의 다른 글
[bouncy castle] hash SHA1, SHA256, SHA512 (0) | 2023.07.31 |
---|---|
[bouncy castle] hash md5 (0) | 2023.07.31 |
[bouncy castle] AES CBC (128, 192, 256) with padding example (0) | 2023.07.30 |
[bouncy castle] AES ECB (128, 192, 256) with padding example (0) | 2023.07.30 |
[bouncy castle] AES CTR (128, 192, 256) example (0) | 2023.07.30 |