《md5、aes、des加密算法》由会员分享,可在线阅读,更多相关《md5、aes、des加密算法(7页珍藏版)》请在金锄头文库上搜索。
1、MD5:用于计算出一段不可逆向计算的数值,以此来验证该 文件是否被修改的. 它可以帮你验证从网上下载下来的 windows7 安装程序是否与发布人发布的东西完全一致,也就是帮助你验证这个程序有没有经过他人(非发布人)的修改。/* * MD5 加密算法 * author tfq * datetime 2011-10-13 */ public class MD5Util /* * MD5 值*/ public static String encryptMD5(String inStr) MessageDigest md5 = null; try md5 = MessageDigest.getIns
2、tance(MD5); catch (Exception e) System.out.println(e.toString(); e.printStackTrace(); return ; char charArray = inStr.toCharArray(); byte byteArray = new bytecharArray.length; for (int i = 0; i charArray.length; i+) byteArrayi = (byte) charArrayi; byte md5Bytes = md5.digest(byteArray); StringBuffer
3、hexValue = new StringBuffer(); for (int i = 0; i md5Bytes.length; i+) int val = (int) md5Bytesi) if (val 16) hexValue.append(0); hexValue.append(Integer.toHexString(val); return hexValue.toString(); /* * MD5 加密 */ public static String decryptMD5(String inStr) char a = inStr.toCharArray(); for (int i
4、 = 0; i a.length; i+) /按位异或ai = (char) (ai t); String s = new String(a); return s; / 可逆的 MD5 加密public static void main(String args) String s = new String(zhonghuatengfei); System.out.println(加密前: + s); /生成 MD5 值String encryptResult = encryptMD5(s);System.out.println(MD5 后: + encryptResult); /加密Strin
5、g decryptResult = decryptMD5(encryptResult);System.out.println(MD5 后加密: + decryptResult); /解密String decryptResult2 = decryptMD5(decryptResult);System.out.println(解密为 MD5 后的: + decryptResult2); AES: 高级加密标准( 英语:Advanced Encryption Standard, 缩写:AES),在密码学中又称 Rijndael 加密法,是 美国联邦政府采用的一种 区块加密标准。这个标准用来替代原先的
6、 DES,已经被多方分析且广 为全世界所使用。 经过五年的甄选流程,高级加密标准由 美国国家标准与技术研究院(NIST)于 2001 年 11 月 26 日发布于 FIPS PUB 197,并在 2002 年 5 月 26 日成为有效的标准。2006 年,高级加密标准已然成为对称密钥加密中最流行的算法之一。public class TestAES /* AES 加密 * param content* param password * return*/public static byte encrypt(String content, String password) try KeyGenera
7、tor kgen = KeyGenerator.getInstance(AES); kgen.init(128, new SecureRandom(password.getBytes(); SecretKey secretKey = kgen.generateKey(); byte enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES); Cipher cipher = Cipher.getInstance(AES); byte byteContent = c
8、ontent.getBytes(utf-8); cipher.init(Cipher.ENCRYPT_MODE, key); byte result = cipher.doFinal(byteContent); return result; catch (NoSuchAlgorithmException e) e.printStackTrace(); catch (NoSuchPaddingException e) e.printStackTrace(); catch (InvalidKeyException e) e.printStackTrace(); catch (Unsupported
9、EncodingException e) e.printStackTrace(); catch (IllegalBlockSizeException e) e.printStackTrace(); catch (BadPaddingException e) e.printStackTrace(); return null; /*解密* param content * param password * return */ public static byte decrypt(byte content, String password) try KeyGenerator kgen = KeyGen
10、erator.getInstance(AES); kgen.init(128, new SecureRandom(password.getBytes(); SecretKey secretKey = kgen.generateKey(); byte enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES); Cipher cipher = Cipher.getInstance(AES); cipher.init(Cipher.DECRYPT_MODE, key)
11、; byte result = cipher.doFinal(content); return result; catch (NoSuchAlgorithmException e) e.printStackTrace(); catch (NoSuchPaddingException e) e.printStackTrace(); catch (InvalidKeyException e) e.printStackTrace(); catch (IllegalBlockSizeException e) e.printStackTrace(); catch (BadPaddingException
12、 e) e.printStackTrace(); return null; /*加密后转化成十六进制 * param buf * return */ public static String parseByte2HexStr(byte buf) StringBuffer sb = new StringBuffer(); for (int i = 0; i buf.length; i+) String hex = Integer.toHexString(bufi if (hex.length() = 1) hex = 0 + hex; sb.append(hex.toUpperCase(); r
13、eturn sb.toString(); /*十六进制转换成 bytes * param hexStr * return */ public static byte parseHexStr2Byte(String hexStr) if (hexStr.length() 1) return null; byte result = new bytehexStr.length()/2; for (int i = 0;i hexStr.length()/2; i+) int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16); int l
14、ow = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16); resulti = (byte) (high * 16 + low); return result; public static void main(String args) String content = pass1234; /密码String password = Huateng.gd.Oceans Fourteen.DWMNTH2CJFLCWL; System.out.println(原文: + content); byte encryptResult = encryp
15、t(content, password); String encryptResultStr = parseByte2HexStr(encryptResult); System.out.println(加密后: + encryptResultStr); byte decryptFrom = parseHexStr2Byte(encryptResultStr); byte decryptResult = decrypt(decryptFrom,password); System.out.println(解密后: + new String(decryptResult); DES: 现在已经不是一种安全的加密方法,主要因为它