1.可逆的加密解密 举3des为例
参考http://java.sun.com/j2se/1.5.0/docs/guide/security/CryptoSpec.html#AppA
key的构造函数
new SecretKeySpec(字符数组,算法名称);
// 3des长度固定为24字节
用Cipher 工具类加密解密
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);//加密模式
cipher.doFinal(明文);
cipher.init(Cipher.DECRYPT_MODE, key);//解密模式
明文是字节数组,密文也是。所以需要把字节数组转为字符串。方便保存。
2.
算法摘要 举md5为例
参考文章:http://java.sun.com/j2se/1.5.0/docs/guide/security/CryptoSpec.html#MessageDigest
信息摘要是安全的单向哈希函数,它接收任意大小的数据,输出固定长度的哈希值。
md5算法一般是明文和另外一个字符串key相加再加密
输出固定32位
MessageDigest
MessageDigest alg = MessageDigest.getInstance("MD5");
alg.update(明文.getBytes());
alg.digest();//摘要