Java中的各种加密算法

 JAVA中为我们提供了丰富的加密技术,可以基本的分为单向加密和非对称加密

 1.单向加密算法

 单向加密算法主要用来验证数据传输的过程中,是否被篡改过。

  • BASE64 严格地说,属于编码格式,而非加密算法

  • MD5(Message Digest algorithm 5,信息摘要算法)

  • SHA(Secure Hash Algorithm,安全散列算法)

  • HMAC(Hash Message Authentication Code,散列消息鉴别码

 2.对称和非对称加密算法
   对称和非对称加密算法主要采用公钥和私钥的形式,来对数据加密。
  • DES(Data Encryption Standard,数据加密算法)

  • PBE(Password-based encryption,基于密码验证)

  • RSA(算法的名字以发明者的名字命名:Ron Rivest, AdiShamir 和Leonard Adleman)

  • DH(Diffie-Hellman算法,密钥一致协议)

  • DSA(Digital Signature Algorithm,数字签名)

  • ECC(Elliptic Curves Cryptography,椭圆曲线密码编码学)


  更多理论的简介,请自行查阅,下面提供代码,来看一下。

  基础加密
 
[java]  view plain  copy
  1. <span style="font-family:Comic Sans MS;font-size:18px;">package com.test;  
  2. import java.security.MessageDigest;  
  3.   
  4. import javax.crypto.KeyGenerator;  
  5. import javax.crypto.Mac;  
  6. import javax.crypto.SecretKey;  
  7. import javax.crypto.spec.SecretKeySpec;  
  8.   
  9. import sun.misc.BASE64Decoder;  
  10. import sun.misc.BASE64Encoder;  
  11.   
  12. /**   
  13.  * @ClassName: coder    
  14.  * @Description: 加密组件 
  15.  * @author: LUCKY   
  16.  * @date:2016年1月4日 下午1:24:12      
  17.  */   
  18. public abstract class coder {  
  19.     public static final String KEY_SHA = "SHA";  
  20.     public static final String KEY_MD5 = "MD5";  
  21.    
  22.     /** 
  23.      * MAC算法可选以下多种算法 
  24.      *  
  25.      * <pre> 
  26.      * HmacMD5  
  27.      * HmacSHA1  
  28.      * HmacSHA256  
  29.      * HmacSHA384  
  30.      * HmacSHA512 
  31.      * </pre> 
  32.      */  
  33.     public static final String KEY_MAC = "HmacMD5";  
  34.    
  35.     /** 
  36.      * BASE64解密 
  37.      *  
  38.      * @param key 
  39.      * @return 
  40.      * @throws Exception 
  41.      */  
  42.     public static byte[] decryptBASE64(String key) throws Exception {  
  43.         return (new BASE64Decoder()).decodeBuffer(key);  
  44.     }  
  45.    
  46.     /** 
  47.      * BASE64加密 
  48.      *  
  49.      * @param key 
  50.      * @return 
  51.      * @throws Exception 
  52.      */  
  53.     public static String encryptBASE64(byte[] key) throws Exception {  
  54.         return (new BASE64Encoder()).encodeBuffer(key);  
  55.     }  
  56.    
  57.     /** 
  58.      * MD5加密 
  59.      *  
  60.      * @param data 
  61.      * @return 
  62.      * @throws Exception 
  63.      */  
  64.     public static byte[] encryptMD5(byte[] data) throws Exception {  
  65.    
  66.         MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);  
  67.         md5.update(data);  
  68.    
  69.         return md5.digest();  
  70.    
  71.     }  
  72.    
  73.     /** 
  74.      * SHA加密 
  75.      *  
  76.      * @param data 
  77.      * @return 
  78.      * @throws Exception 
  79.      */  
  80.     public static byte[] encryptSHA(byte[] data) throws Exception {  
  81.    
  82.         MessageDigest sha = MessageDigest.getInstance(KEY_SHA);  
  83.         sha.update(data);  
  84.    
  85.         return sha.digest();  
  86.    
  87.     }  
  88.    
  89.     /** 
  90.      * 初始化HMAC密钥 
  91.      *  
  92.      * @return 
  93.      * @throws Exception 
  94.      */  
  95.     public static String initMacKey() throws Exception {  
  96.         KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);  
  97.    
  98.         SecretKey secretKey = keyGenerator.generateKey();  
  99.           
  100.         return encryptBASE64(secretKey.getEncoded());  
  101.     }  
  102.    
  103.     /** 
  104.      * HMAC加密 
  105.      *  
  106.      * @param data 
  107.      * @param key 
  108.      * @return 
  109.      * @throws Exception 
  110.      */  
  111.     public static byte[] encryptHMAC(byte[] data, String key) throws Exception {  
  112.    
  113.         SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);  
  114.         Mac mac = Mac.getInstance(secretKey.getAlgorithm());  
  115.         mac.init(secretKey);  
  116.    
  117.         return mac.doFinal(data);  
  118.    
  119.     }  
  120. }</span>  


RSA安全编码组件
 
[java]  view plain  copy
  1. <span style="font-family:Comic Sans MS;font-size:18px;">package com.test;  
  2. import java.security.Key;  
  3. import java.security.KeyFactory;  
  4. import java.security.KeyPair;  
  5. import java.security.KeyPairGenerator;  
  6. import java.security.PrivateKey;  
  7. import java.security.PublicKey;  
  8. import java.security.Signature;  
  9. import java.security.interfaces.RSAPrivateKey;  
  10. import java.security.interfaces.RSAPublicKey;  
  11. import java.security.spec.PKCS8EncodedKeySpec;  
  12. import java.security.spec.X509EncodedKeySpec;  
  13. import java.util.HashMap;  
  14. import java.util.Map;  
  15.   
  16. import javax.crypto.Cipher;  
  17.    
  18.   
  19. /**   
  20.  * @ClassName: RSACoder    
  21.  * @Description: RSA安全编码组件 
  22.  * @author: LUCKY   
  23.  * @date:2016年1月4日 下午1:25:34      
  24.  */   
  25. public abstract class RSACoder extends coder {  
  26.     public static final String KEY_ALGORITHM = "RSA";  
  27.     public static final String SIGNATURE_ALGORITHM = "MD5withRSA";  
  28.    
  29.     private static final String PUBLIC_KEY = "RSAPublicKey";  
  30.     private static final String PRIVATE_KEY = "RSAPrivateKey";  
  31.    
  32.     /** 
  33.      * 用私钥对信息生成数字签名 
  34.      *  
  35.      * @param data 
  36.      *            加密数据 
  37.      * @param privateKey 
  38.      *            私钥 
  39.      *  
  40.      * @return 
  41.      * @throws Exception 
  42.      */  
  43.     public static String sign(byte[] data, String privateKey) throws Exception {  
  44.         // 解密由base64编码的私钥  
  45.         byte[] keyBytes = decryptBASE64(privateKey);  
  46.    
  47.         // 构造PKCS8EncodedKeySpec对象  
  48.         PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);  
  49.    
  50.         // KEY_ALGORITHM 指定的加密算法  
  51.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);  
  52.    
  53.         // 取私钥匙对象  
  54.         PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);  
  55.    
  56.         // 用私钥对信息生成数字签名  
  57.         Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);  
  58.         signature.initSign(priKey);  
  59.         signature.update(data);  
  60.    
  61.         return encryptBASE64(signature.sign());  
  62.     }  
  63.    
  64.     /** 
  65.      * 校验数字签名 
  66.      *  
  67.      * @param data 
  68.      *            加密数据 
  69.      * @param publicKey 
  70.      *            公钥 
  71.      * @param sign 
  72.      *            数字签名 
  73.      *  
  74.      * @return 校验成功返回true 失败返回false 
  75.      * @throws Exception 
  76.      *  
  77.      */  
  78.     public static boolean verify(byte[] data, String publicKey, String sign)  
  79.             throws Exception {  
  80.    
  81.         // 解密由base64编码的公钥  
  82.         byte[] keyBytes = decryptBASE64(publicKey);  
  83.    
  84.         // 构造X509EncodedKeySpec对象  
  85.         X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);  
  86.    
  87.         // KEY_ALGORITHM 指定的加密算法  
  88.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);  
  89.    
  90.         // 取公钥匙对象  
  91.         PublicKey pubKey = keyFactory.generatePublic(keySpec);  
  92.    
  93.         Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);  
  94.         signature.initVerify(pubKey);  
  95.         signature.update(data);  
  96.    
  97.         // 验证签名是否正常  
  98.         return signature.verify(decryptBASE64(sign));  
  99.     }  
  100.    
  101.     /** 
  102.      * 解密<br> 
  103.      * 用私钥解密 
  104.      *  
  105.      * @param data 
  106.      * @param key 
  107.      * @return 
  108.      * @throws Exception 
  109.      */  
  110.     public static byte[] decryptByPrivateKey(byte[] data, String key)  
  111.             throws Exception {  
  112.         // 对密钥解密  
  113.         byte[] keyBytes = decryptBASE64(key);  
  114.    
  115.         // 取得私钥  
  116.         PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);  
  117.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);  
  118.         Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);  
  119.    
  120.         // 对数据解密  
  121.         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());  
  122.         cipher.init(Cipher.DECRYPT_MODE, privateKey);  
  123.    
  124.         return cipher.doFinal(data);  
  125.     }  
  126.    
  127.     /** 
  128.      * 解密<br> 
  129.      * 用私钥解密 
  130.      *  
  131.      * @param data 
  132.      * @param key 
  133.      * @return 
  134.      * @throws Exception 
  135.      */  
  136.     public static byte[] decryptByPublicKey(byte[] data, String key)  
  137.             throws Exception {  
  138.         // 对密钥解密  
  139.         byte[] keyBytes = decryptBASE64(key);  
  140.    
  141.         // 取得公钥  
  142.         X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);  
  143.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);  
  144.         Key publicKey = keyFactory.generatePublic(x509KeySpec);  
  145.    
  146.         // 对数据解密  
  147.         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());  
  148.         cipher.init(Cipher.DECRYPT_MODE, publicKey);  
  149.    
  150.         return cipher.doFinal(data);  
  151.     }  
  152.    
  153.     /** 
  154.      * 加密<br> 
  155.      * 用公钥加密 
  156.      *  
  157.      * @param data 
  158.      * @param key 
  159.      * @return 
  160.      * @throws Exception 
  161.      */  
  162.     public static byte[] encryptByPublicKey(byte[] data, String key)  
  163.             throws Exception {  
  164.         // 对公钥解密  
  165.         byte[] keyBytes = decryptBASE64(key);  
  166.    
  167.         // 取得公钥  
  168.         X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);  
  169.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);  
  170.         Key publicKey = keyFactory.generatePublic(x509KeySpec);  
  171.    
  172.         // 对数据加密  
  173.         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());  
  174.         cipher.init(Cipher.ENCRYPT_MODE, publicKey);  
  175.    
  176.         return cipher.doFinal(data);  
  177.     }  
  178.    
  179.     /** 
  180.      * 加密<br> 
  181.      * 用私钥加密 
  182.      *  
  183.      * @param data 
  184.      * @param key 
  185.      * @return 
  186.      * @throws Exception 
  187.      */  
  188.     public static byte[] encryptByPrivateKey(byte[] data, String key)  
  189.             throws Exception {  
  190.         // 对密钥解密  
  191.         byte[] keyBytes = decryptBASE64(key);  
  192.    
  193.         // 取得私钥  
  194.         PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);  
  195.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);  
  196.         Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);  
  197.    
  198.         // 对数据加密  
  199.         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());  
  200.         cipher.init(Cipher.ENCRYPT_MODE, privateKey);  
  201.    
  202.         return cipher.doFinal(data);  
  203.     }  
  204.    
  205.     /** 
  206.      * 取得私钥 
  207.      *  
  208.      * @param keyMap 
  209.      * @return 
  210.      * @throws Exception 
  211.      */  
  212.     public static String getPrivateKey(Map<String, Object> keyMap)  
  213.             throws Exception {  
  214.         Key key = (Key) keyMap.get(PRIVATE_KEY);  
  215.    
  216.         return encryptBASE64(key.getEncoded());  
  217.     }  
  218.    
  219.     /** 
  220.      * 取得公钥 
  221.      *  
  222.      * @param keyMap 
  223.      * @return 
  224.      * @throws Exception 
  225.      */  
  226.     public static String getPublicKey(Map<String, Object> keyMap)  
  227.             throws Exception {  
  228.         Key key = (Key) keyMap.get(PUBLIC_KEY);  
  229.    
  230.         return encryptBASE64(key.getEncoded());  
  231.     }  
  232.    
  233.     /** 
  234.      * 初始化密钥 
  235.      *  
  236.      * @return 
  237.      * @throws Exception 
  238.      */  
  239.     public static Map<String, Object> initKey() throws Exception {  
  240.         KeyPairGenerator keyPairGen = KeyPairGenerator  
  241.                 .getInstance(KEY_ALGORITHM);  
  242.         keyPairGen.initialize(1024);  
  243.    
  244.         KeyPair keyPair = keyPairGen.generateKeyPair();  
  245.    
  246.         // 公钥  
  247.         RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();  
  248.    
  249.         // 私钥  
  250.         RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();  
  251.    
  252.         Map<String, Object> keyMap = new HashMap<String, Object>(2);  
  253.    
  254.         keyMap.put(PUBLIC_KEY, publicKey);  
  255.         keyMap.put(PRIVATE_KEY, privateKey);  
  256.         return keyMap;  
  257.     }  
  258. }</span>  

                                 源博主的地址:http://blog.csdn.net/luckyzhoustar/article/details/50455407
  1.                                         
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值