不可逆 ,但是有人可以碰撞出来
信息摘要MD5:
ublic class MD5 { public static final String KEY_MD5 = "MD5"; public static String getResult(String inputStr) { System.out.println("=======加密前的数据:"+inputStr); BigInteger bigInteger=null; try { MessageDigest md = MessageDigest.getInstance(KEY_MD5); byte[] inputData = inputStr.getBytes(); md.update(inputData); bigInteger = new BigInteger(md.digest()); } catch (Exception e) {e.printStackTrace();} System.out.println("MD5加密后:" + bigInteger.toString(16)); return bigInteger.toString(16); } public static void main(String args[]) { try { String inputStr = "简单加密8888888888888888888"; getResult(inputStr); } catch (Exception e) { e.printStackTrace(); } } }SHA
安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准(Digital Signature Standard DSS)
public class SHA { public static final String KEY_SHA = "SHA"; public static String getResult(String inputStr) { BigInteger sha =null; System.out.println("=======加密前的数据:"+inputStr); byte[] inputData = inputStr.getBytes(); try { MessageDigest messageDigest = MessageDigest.getInstance(KEY_SHA); messageDigest.update(inputData); sha = new BigInteger(messageDigest.digest()); System.out.println("SHA加密后:" + sha.toString(32)); } catch (Exception e) {e.printStackTrace();} return sha.toString(32); } public static void main(String args[]) { try { String inputStr = "简单加密"; getResult(inputStr); } catch (Exception e) { e.printStackTrace(); } } }
可逆
Base64 DESS 3DES RSA
RSA易于理解
public class Rsa {
/**
* 获取公密钥
*
*/
public static Map
try {
KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
rsa.initialize(2048);
KeyPair keyPair = rsa.generateKeyPair();
//存取公钥
PublicKey aPublic = keyPair.getPublic();
//存取私钥
PrivateKey aPrivate = keyPair.getPrivate();
Map
keyMap.put("PUBLIC_KEY",aPublic);
keyMap.put("PRIVATE_KEY",aPrivate);
return keyMap;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
//取出公钥
public static String getPublicKey(Map
Key key = (Key) keymap.get("PUBLIC_KEY");
return new String(key.getEncoded());
}
//取出密钥
public static String getPrivateKey(Map
Key key = (Key) keyMap.get("PRIVATE_KEY");
return new String(key.getEncoded());
}
//公钥加密
public static String encryptByPublicKey(String data, RSAPublicKey publicKey){
try {
Cipher rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.ENCRYPT_MODE,publicKey);
//模长
int key_len = publicKey.getModulus().bitLength()/8;
//加密数据长度<=模长-11
String[] datas = {"",""};//splitString(data,key_len - 11);
String mi = "";
for (String s : datas) {
mi += "";//rsa.doFinal(s.getBytes());
}
return mi;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return null;
}
}
DESS比较简便 但是秘钥生成方式有点复杂
package com.zms.classify;
public class DESS {
public static byte[] encryMode(String keyStr,byte[] src){
//生成密钥
try {
SecretKey deSede = new SecretKeySpec(keyStr.getBytes(), "DESede");
Cipher deSede1 = Cipher.getInstance("DESede");
deSede1.init(Cipher.ENCRYPT_MODE,deSede);
return deSede1.doFinal(src);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
return null;
}
//
// public DESedeEncrypt() throws Exception
// {
// // 固定密钥
// byte[] buffer = new byte[]{0x6B, 0x46, 0x0E, 0x64, 0x0D, 0x70, 0x5B, 0x7A, (byte) 0xD9, 0x37, 0x68,
// 0x67, (byte) 0xE0, 0x0B, (byte) 0xE9, 0x2C, 0x3D, 0x52, (byte) 0xF1, 0x4F, (byte) 0xF1, (byte) 0x9E, (byte) 0xFE, (byte) 0x94};
//
// System.out.println("初始化钥匙");
// byte[] buffer = new byte[]{0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
// 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31};
// byte[] buffer = new byte[]{0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef,
// (byte) 0xfe, (byte) 0xdc, (byte) 0xba, (byte) 0x98, 0x76, 0x54, 0x32, 0x10,
// (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67 };
// byte[] buffer = new byte[]{0x47, 0x33, 0x43, 0x4D, 0x4F, 0x50, 0x31, 0x32, 0x33,
// 0x47, 0x33, 0x43, 0x4D, 0x4F, 0x50, 0x31, 0x32, 0x33, 0x47, 0x33, 0x43, 0x4D, 0x4F, 0x50};
//
// SecretKeySpec key = new SecretKeySpec(buffer, "DESede");
//
//
// ThreeDES des = new ThreeDES();// 实例化一个对像
// des.getKey("G3CMOP123G3CMOP123G3CMOP");// 生成密匙
//
// Key key = des.key;
//
// encryptCipher = Cipher.getInstance(KEY_ALGORITHM);
// encryptCipher.init(Cipher.ENCRYPT_MODE, key);
//
// decryptCipher = Cipher.getInstance("DESede/ECB/NoPadding");
// decryptCipher.init(Cipher.DECRYPT_MODE, key);
// }
//加密和解密一样 但是密钥需的构造方法生成
public static void main(String[] args) {
System.out.println(encryMode("1432142143214312", "456".getBytes()));
}
}