private static final int BIT = 128;
public static void main(String []args) {
long start = System.nanoTime();
AESDataTransformer des = new AESDataTransformer("811");
StringBuffer content = new StringBuffer();
content.append("BEGIN:VCARD");
content.append("VERSION:2.1");
content.append("N;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:;=E6=AC=A7=E9=98=B3=E9=94=8B;;;");
content.append("FN;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:=E6=AC=A7=E9=98=B3=E9=94=8B");
content.append("TEL;CELL:13163209158");
content.append("END:VCARD");
try {
//System.out.println(StringTools.getUTF8String(des.encode(StringTools.getUTF8Bytes(content.toString()))));
byte [] decodeContent = des.decode(des.encode(StringTools.getUTF8Bytes(content.toString())));
System.out.println(StringTools.getUTF8String(decodeContent));
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("time=" + ((System.nanoTime() - start)/ (1000 * 1000)));
}
public AESDataTransformer(String key) {
super(key);
// TODO Auto-generated constructor stub
}
public AESDataTransformer(String key, String name) {
super(key, name);
}
@Override
public byte[] encode(byte[] data) throws TransformerException {
KeyGenerator kgen;
try {
kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(StringTools.getUTF8Bytes(this.key));
kgen.init(BIT, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// ᄡᄡᄑ때ᅵᅡ→ᅥ
cipher.init(Cipher.ENCRYPT_MODE, key);// 뼈ᄐᄏᆵ
return StringTools.getUTF8Bytes(StringTools.byte2hex(cipher.doFinal(data))); // 투ᅢᅵ
} catch (Exception e) {
throw new TransformerException("Transformation error: ", e);
}
}
@Override
public String encode(String data) throws TransformerException {
return StringTools.getUTF8String(encode(StringTools.getUTF8Bytes(data)));
}
@Override
public byte[] decode(byte[] data) throws TransformerException {
KeyGenerator kgen;
try {
kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(StringTools.getUTF8Bytes(this.key));
kgen.init(BIT, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// ᄡᄡᄑ때ᅵᅡ→ᅥ
cipher.init(Cipher.DECRYPT_MODE, key);// 뼈ᄐᄏᆵ
return cipher.doFinal(StringTools.hex2byte(data)); // ᄑ¬ᅢᅵ
} catch (Exception e) {
throw new TransformerException("Transformation error: ", e);
}
}
@Override
public String decode(String data) throws TransformerException {
return StringTools.getUTF8String(decode(StringTools.getUTF8Bytes(data)));
}