java 中的 加密

MD5加密(不能解密)

[java] view plain copy
  1. public class Encrypter  
  2. //   default algorithm is MD5  
  3.     public static String encrypt(String message) throws Exception  
  4.         return encrypt(message, "MD5");  
  5.      
  6.   
  7.     // algorithm: MD5 or SHA-1  
  8.     // return string length: 32 if algorithm MD5, or 40 if algorithm SHA-1  
  9.     public static String encrypt(String message, String algorithm)  
  10.             throws Exception  
  11.         if (message == null)  
  12. //          throw new Exception("message is null.");  
  13.             message "";  
  14.          
  15.         if (!"MD5".equals(algorithm) && !"SHA-1".equals(algorithm))  
  16.             throw new Exception("algorithm must be MD5 or SHA-1.");  
  17.          
  18.         byte[] buffer message.getBytes();  
  19.   
  20.         // The SHA algorithm results in 20-byte digest, while MD5 is 16 bytes  
  21.         // long.  
  22.         MessageDigest md MessageDigest.getInstance(algorithm);  
  23.   
  24.         // Ensure the digest's buffer is empty. This isn't necessary the first  
  25.         // time used.  
  26.         // However, it is good practice to always empty the buffer out in case  
  27.         // you later reuse it.  
  28.         md.reset();  
  29.   
  30.         // Fill the digest's buffer with data to compute message digest from.  
  31.         md.update(buffer);  
  32.   
  33.         // Generate the digest. This does any necessary padding required by the  
  34.         // algorithm.  
  35.         byte[] digest md.digest();  
  36.   
  37.         // Save or print digest bytes. Integer.toHexString() doesn't print  
  38.         // leading zeros.  
  39.         StringBuffer hexString new StringBuffer();  
  40.         String sHexBit null;  
  41.         for (int 0; digest.length; i++)  
  42.             sHexBit Integer.toHexString(0xFF digest[i]);  
  43.             if (sHexBit.length() == 1)  
  44.                 sHexBit "0" sHexBit;  
  45.              
  46.             hexString.append(sHexBit);  
  47.          
  48.         return hexString.toString();  
  49.      
  50.   
  51.     public static void main(String[] args) throws Exception  
  52.         System.out.println(Encrypter.encrypt("123456"));  
  53.           
  54.      


DES加密

[java] view plain copy
  1. public class CryptUtil  
  2.   
  3.     private static final String PASSWORD_CRYPT_KEY "__jDlog_";  
  4.   
  5.     private static final String DES "DES";  
  6.   
  7.     public byte[] encrypt(byte[] src, byte[] key) throws Exception  
  8.         SecureRandom sr new SecureRandom();  
  9.         DESKeySpec dks new DESKeySpec(key);  
  10.         SecretKeyFactory keyFactory SecretKeyFactory.getInstance(DES);  
  11.         SecretKey securekey keyFactory.generateSecret(dks);  
  12.         Cipher cipher Cipher.getInstance(DES);  
  13.         cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);  
  14.         return cipher.doFinal(src);  
  15.      
  16.   
  17.     public byte[] decrypt(byte[] src, byte[] key) throws Exception  
  18.         SecureRandom sr new SecureRandom();  
  19.         DESKeySpec dks new DESKeySpec(key);  
  20.         SecretKeyFactory keyFactory SecretKeyFactory.getInstance(DES);  
  21.         SecretKey securekey keyFactory.generateSecret(dks);  
  22.         Cipher cipher Cipher.getInstance(DES);  
  23.         cipher.init(Cipher.DECRYPT_MODE, securekey, sr);  
  24.         return cipher.doFinal(src);  
  25.      
  26.   
  27.       
  28.     public final String decrypt(String data)  
  29.         try  
  30.             return new String(decrypt(hex2byte(data.getBytes()),  
  31.                     PASSWORD_CRYPT_KEY.getBytes()));  
  32.         catch (Exception e)  
  33.          
  34.         return null;  
  35.      
  36.   
  37.       
  38.     public final String encrypt(String password)  
  39.         try  
  40.             return byte2hex(encrypt(password.getBytes(), PASSWORD_CRYPT_KEY  
  41.                     .getBytes()));  
  42.         catch (Exception e)  
  43.          
  44.         return null;  
  45.      
  46.   
  47.     public String byte2hex(byte[] b)  
  48.         StringBuffer hs new StringBuffer();  
  49.         String stmp "";  
  50.         for (int 0; b.length; n++)  
  51.             stmp (java.lang.Integer.toHexString(b[n] 0XFF));  
  52.             if (stmp.length() == 1)  
  53.                 hs.append("0").append(stmp);  
  54.             else  
  55.                 hs.append(stmp);  
  56.          
  57.         return hs.toString().toUpperCase();  
  58.      
  59.   
  60.     public byte[] hex2byte(byte[] b)  
  61.         if ((b.length 2) != 0)  
  62.             throw new IllegalArgumentException("The length is not an even.");  
  63.         byte[] b2 new byte[b.length 2];  
  64.         for (int 0; b.length; += 2)  
  65.             String item new String(b, n, 2);  
  66.             b2[n 2] (byte) Integer.parseInt(item, 16);  
  67.          
  68.         return b2;  
  69.      
  70.     public static void main(String[] args)  
  71.         String pwd="123456";  
  72.         CryptUtil u=new CryptUtil();  
  73.         System.out.println(u.encrypt(pwd));  
  74.         System.out.println(u.decrypt("619034920555A7F3"));  
  75.      
  76. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值