在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java加密解密DESUtil、TripleDESUtil

2016-12-20 13:16| 发布者: zhangjf| 查看: 596| 评论: 0

摘要: 代码 import java.security.Key;import java.security.SecureRandom;import java.util.Arrays;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.spec.SecretKeySpec;public class ...
[Java]代码      
  1. import java.security.Key;
  2. import java.security.SecureRandom;
  3. import java.util.Arrays;
  4. import javax.crypto.Cipher;
  5. import javax.crypto.KeyGenerator;
  6. import javax.crypto.spec.SecretKeySpec;
  7. public class DESUtil {
  8.         /*
  9.          *
  10.          * 注: 1、DES使用56位密钥,以现代计算能力,24小时内即可被破解;
  11.          * 2、3DES(即Triple DES)是DES向AES过渡的加密算法(1999年,NIST将3-DES指定为过渡的加密标准)。使用3条56位的密钥对 数据进行三次加密。
  12.          * 3、DES算法的加密密钥是根据用户输入的密码生成的,该算法把64位密码中的第8位、第16位、第24位、第32位、第40位、第48位、第56位、第64位作为奇偶校验位,在计算密钥时要忽略这8位.
  13.          * 在generateKey()生成的随机密钥为8位(64bit)
  14.          */
  15.         public static final String ALGORITHM = "DES";
  16.         public static final String TRANSFORMATION = "DES/ECB/PKCS5Padding";
  17.         /**
  18.          * 生成随机密钥
  19.          *
  20.          * @return
  21.          * @throws Exception
  22.          */
  23.         public static Key generateKey() throws Exception {
  24.                 KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
  25.                 keyGenerator.init(new SecureRandom());
  26.                 Key key = keyGenerator.generateKey();
  27.                 return key;
  28.         }
  29.         /**
  30.          * 生成固定密钥
  31.          *
  32.          * @param seed
  33.          * @return
  34.          * @throws Exception
  35.          */
  36.         public static Key generateKey(byte[] seed) throws Exception {
  37.                 KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
  38.                 keyGenerator.init(new SecureRandom(seed));
  39.                 Key key = keyGenerator.generateKey();
  40.                 return key;
  41.         }
  42.        
  43.         /**
  44.          * 生成固定密钥
  45.          *
  46.          * @param password
  47.          * @return
  48.          * @throws Exception
  49.          */
  50.         public static Key generateKey(String password) throws Exception {
  51.                 return generateKey(password.getBytes());
  52.         }
  53.         /**
  54.          * 执行加密
  55.          *
  56.          * @param content
  57.          * @param key                长度必须为8位,即64bit
  58.          * @return
  59.          * @throws Exception
  60.          */
  61.         public static byte[] encrypt(byte[] content, byte[] key) throws Exception {
  62.                 Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  63.                 cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, ALGORITHM));
  64.                 byte[] output = cipher.doFinal(content);
  65.                 return output;
  66.         }
  67.        
  68.         /**
  69.          * 执行加密
  70.          *
  71.          * @param content
  72.          * @param password
  73.          * @return
  74.          * @throws Exception
  75.          */
  76.         public static byte[] encrypt(byte[] content, String password) throws Exception {
  77.                 Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  78.                 cipher.init(Cipher.ENCRYPT_MODE, generateKey(password));
  79.                 byte[] output = cipher.doFinal(content);
  80.                 return output;
  81.         }
  82.         /**
  83.          * 执行解密
  84.          *
  85.          * @param content
  86.          * @param key                长度必须为8位,即64bit
  87.          * @return
  88.          * @throws Exception
  89.          */
  90.         public static byte[] decrypt(byte[] content, byte[] key) throws Exception {
  91.                 Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  92.                 cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, ALGORITHM));
  93.                 byte[] output = cipher.doFinal(content);
  94.                 return output;
  95.         }
  96.         /**
  97.          * 执行解密
  98.          *
  99.          * @param content
  100.          * @param password
  101.          * @return
  102.          * @throws Exception
  103.          */
  104.         public static byte[] decrypt(byte[] content, String password) throws Exception {
  105.                 Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  106.                 cipher.init(Cipher.DECRYPT_MODE, generateKey(password));
  107.                 byte[] output = cipher.doFinal(content);
  108.                 return output;
  109.         }
  110.        
  111.         public static void main(String[] args) throws Exception {
  112.                 System.out.println(Arrays.toString(encrypt("DES使用56位密钥,以现代计算能力".getBytes(), "012345")));
  113.                 System.out.println(new String(decrypt(encrypt("DES使用56位密钥,以现代计算能力".getBytes(), "012345"), "012345")));
  114.                 System.out.println(Arrays.toString(encrypt("DES使用56位密钥,以现代计算能力".getBytes(), "01234567".getBytes())));
  115.                 System.out.println(new String(decrypt(encrypt("DES使用56位密钥,以现代计算能力".getBytes(), "01234567".getBytes()), "01234567".getBytes())));
  116.                
  117.                 /*
  118.                  * 控制台输出:
  119.                  *
  120.                  * [117, -109, -80, -75, 51, -86, -57, -109, -94, 58, 38, 94, 39, -107, -60, 65, -122, -7, -124, 2, -23, -30, -98, -64, 90, 26, 15, 82, 84, 102, -108, -3, -68, -4, 110, -86, -106, 19, -65, -110, 2, 15, 49, -79, -98, 38, -39, 6]
  121.                  * DES使用56位密钥,以现代计算能力
  122.                  * [-6, 63, -15, 73, -28, 85, 125, 64, 6, 55, -63, 99, 114, 21, 108, -49, 19, 11, -15, -126, 36, -92, 62, 112, -40, 64, -102, 127, -94, -53, -89, 33, 72, -20, -126, -90, 105, -37, -68, -46, -61, -36, 5, -103, 27, 32, 84, 28]
  123.                  * DES使用56位密钥,以现代计算能力
  124.                  */
  125.         }
  126. }
复制代码

[Java]代码      
  1. import java.security.Key;
  2. import java.security.SecureRandom;
  3. import java.util.Arrays;
  4. import javax.crypto.Cipher;
  5. import javax.crypto.KeyGenerator;
  6. import javax.crypto.spec.SecretKeySpec;
  7. public class TripleDESUtil {
  8.         public static final String ALGORITHM = "DESede";
  9.         public static final String TRANSFORMATION = "DESede/ECB/PKCS5Padding";
  10.         /**
  11.          * 生成随机密钥
  12.          *
  13.          * @return
  14.          * @throws Exception
  15.          */
  16.         public static Key generateKey() throws Exception {
  17.                 KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
  18.                 keyGenerator.init(new SecureRandom());
  19.                 Key key = keyGenerator.generateKey();
  20.                 return key;
  21.         }
  22.         /**
  23.          * 生成固定密钥
  24.          *
  25.          * @param seed
  26.          * @return
  27.          * @throws Exception
  28.          */
  29.         public static Key generateKey(byte[] seed) throws Exception {
  30.                 KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
  31.                 keyGenerator.init(new SecureRandom(seed));
  32.                 Key key = keyGenerator.generateKey();
  33.                 return key;
  34.         }
  35.        
  36.         /**
  37.          * 生成固定密钥
  38.          *
  39.          * @param password
  40.          * @return
  41.          * @throws Exception
  42.          */
  43.         public static Key generateKey(String password) throws Exception {
  44.                 return generateKey(password.getBytes());
  45.         }
  46.         /**
  47.          * 执行加密
  48.          *
  49.          * @param content
  50.          * @param key                长度必须为8位,即64bit
  51.          * @return
  52.          * @throws Exception
  53.          */
  54.         public static byte[] encrypt(byte[] content, byte[] key) throws Exception {
  55.                 Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  56.                 cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, ALGORITHM));
  57.                 byte[] output = cipher.doFinal(content);
  58.                 return output;
  59.         }
  60.        
  61.         /**
  62.          * 执行加密
  63.          *
  64.          * @param content
  65.          * @param password
  66.          * @return
  67.          * @throws Exception
  68.          */
  69.         public static byte[] encrypt(byte[] content, String password) throws Exception {
  70.                 Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  71.                 cipher.init(Cipher.ENCRYPT_MODE, generateKey(password));
  72.                 byte[] output = cipher.doFinal(content);
  73.                 return output;
  74.         }
  75.         /**
  76.          * 执行解密
  77.          *
  78.          * @param content
  79.          * @param key                长度必须为8位,即64bit
  80.          * @return
  81.          * @throws Exception
  82.          */
  83.         public static byte[] decrypt(byte[] content, byte[] key) throws Exception {
  84.                 Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  85.                 cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, ALGORITHM));
  86.                 byte[] output = cipher.doFinal(content);
  87.                 return output;
  88.         }
  89.         /**
  90.          * 执行解密
  91.          *
  92.          * @param content
  93.          * @param password
  94.          * @return
  95.          * @throws Exception
  96.          */
  97.         public static byte[] decrypt(byte[] content, String password) throws Exception {
  98.                 Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  99.                 cipher.init(Cipher.DECRYPT_MODE, generateKey(password));
  100.                 byte[] output = cipher.doFinal(content);
  101.                 return output;
  102.         }
  103.        
  104.         public static void main(String[] args) throws Exception {
  105.                 System.out.println(Arrays.toString(encrypt("使用3条56位的密钥对 数据进行三次加密。".getBytes(), "012345")));
  106.                 System.out.println(new String(decrypt(encrypt("使用3条56位的密钥对 数据进行三次加密。".getBytes(), "012345"), "012345")));
  107.                 System.out.println(Arrays.toString(encrypt("使用3条56位的密钥对 数据进行三次加密。".getBytes(), "012345670123456701234567".getBytes())));
  108.                 System.out.println(new String(decrypt(encrypt("使用3条56位的密钥对 数据进行三次加密。".getBytes(), "012345670123456701234567".getBytes()), "012345670123456701234567".getBytes())));
  109.                
  110.                 /*
  111.                  * 控制台输出:
  112.                  *
  113.                  * [-46, 78, 30, 115, 9, -79, -59, 46, 8, 46, -1, -92, -53, -42, -86, 15, -8, -25, -18, 92, 100, -109, 68, -9, -42, 80, 42, 60, -62, -43, 41, 84, -114, 52, 92, -115, -92, 16, -15, 3, 36, -105, 69, 118, -126, 61, 81, 121, -99, -89, -67, 91, 70, 19, 85, 9]
  114.                  * 使用3条56位的密钥对 数据进行三次加密。
  115.                  * [38, 108, 104, -124, 124, -73, -66, -121, -43, -41, -102, 74, -71, -98, 71, 4, 7, -50, 78, -28, 39, -103, 115, -93, -88, -107, -113, 89, -41, 55, -93, 111, -43, -120, -47, -50, -2, -104, 107, 105, 114, 45, 120, 40, 103, 64, 19, 60, 37, 18, 100, 71, 106, -66, 123, -66]
  116.                  * 使用3条56位的密钥对 数据进行三次加密。
  117.                  */
  118.         }
  119. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 17:39

Copyright 2015-2025 djqfx

返回顶部