AES CBC with padding Example
128, 192, 256의 의미는 키 길이의 bit수를 의미한다.
128 bit은 16 byte key 길이, 192 bit은 24 byte key 길이, 192 bit은 32 byte key 길이를 의미한다.
block size에 맞게 padding하여 암호화 / 복호화하는 예제
참고사항) Bouncy castle에서 Padding 방식을 지정하지 않을 경우 default padding 방식은 PKCS7 방식을 사용한다.
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[] aesCbcPadEncrypt(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 CBCBlockCipher(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[] aesCbcPadDecrypt(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 CBCBlockCipher(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 aesCbcPadExample() throws InvalidCipherTextException {
byte[] text = "pass".getBytes(StandardCharsets.UTF_8);
byte[] ret;
ret = aesCbcPadEncrypt(aes_128bit_16byte, text, null);
ret = aesCbcPadDecrypt(aes_128bit_16byte, ret, null);
System.out.println(new String(ret, StandardCharsets.UTF_8));
ret = aesCbcPadEncrypt(aes_192bit_24byte, text, null);
ret = aesCbcPadDecrypt(aes_192bit_24byte, ret, null);
System.out.println(new String(ret, StandardCharsets.UTF_8));
ret = aesCbcPadEncrypt(aes_256bit_32byte, text, null);
ret = aesCbcPadDecrypt(aes_256bit_32byte, ret, null);
System.out.println(new String(ret, StandardCharsets.UTF_8));
}
결과: 각 키 길이별 첫번째 라인은 암호화한 결과, 두번째 라인은 복호화한 결과
// 128 bit 16 byte
8B:7A:92:30:79:B7:68:4A:29:D2:B9:2B:69:3D:E8:59
pass
// 192 bit 24 byte
F1:8D:FB:DC:58:A9:72:37:D2:D7:35:63:C1:EA:FC:11
pass
// 256 bit 32 byte
75:1E:9E:34:DE:A2:F8:20:E5:F5:FF:94:61:2C:B5:7B
pass
코드위치
'bouncy castle' 카테고리의 다른 글
[bouncy castle] hash md5 (0) | 2023.07.31 |
---|---|
[bouncy castle] AES CTR (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 |
[bouncy castle] AES CBC (128, 192, 256) example (0) | 2023.07.30 |