Java加密技术

关键字: des, 对称加密算法

    接下来我们介绍对称加密算法,最常用的莫过于DES数据加密算法。
DES
DES-Data Encryption Standard,即数据加密算法。是IBM公司于1975年研究成功并公开发表的。DES算法的入口参数有三个:Key、Data、Mode。其中 Key为8个字节共64位,是DES算法的工作密钥;Data也为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密 或解密。
DES算法把64位的明文输入块变为64位的密文输出块,它所使用的密钥也是64位。
通过java代码实现如下:
Java代码
  1. import java.security.Key;  
  2.   
  3. import javax.crypto.Cipher;  
  4. import javax.crypto.SecretKey;  
  5. import javax.crypto.SecretKeyFactory;  
  6. import javax.crypto.spec.DESKeySpec;  
  7.   
  8. /**
  9. * DES安全编码组件
  10. *
  11. * @author 梁栋
  12. * @version 1.0
  13. * @since 1.0
  14. */  
  15. public abstract class DESCoder extends Coder {  
  16.     public static final String ALGORITHM = "DES";  
  17.   
  18.     /**
  19.       * 取得密钥
  20.       *
  21.       * @param key
  22.       * @return
  23.       * @throws Exception
  24.       */  
  25.     public static byte[] getKey(byte[] key) throws Exception {  
  26.         return encryptSHA(encryptMD5(key));  
  27.      }  
  28.   
  29.     /**
  30.       * 加密
  31.       *
  32.       * @param data
  33.       * @param key
  34.       * @return
  35.       * @throws Exception
  36.       */  
  37.     public static byte[] encode(byte[] data, byte[] key) throws Exception {  
  38.          DESKeySpec dks = new DESKeySpec(key);  
  39.          SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);  
  40.   
  41.          SecretKey secretKey = keyFactory.generateSecret(dks);  
  42.   
  43.         return encode(data, secretKey);  
  44.      }  
  45.   
  46.     /**
  47.       * 加密
  48.       *
  49.       * @param data
  50.       * @param key
  51.       * @return
  52.       * @throws Exception
  53.       */  
  54.     public static byte[] encode(byte[] data, Key key) throws Exception {  
  55.          Cipher cipher = Cipher.getInstance(ALGORITHM);  
  56.          cipher.init(Cipher.ENCRYPT_MODE, key);  
  57.   
  58.         return cipher.doFinal(data);  
  59.      }  
  60.   
  61.     /**
  62.       * 解密
  63.       *
  64.       * @param str
  65.       * @param key
  66.       * @return
  67.       * @throws Exception
  68.       */  
  69.     public static byte[] decode(byte[] data, byte[] key) throws Exception {  
  70.          DESKeySpec dks = new DESKeySpec(key);  
  71.   
  72.          SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);  
  73.          SecretKey secretKey = keyFactory.generateSecret(dks);  
  74.   
  75.         return decode(data, secretKey);  
  76.      }  
  77.   
  78.     /**
  79.       * 解密
  80.       *
  81.       * @param data
  82.       * @param key
  83.       * @return
  84.       * @throws Exception
  85.       */  
  86.     public static byte[] decode(byte[] data, Key key) throws Exception {  
  87.          Cipher cipher = Cipher.getInstance(ALGORITHM);  
  88.          cipher.init(Cipher.DECRYPT_MODE, key);  
  89.   
  90.         return cipher.doFinal(data);  
  91.      }  
  92. }  
import java.security.Key;  import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec;  /**  * DES安全编码组件  *   * @author 梁栋  * @version 1.0  * @since 1.0  */ public abstract class DESCoder extends Coder {  public static final String ALGORITHM = "DES";   /**   * 取得密钥   *    * @param key   * @return   * @throws Exception   */  public static byte[] getKey(byte[] key) throws Exception {   return encryptSHA(encryptMD5(key));  }   /**   * 加密   *    * @param data   * @param key   * @return   * @throws Exception   */  public static byte[] encode(byte[] data, byte[] key) throws Exception {   DESKeySpec dks = new DESKeySpec(key);   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);    SecretKey secretKey = keyFactory.generateSecret(dks);    return encode(data, secretKey);  }   /**   * 加密   *    * @param data   * @param key   * @return   * @throws Exception   */  public static byte[] encode(byte[] data, Key key) throws Exception {   Cipher cipher = Cipher.getInstance(ALGORITHM);   cipher.init(Cipher.ENCRYPT_MODE, key);    return cipher.doFinal(data);  }   /**   * 解密   *    * @param str   * @param key   * @return   * @throws Exception   */  public static byte[] decode(byte[] data, byte[] key) throws Exception {   DESKeySpec dks = new DESKeySpec(key);    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);   SecretKey secretKey = keyFactory.generateSecret(dks);    return decode(data, secretKey);  }   /**   * 解密   *    * @param data   * @param key   * @return   * @throws Exception   */  public static byte[] decode(byte[] data, Key key) throws Exception {   Cipher cipher = Cipher.getInstance(ALGORITHM);   cipher.init(Cipher.DECRYPT_MODE, key);    return cipher.doFinal(data);  } }

延续上一个类的实现,我们通过MD5以及SHA对字符串加密生成密钥,这是比较常见的密钥生成方式。
再给出一个测试类:
Java代码
  1. import static org.junit.Assert.*;  
  2.   
  3. import org.junit.Test;  
  4.   
  5. /**
  6. *
  7. * @author 梁栋
  8. * @version 1.0
  9. * @since 1.0
  10. */  
  11. public class DESCoderTest {  
  12.   
  13.     @Test  
  14.     public void test() {  
  15.         try {  
  16.              String inputStr = "DES";  
  17.              String key = "中文";  
  18.             byte[] keyBytes = DESCoder.getKey(key.getBytes());  
  19.              System.err.println("原文:\t" + inputStr);  
  20.   
  21.              System.err.println("密钥:\t" + new String(keyBytes));  
  22.   
  23.             byte[] inputData = inputStr.getBytes();  
  24.              inputData = DESCoder.encode(inputData, keyBytes);  
  25.               
  26.              System.err.println("加密后:\t" + new String(inputData));  
  27.               
  28.             byte[] outputData = DESCoder.decode(inputData, keyBytes);  
  29.              String outputStr = new String(outputData);  
  30.               
  31.              System.err.println("解密后:\t" + outputStr);  
  32.               
  33.              assertEquals(inputStr, outputStr);  
  34.          } catch (Exception e) {  
  35.              e.printStackTrace();  
  36.          }  
  37.      }  
  38. }  
import static org.junit.Assert.*;  import org.junit.Test;  /**  *   * @author 梁栋  * @version 1.0  * @since 1.0  */ public class DESCoderTest {   @Test  public void test() {   try {    String inputStr = "DES";    String key = "中文";    byte[] keyBytes = DESCoder.getKey(key.getBytes());    System.err.println("原文:\t" + inputStr);     System.err.println("密钥:\t" + new String(keyBytes));     byte[] inputData = inputStr.getBytes();    inputData = DESCoder.encode(inputData, keyBytes);        System.err.println("加密后:\t" + new String(inputData));        byte[] outputData = DESCoder.decode(inputData, keyBytes);    String outputStr = new String(outputData);        System.err.println("解密后:\t" + outputStr);        assertEquals(inputStr, outputStr);   } catch (Exception e) {    e.printStackTrace();   }  } }

得到的输出内容如下:
Console代码
  1. 原文: DES  
  2. 密钥: .+ g M[ =   
  3. g  
  4. 加密后:     Yg)w   
  5. 解密后:     DES  
原文: DES 密钥:     .+ g M[   =    g 加密后:    Yg)w  解密后: DES

    由控制台得到的输出,我们能够比对加密、解密后结果一致。这是一种简单的加密解密方式,只有一个密钥。
    其实DES有很多同胞兄弟,如DESede(TripleDES)、AES、Blowfish、RC2、RC4(ARCFOUR)。这里就不过多阐述了,大同小异,只要换掉ALGORITHM换成对应的值就可以了,此外就是密钥长度不同了。

Java代码
  1. /**
  2. * DES           key size must be equal to 56
  3. * DESede(TripleDES) key size must be equal to 112 or 168
  4. * AES           key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available
  5. * Blowfish      key size must be multiple of 8, and can only range from 32 to 448 (inclusive)
  6. * RC2           key size must be between 40 and 1024 bits
  7. * RC4(ARCFOUR) key size must be between 40 and 1024 bits
  8. **/  
/**  * DES          key size must be equal to 56  * DESede(TripleDES) key size must be equal to 112 or 168  * AES          key size must be equal to 128, 192 or 256,but 192 and 256 bits may not be available  * Blowfish     key size must be multiple of 8, and can only range from 32 to 448 (inclusive)  * RC2          key size must be between 40 and 1024 bits  * RC4(ARCFOUR) key size must be between 40 and 1024 bits  **/

你可能感兴趣的:(java加密)