JAVA语言DES算法

来源:互联网 发布:凡事有偶然的凑巧知乎 编辑:程序博客网 时间:2024/06/01 08:43

1、DES算法:

 

 

Java代码  收藏代码
  1. /** 
  2.  * 加解密算法 
  3.  * @param data  加解密数据 
  4.  * @param key   秘钥 
  5.  * @param mode  模式 
  6.  * @return      加解密结果 
  7.  */  
  8. public static byte[] desCryt(byte[] data, byte[] key, int mode){  
  9.     byte[] result = null ;  
  10.     try {  
  11.         SecureRandom sr = new SecureRandom();    
  12.         SecretKeyFactory keyFactory;  
  13.         DESKeySpec dks = new DESKeySpec(key);  
  14.         keyFactory = SecretKeyFactory.getInstance("DES");  
  15.         SecretKey secretkey = keyFactory.generateSecret(dks);   
  16.         //创建Cipher对象  
  17.         Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");    
  18.         //初始化Cipher对象    
  19.         cipher.init(mode, secretkey, sr);    
  20.         //加解密  
  21.         result = cipher.doFinal(data);   
  22.     } catch (NoSuchAlgorithmException e) {  
  23.         e.printStackTrace();  
  24.     } catch (InvalidKeySpecException e) {  
  25.         e.printStackTrace();  
  26.     } catch (NoSuchPaddingException e) {  
  27.         e.printStackTrace();  
  28.     } catch (InvalidKeyException e) {  
  29.         e.printStackTrace();  
  30.     } catch (IllegalBlockSizeException e) {  
  31.         e.printStackTrace();  
  32.     } catch (BadPaddingException e) {  
  33.         e.printStackTrace();  
  34.     }   
  35.       
  36.     return result;  
  37. }  

 

 2、byte数组转换成16进制字符串

 

 

 

Java代码  收藏代码
  1. /** 
  2.  * byte数组转换成16进制字符串 
  3. * @param b 
  4. * @return 
  5. */  
  6. ublic static String bytes2HexString(byte[] b) {  
  7.     String ret = "";  
  8.     for (int i = 0; i < b.length; i++) {  
  9.       String hex = Integer.toHexString(b[i] & 0xFF);  
  10.       if (hex.length() == 1) {  
  11.         hex = '0' + hex;  
  12.       }  
  13.       ret += hex.toUpperCase();  
  14.     }  
  15.     return ret;  

 

 

3、16进制字符串转成byte数组

 

 

Java代码  收藏代码
  1. /** 
  2.  * 16进制字符串转成byte数组 
  3. * @param src 
  4. * @return 
  5. */  
  6. ublic static byte[] hexString2Bytes(String src){  
  7.     byte[] ret = new byte[8];  
  8.     byte[] tmp = src.getBytes();  
  9.     for(int i=0; i<8; i++){  
  10.       ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);  
  11.     }  
  12.     return ret;  
  13. }  

 

 

 

Java代码  收藏代码
  1.  public static byte uniteBytes(byte src0, byte src1) {  
  2.         byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();  
  3.         _b0 = (byte)(_b0 << 4);  
  4.         byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();  
  5.         byte ret = (byte)(_b0 ^ _b1);  
  6.         return ret;  
  7. }  

 

 

 

执行

 

 

Java代码  收藏代码
  1. public static void main(String[] args) {  
  2.     //加解密模式  
  3.     int mode = Cipher.ENCRYPT_MODE;  
  4.     //被加解密byte数组16进制字符串  
  5.     String dataHexString = "1234567887654321";  
  6.     //秘钥byte数组16进制字符串  
  7.     String keyHexString = "9AAB1D2EE004AAC3";  
  8.     byte[] data = hexString2Bytes(dataHexString);  
  9.     byte[] key = hexString2Bytes(keyHexString);  
  10.     byte[] result = desCryt(data, key, mode);  
  11.     //打印结果  
  12.     System.out.println("结果:"+bytes2HexString(result));  
  13. }  

 

 结果:7D592BF239849E76

 

执行

 

 

Java代码  收藏代码
  1. public static void main(String[] args) {  
  2.     //加解密模式  
  3.     int mode = Cipher.DECRYPT_MODE;  
  4.     //被加解密byte数组16进制字符串  
  5.     String dataHexString = "7D592BF239849E76";  
  6.     //秘钥byte数组16进制字符串  
  7.     String keyHexString = "9AAB1D2EE004AAC3";  
  8.     byte[] data = hexString2Bytes(dataHexString);  
  9.     byte[] key = hexString2Bytes(keyHexString);  
  10.     byte[] result = desCryt(data, key, mode);  
  11.     //打印结果  
  12.     System.out.println("结果:"+bytes2HexString(result));  
  13. }  

 

 结果:1234567887654321

 

 

PS:

获取Cipher对象的时候一定要写成

Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");

不要写成

Cipher cipher = Cipher.getInstance("DES");

否则解密的时候会报错:

Given final block not properly padded


原因是Cipher cipher = Cipher.getInstance("DES");与Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");等同,填充方式错误,加密的时候会得到16长度的字节数组。

 

JCE详解传送门:http://docs.oracle.com/javase/1.5.0/docs/guide/security/jce/JCERefGuide.html

0 0
原创粉丝点击