java主要的加密解密算法
如基本的单向加密算法:
- BASE64 严格地说,属于编码格式,而非加密算法
- MD5(Message Digest algorithm 5,信息摘要算法)
- SHA(Secure Hash Algorithm,安全散列算法)
- HMAC(Hash Message Authentication Code,散列消息鉴别码)
复杂的对称加密(DES、PBE)、非对称加密算法:
- DES(Data Encryption Standard,数据加密算法)
- PBE(Password-based encryption,基于密码验证)
- RSA(算法的名字以发明者的名字命名:Ron Rivest, AdiShamir 和Leonard Adleman)
- DH(Diffie-Hellman算法,密钥一致协议)
- DSA(Digital Signature Algorithm,数字签名)
- ECC(Elliptic Curves Cryptography,椭圆曲线密码编码学)
1、BASE64
Base64定义:Base64内容传送编码被设计用来把任意序列的8位字节描述为一种不易被人直接识别的形式。(The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.)
常见于邮件、http加密,截取http信息,你就会发现登录操作的用户名、密码字段通过BASE64加密的。
实例:
package cn.tzz.java.crypto;
import java.io.IOException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class BASE64Util {
/**加密*/
public static String encrypt(byte[] key) {
return (new BASE64Encoder()).encodeBuffer(key);
}
/**解密*/
public static String decrypt(String key) {
try {
return new String((new BASE64Decoder()).decodeBuffer(key),"utf-8");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String encryptStr = encrypt("123456".getBytes());
System.out.println("加密:"+encryptStr);
System.out.println("解密:"+decrypt(encryptStr));
}
}
2、MD5&SHA
MD5 -- message-digest algorithm 5 (信息-摘要算法)
SHA(Secure Hash Algorithm,安全散列算法)
package cn.tzz.java.crypto.simple;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5SHACryptoUtil {
/**md5加密*/
public static String md5Encrypt(String str) {
String md5String = null;
try {
// 获得MD5摘要算法的 MessageDigest 对象
MessageDigest md = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
md.update(str.getBytes());
// 获得密文,把密文转换成十六进制的字符串形式
//方式一
md5String = byte2Hex(md.digest());
//方式二
//md5String = byteToHex(md.digest());
//方式三
//md5String = byteToString(md.digest());
} catch (Exception ex) {
ex.printStackTrace();
}
return md5String;
}
/**SHA1加密*/
public static String shaEncrypt(String date) {
byte[] digest = null;
String rs = null;
try {
// 得到一个SHA-1的消息摘要
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
// 添加要进行计算摘要的信息
messageDigest.update(date.getBytes());
// 得到该摘要
digest = messageDigest.digest();
// 将摘要转为字符串
rs = byte2Hex(digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return rs;
}
// 1.把密文转换成十六进制的字符串形式(Integer.toHexString函数)
public static String byte2Hex(byte[] b) {
StringBuffer sb = new StringBuffer();
String tmp = "";
for (int i = 0; i < b.length; i++) {
tmp = Integer.toHexString(b[i] & 0XFF);
if (tmp.length() == 1){
sb.append("0");
}
sb.append(tmp);
}
return sb.toString();
}
// 2.把密文转换成十六进制的字符串形式(自定义)
public static String byteToHex(byte[] b) {
// 全局数组
char[] hexDigits = {'0' , '1' , '2' , '3' , '4' , '5' ,'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f'};
// 把密文转换成十六进制的字符串形式
int j = b.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = b[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
}
// 3.转换字节数组为16进制字串
private static String byteToString(byte[] b) {
StringBuffer sBuffer = new StringBuffer();
for (int i = 0; i < b.length; i++) {
sBuffer.append(byteToArrayString(b[i]));
}
return sBuffer.toString();
}
// 返回形式为数字跟字符串
private static String byteToArrayString(byte b) {
String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
int iRet = b;
if (iRet < 0) {
iRet += 256;
}
int iD1 = iRet / 16;
int iD2 = iRet % 16;
return strDigits[iD1] + strDigits[iD2];
}
public static void main(String[] args) {
System.out.println(md5Encrypt("123456"));
System.out.println(shaEncrypt("123456"));
}
}