java中加密解密⼯具类
在⼯作中经常遇到需要加密、解密的场景。例如⽤户的⼿机号等信息,在保存到数据库的过程中,需要对数据进⾏加密。取出时进⾏解密。
public class DEStool {
private String sKey;
public DEStool() {
//默认构造函数提供默认密钥
sKey = "des@#$12";
}
public DEStool(String securityKey) {
if (securityKey.length() < 8) {
throw new IllegalArgumentException("密钥长度⾄少8位");
}
this.sKey = securityKey;
}
private Cipher makeCipher() throws Exception{
Instance("DES");
java加密方式有哪些}
private SecretKey makeKeyFactory() throws Exception{
SecretKeyFactory des = Instance("DES");
SecretKey secretKey = ateSecret(new Bytes()));
return secretKey;
}
public String encrypt(String text) throws Exception{
Cipher cipher = makeCipher();
SecretKey secretKey = makeKeyFactory();
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return new deBase64(cipher.Bytes())));
}
public String decrypt(String text) throws Exception{
Cipher cipher = makeCipher();
SecretKey secretKey = makeKeyFactory();
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.Bytes())));
}
public static void main(String[] args) {
DEStool tool = new DEStool("nice1234");
String content = "中国";
System.out.println("原⽂内容:"+content);
String encrpt = null;
try {
encrpt = pt(content);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("加密后:"+encrpt + ", 长度=" + encrpt.length());
String descript =null;
try {
descript = tool.decrypt(encrpt);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("解密后:" + descript);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论