加密法(AES,MD5)----对String加密

 对String的加密是在对byte[]的加密基础上进行的。

就是把String转回byte数组输入,然后在把byte数组转回String输出

 

加密:

 

private String getEncString(String msg) {

byte[] ming = null;

byte[] enc = null;

String mStr = "";

BASE64Encoder base64en = new BASE64Encoder();

try {

ming = msg.getBytes("UTF-8");

enc = this.getEncCode(ming);

mStr = base64en.encode(enc);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} finally {

ming = null;

enc = null;

base64en = null;

}

return mStr;

}

 

 

解密:

 

private String getDecString(String msg) {

BASE64Decoder base64dc = new BASE64Decoder();

byte[] enc = null;

byte[] ming = null;

String decStr = "";

try {

enc = base64dc.decodeBuffer(msg);

ming = this.getDecCode(enc);

decStr = new String(ming, "UTF-8");

} catch (IOException e) {

e.printStackTrace();

} finally {

base64dc = null;

enc = null;

ming = null;

}

return decStr;

}

 
上面的方法在前一篇文章有写到。
 
 
MD5加密:
 
private  byte[] encryptMD5(String msg) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
byte[] src = msg.getBytes();
md5.update(src);
byte[] result = md5.digest();
return result;
}
 
使用MD5的digest方法加密返回一个byte[]数组,再转回想要的类型。
 
 
在这两个加密法里面注意的是
 
要声明用的是哪种加密法如MD5:MessageDigest.getInstance("MD5");
AES:c = Cipher.getInstance("AES");
 
如果有什么不对的地方,大家也可以指出来大家一起学习。
 

本文出自 “Just do it” 博客,请务必保留此出处http://davenzeng.blog.51cto.com/3896952/989640

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值