DES加密:已经被破解的,但安全性还算ok,比较流行的一种加密方式。
1 package com.cmit.testing.utils.des; 2 3 import javax.crypto.Cipher; 4 import javax.crypto.SecretKey; 5 import javax.crypto.SecretKeyFactory; 6 import javax.crypto.spec.DESKeySpec; 7 import javax.crypto.spec.IvParameterSpec; 8 9 10 public class Des { 11 12 13 /** 14 * 加密 15 * @param data 16 * @param sKey 17 * @return 18 */ 19 public static byte[] encrypt(byte[] data, String sKey) { 20 try { 21 byte[] key = sKey.getBytes(); 22 // 初始化向量 23 IvParameterSpec iv = new IvParameterSpec(key); 24 DESKeySpec desKey = new DESKeySpec(key); 25 // 创建一个密匙工厂,然后用它把DESKeySpec转换成securekey 26 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); 27 SecretKey securekey = keyFactory.generateSecret(desKey); 28 // Cipher对象实际完成加密操作 29 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); 30 // 用密匙初始化Cipher对象 31 cipher.init(Cipher.ENCRYPT_MODE, securekey, iv); 32 // 现在,获取数据并加密 33 // 正式执行加密操作 34 return cipher.doFinal(data); 35 } catch (Throwable e) { 36 e.printStackTrace(); 37 } 38 return null; 39 } 40 41 /** 42 * 解密 43 * @param src 44 * @param sKey 45 * @return 46 * @throws Exception 47 */ 48 public static byte[] decrypt(byte[] src, String sKey) throws Exception { 49 byte[] key = sKey.getBytes(); 50 // 初始化向量 51 IvParameterSpec iv = new IvParameterSpec(key); 52 // 创建一个DESKeySpec对象 53 DESKeySpec desKey = new DESKeySpec(key); 54 // 创建一个密匙工厂 55 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); 56 // 将DESKeySpec对象转换成SecretKey对象 57 SecretKey securekey = keyFactory.generateSecret(desKey); 58 // Cipher对象实际完成解密操作 59 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); 60 // 用密匙初始化Cipher对象 61 cipher.init(Cipher.DECRYPT_MODE, securekey, iv); 62 // 真正开始解密操作 63 return cipher.doFinal(src); 64 } 65 66 /** 67 * 将二进制转换成16进制 68 * 69 * @param buf 70 * @return 71 */ 72 public static String parseByte2HexStr(byte buf[]) { 73 StringBuffer sb = new StringBuffer(); 74 for (int i = 0; i < buf.length; i++) { 75 String hex = Integer.toHexString(buf[i] & 0xFF); 76 if (hex.length() == 1) { 77 hex = '0' + hex; 78 } 79 sb.append(hex.toUpperCase()); 80 } 81 return sb.toString(); 82 } 83 84 /** 85 * 将16进制转换为二进制 86 * 87 * @param hexStr 88 * @return 89 */ 90 public static byte[] parseHexStr2Byte(String hexStr) { 91 if (hexStr.length() < 1) return null; 92 byte[] result = new byte[hexStr.length() / 2]; 93 for (int i = 0; i < hexStr.length() / 2; i++) { 94 int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); 95 int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); 96 result[i] = (byte) (high * 16 + low); 97 } 98 return result; 99 } 100 }
1 package com.cmit.testing.utils.des; 2 3 import java.nio.charset.Charset; 4 5 6 public class DesUtil { 7 8 /** 9 * 加密 10 * @param srcStr 11 * @param charset 12 * @param sKey 13 * @return 14 */ 15 public static String encrypt(String srcStr, Charset charset, String sKey) { 16 byte[] src = srcStr.getBytes(charset); 17 byte[] buf = Des.encrypt(src, sKey); 18 return Des.parseByte2HexStr(buf); 19 } 20 21 /** 22 * 解密 23 * 24 * @param hexStr 25 * @param sKey 26 * @return 27 * @throws Exception 28 */ 29 public static String decrypt(String hexStr, Charset charset, String sKey) throws Exception { 30 byte[] src = Des.parseHexStr2Byte(hexStr); 31 byte[] buf = Des.decrypt(src, sKey); 32 return new String(buf, charset); 33 } 34 35 private static final String SKEY = "abcdefgh"; 36 private static final Charset CHARSET = Charset.forName("gb2312"); 37 38 public static void main(String[] args) { 39 // 待加密内容 40 String str = "dda@1313"; 41 String encryptResult = DesUtil.encrypt(str, CHARSET, SKEY); 42 System.out.println(encryptResult); 43 // 直接将如上内容解密 44 String decryResult = ""; 45 try { 46 decryResult = DesUtil.decrypt(encryptResult, CHARSET, SKEY); 47 } catch (Exception e1) { 48 e1.printStackTrace(); 49 } 50 System.out.println(decryResult); 51 } 52 }
AES:较为流行,安全性良好
1 package com.cmit.testing; 2 3 import java.io.IOException; 4 import java.io.UnsupportedEncodingException; 5 import java.security.InvalidKeyException; 6 import java.security.NoSuchAlgorithmException; 7 import java.security.SecureRandom; 8 import java.util.Base64; 9 import java.util.Scanner; 10 11 import javax.crypto.BadPaddingException; 12 import javax.crypto.Cipher; 13 import javax.crypto.IllegalBlockSizeException; 14 import javax.crypto.KeyGenerator; 15 import javax.crypto.NoSuchPaddingException; 16 import javax.crypto.SecretKey; 17 import javax.crypto.spec.SecretKeySpec; 18 19 import sun.misc.BASE64Decoder; 20 import sun.misc.BASE64Encoder; 21 22 /* 23 * AES对称加密和解密 24 */ 25 public class SymmetricEncoder { 26 27 static String encodeRules="hhhhhh"; 28 /* 29 * 加密 30 * 1.构造密钥生成器 31 * 2.根据ecnodeRules规则初始化密钥生成器 32 * 3.产生密钥 33 * 4.创建和初始化密码器 34 * 5.内容加密 35 * 6.返回字符串 36 */ 37 public static String AESEncode(String content){ 38 try { 39 //1.构造密钥生成器,指定为AES算法,不区分大小写 40 KeyGenerator keygen=KeyGenerator.getInstance("AES"); 41 //2.根据ecnodeRules规则初始化密钥生成器 42 //生成一个128位的随机源,根据传入的字节数组 43 keygen.init(128, new SecureRandom(encodeRules.getBytes())); 44 //3.产生原始对称密钥 45 SecretKey original_key=keygen.generateKey(); 46 //4.获得原始对称密钥的字节数组 47 byte [] raw=original_key.getEncoded(); 48 //5.根据字节数组生成AES密钥 49 SecretKey key=new SecretKeySpec(raw, "AES"); 50 //6.根据指定算法AES自成密码器 51 Cipher cipher=Cipher.getInstance("AES"); 52 //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY 53 cipher.init(Cipher.ENCRYPT_MODE, key); 54 //8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码 55 byte [] byte_encode=content.getBytes("utf-8"); 56 //9.根据密码器的初始化方式--加密:将数据加密 57 byte [] byte_AES=cipher.doFinal(byte_encode); 58 //10.将加密后的数据转换为字符串 59 //这里用Base64Encoder中会找不到包 60 //解决办法: 61 //在项目的Build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了。 62 String AES_encode=new String(new BASE64Encoder().encode(byte_AES)); 63 //11.将字符串返回 64 return AES_encode; 65 } catch (NoSuchAlgorithmException e) { 66 e.printStackTrace(); 67 } catch (NoSuchPaddingException e) { 68 e.printStackTrace(); 69 } catch (InvalidKeyException e) { 70 e.printStackTrace(); 71 } catch (IllegalBlockSizeException e) { 72 e.printStackTrace(); 73 } catch (BadPaddingException e) { 74 e.printStackTrace(); 75 } catch (UnsupportedEncodingException e) { 76 e.printStackTrace(); 77 } 78 79 //如果有错就返加nulll 80 return null; 81 } 82 /* 83 * 解密 84 * 解密过程: 85 * 1.同加密1-4步 86 * 2.将加密后的字符串反纺成byte[]数组 87 * 3.将加密内容解密 88 */ 89 public static String AESDncode(String content){ 90 try { 91 //1.构造密钥生成器,指定为AES算法,不区分大小写 92 KeyGenerator keygen=KeyGenerator.getInstance("AES"); 93 //2.根据ecnodeRules规则初始化密钥生成器 94 //生成一个128位的随机源,根据传入的字节数组 95 keygen.init(128, new SecureRandom(encodeRules.getBytes())); 96 //3.产生原始对称密钥 97 SecretKey original_key=keygen.generateKey(); 98 //4.获得原始对称密钥的字节数组 99 byte [] raw=original_key.getEncoded(); 100 //5.根据字节数组生成AES密钥 101 SecretKey key=new SecretKeySpec(raw, "AES"); 102 //6.根据指定算法AES自成密码器 103 Cipher cipher=Cipher.getInstance("AES"); 104 //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY 105 cipher.init(Cipher.DECRYPT_MODE, key); 106 //8.将加密并编码后的内容解码成字节数组 107 byte [] byte_content= new BASE64Decoder().decodeBuffer(content); 108 /* 109 * 解密 110 */ 111 byte [] byte_decode=cipher.doFinal(byte_content); 112 String AES_decode=new String(byte_decode,"utf-8"); 113 return AES_decode; 114 } catch (NoSuchAlgorithmException e) { 115 e.printStackTrace(); 116 } catch (NoSuchPaddingException e) { 117 e.printStackTrace(); 118 } catch (InvalidKeyException e) { 119 e.printStackTrace(); 120 } catch (IOException e) { 121 e.printStackTrace(); 122 } catch (IllegalBlockSizeException e) { 123 e.printStackTrace(); 124 } catch (BadPaddingException e) { 125 e.printStackTrace(); 126 } 127 128 //如果有错就返加nulll 129 return null; 130 } 131 132 public static void main(String[] args) { 133 SymmetricEncoder se=new SymmetricEncoder(); 134 String content="abc@123"; 135 /* 136 * 加密 137 */ 138 String s = se.AESEncode(content); 139 System.out.println("根据输入的规则"+encodeRules+"加密后的密文是:"+s); 140 141 /* 142 * 解密 143 */ 144 System.out.println("根据输入的规则"+encodeRules+"解密后的明文是:"+se.AESDncode(s)); 145 } 146 147 }
异或加密:很普通的加密,可以和前端代码混用,其实就是很多用于不展示明文,实际上能很轻易破解
1 package com.cmit.testing.utils; 2 3 public class XORUtl { 4 public static final int KEY = 7; 5 6 public static void main(String[] args) { 7 String account = "lwj"; 8 String password = "1234"; 9 String secretKey= XORUtl.encry(account+"@"+password); 10 System.out.println("原始 的字符串为:" + account+"@"+password); 11 System.out.println("原始 的字符串为:" +"sbtsGFdc&576?"); 12 System.out.println("加密后 的字符串为:" + secretKey); 13 System.out.println("解密后 的字符串为:" + XORUtl.decryPwd("sbtsGFdc&576?")); 14 String[] split = XORUtl.decryPwd(secretKey).split("@"); 15 System.out.println(split[0]); 16 System.out.println(split[1]); 17 } 18 19 /** 20 * 加密 21 * 22 * @param str 23 * @return 24 */ 25 public static String encry(String str) { 26 StringBuilder str2 = new StringBuilder(); 27 //加密过程 28 for (int i = 0; i < str.length(); i++) { 29 char c = (char) (str.charAt(i) ^ KEY); 30 str2.append(c); 31 } 32 return str2.toString(); 33 } 34 35 /** 36 * 解密 37 * 38 * @param str 39 * @return 40 */ 41 public static String decryPwd(String str) { 42 StringBuilder str3 = new StringBuilder(); 43 //解密过程 44 for (int i = 0; i < str.length(); i++) { 45 char c = (char) (str.charAt(i) ^ KEY); 46 str3.append(c); 47 } 48 return str3.toString(); 49 } 50 }