aes 算法 java_java实现AES算法 | 学步园

该博客主要展示了使用AES数据加密器进行加密和解密操作的性能测试。代码中创建了一个AESDataTransformer实例,对特定字符串进行UTF-8编码,然后加密和解密。测试结果显示了整个过程所花费的时间,强调了加密解密的速度。
摘要由CSDN通过智能技术生成

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)));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值