BLOG main image
분류 전체보기 (42)
잡담 (6)
활용 (17)
language (3)
project (14)
design (2)
자료정리 (0)
결혼 준비 (0)
sarl pyver
sarl pyver
geometry dash apk zippy
geometry dash apk zippy
geometry dash apk full
geometry dash apk full
cheap christian louboutin
cheap christian louboutin
herehoju.com
herehoju.com
235,056 Visitors up to today!
Today 2 hit, Yesterday 22 hit
daisy rss
tistory 티스토리 가입하기!
2013.01.25 00:03

AES CBC 모드로 로 짧은 문자열을 암호화 하면 갑자기 긴 문자열이 튀어나온다.
이럴 땐 CTR NoPadding 모드로 암호화 하면 동일한 길이의 문자열이 나온다.

참고 : What encryption algorithm is best for small strings?


import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class CryptoTool {
	private byte[] key;
	public CryptoTool(String key) throws Exception {
		byte[] keyByte = key.getBytes("ASCII");
		if(keyByte.length != 16)
			throw new Exception("key length error");
		this.key = keyByte.clone();
	}

	public byte[] crypt(byte[] text) throws Exception {
		byte[] crypt = null;
		try {
			SecretKey aesKey = new SecretKeySpec(key, "AES");
			final Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
			cipher.init(Cipher.ENCRYPT_MODE, aesKey, new IvParameterSpec(key));
			crypt = cipher.doFinal(text);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
		return crypt;
	}

	public byte[] decrypt(byte[] text) throws Exception {
		SecretKey aesKey = new SecretKeySpec(key, "AES");
		final Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
		cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(key));
		return cipher.doFinal(text);
	}
}


Name
Password
Homepage
Secret