标签:
1 package com.example.decript; 2 3 import java.io.UnsupportedEncodingException; 4 import java.security.InvalidKeyException; 5 import java.security.MessageDigest; 6 import java.security.NoSuchAlgorithmException; 7 import java.security.SecureRandom; 8 import javax.crypto.BadPaddingException; 9 import javax.crypto.Cipher; 10 import javax.crypto.IllegalBlockSizeException; 11 import javax.crypto.KeyGenerator; 12 import javax.crypto.NoSuchPaddingException; 13 import javax.crypto.SecretKey; 14 import javax.crypto.spec.SecretKeySpec; 15 16 public class DecriptTest { 17 18 public static String SHA1(String decript) { 19 try { 20 MessageDigest digest = java.security.MessageDigest 21 .getInstance("SHA-1"); 22 digest.update(decript.getBytes()); 23 byte messageDigest[] = digest.digest(); 24 // Create Hex String 25 StringBuffer hexString = new StringBuffer(); 26 // 字节数组转换为 十六进制 数 27 for (int i = 0; i < messageDigest.length; i++) { 28 String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); 29 if (shaHex.length() < 2) { 30 hexString.append(0); 31 } 32 hexString.append(shaHex); 33 } 34 return hexString.toString(); 35 36 } catch (NoSuchAlgorithmException e) { 37 e.printStackTrace(); 38 } 39 return ""; 40 } 41 42 public static String SHA(String decript) { 43 try { 44 MessageDigest digest = java.security.MessageDigest 45 .getInstance("SHA"); 46 digest.update(decript.getBytes()); 47 byte messageDigest[] = digest.digest(); 48 // Create Hex String 49 StringBuffer hexString = new StringBuffer(); 50 // 字节数组转换为 十六进制 数 51 for (int i = 0; i < messageDigest.length; i++) { 52 String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); 53 if (shaHex.length() < 2) { 54 hexString.append(0); 55 } 56 hexString.append(shaHex); 57 } 58 return hexString.toString(); 59 60 } catch (NoSuchAlgorithmException e) { 61 e.printStackTrace(); 62 } 63 return ""; 64 } 65 66 public static String MD5(String input) { 67 try { 68 // 获得MD5摘要算法的 MessageDigest 对象 69 MessageDigest mdInst = MessageDigest.getInstance("MD5"); 70 // 使用指定的字节更新摘要 71 mdInst.update(input.getBytes()); 72 // 获得密文 73 byte[] md = mdInst.digest(); 74 // 把密文转换成十六进制的字符串形式 75 StringBuffer hexString = new StringBuffer(); 76 // 字节数组转换为 十六进制 数 77 for (int i = 0; i < md.length; i++) { 78 String shaHex = Integer.toHexString(md[i] & 0xFF); 79 if (shaHex.length() < 2) { 80 hexString.append(0); 81 } 82 hexString.append(shaHex); 83 } 84 return hexString.toString(); 85 } catch (NoSuchAlgorithmException e) { 86 e.printStackTrace(); 87 } 88 return ""; 89 } 90 91 /** 92 * 加密 93 * 94 * @param content 95 * 需要加密的内容 96 * @param password 97 * 加密密码 98 * @return 99 */ 100 public static byte[] encryptAES(String content, String password) { 101 try { 102 KeyGenerator kgen = KeyGenerator.getInstance("AES"); 103 kgen.init(128, new SecureRandom(password.getBytes())); 104 SecretKey secretKey = kgen.generateKey(); 105 byte[] enCodeFormat = secretKey.getEncoded(); 106 SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); 107 Cipher cipher = Cipher.getInstance("AES");// 创建密码器 108 byte[] byteContent = content.getBytes("utf-8"); 109 cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 110 byte[] result = cipher.doFinal(byteContent); 111 return result; // 加密 112 } catch (NoSuchAlgorithmException e) { 113 e.printStackTrace(); 114 } catch (NoSuchPaddingException e) { 115 e.printStackTrace(); 116 } catch (InvalidKeyException e) { 117 e.printStackTrace(); 118 } catch (UnsupportedEncodingException e) { 119 e.printStackTrace(); 120 } catch (IllegalBlockSizeException e) { 121 e.printStackTrace(); 122 } catch (BadPaddingException e) { 123 e.printStackTrace(); 124 } 125 return null; 126 } 127 128 /** 129 * 解密 130 * 131 * @param content 132 * 待解密内容 133 * @param password 134 * 解密密钥 135 * @return 136 */ 137 public static byte[] decryptAES(byte[] content, String password) { 138 try { 139 KeyGenerator kgen = KeyGenerator.getInstance("AES"); 140 kgen.init(128, new SecureRandom(password.getBytes())); 141 SecretKey secretKey = kgen.generateKey(); 142 byte[] enCodeFormat = secretKey.getEncoded(); 143 SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); 144 Cipher cipher = Cipher.getInstance("AES");// 创建密码器 145 cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 146 byte[] result = cipher.doFinal(content); 147 return result; // 加密 148 } catch (NoSuchAlgorithmException e) { 149 e.printStackTrace(); 150 } catch (NoSuchPaddingException e) { 151 e.printStackTrace(); 152 } catch (InvalidKeyException e) { 153 e.printStackTrace(); 154 } catch (IllegalBlockSizeException e) { 155 e.printStackTrace(); 156 } catch (BadPaddingException e) { 157 e.printStackTrace(); 158 } 159 return null; 160 } 161 162 /** 163 * BASE64解密 164 * 165 * @param key 166 * @return 167 * @throws Exception 168 */ 169 public static String decryptBASE64(String key) { 170 171 return ""; 172 } 173 174 /** 175 * BASE64加密 176 * 177 * @param key 178 * @return 179 * @throws Exception 180 */ 181 public static String encryptBASE64(String key) { 182 183 return ""; 184 } 185 }
原文链接:http://www.open-open.com/lib/view/open1392185662160.html
Java Base64、AES、SHA1、MD5加密算法(转载)
标签:
原文地址:http://www.cnblogs.com/LEARN4J/p/5413802.html