JAVA实现DES加密

DES算法为密码体制中的对称密码体制,又被成为美国数据加密标准,是1972年美国IBM公司研制的对称密码体制加密算法。其密钥长度为56位,明文按64位进行分组,将分组后的明文组和56位的密钥按位替代或交换的方法形成密文组的加密方法。

DES加密算法特点:分组比较短、密钥太短、密码生命周期短、运算速度较慢。

DES工作的基本原理是,其入口参数有三个:key、data、mode。 key为加密解密使用的密钥,data为加密解密的数据,mode为其工作模式。当模式为加密模式时,明文按照64

DES加密算法 DES加密算法

位进行分组,形成明文组,key用于对数据加密,当模式为解密模式时,key用于对数据解密。实际运用中,密钥只用到了64位中的56位,这样才具有高的安全性。

DES(Data EncryptionStandard)算法,于1977年得到美国政府的正式许可,是一种用56位密钥来加密64位数据的方法。虽然56位密钥的DES算法已经风光不在,而且常有用Des加密的明文被破译的报道,但是了解一下昔日美国的标准加密算法总是有益的,而且目前DES算法得到了广泛的应用,在某些场合,仍然发挥着余热。


  1. package  test;  

  2. import java.io.FileInputStream;  

  3. import java.io.FileOutputStream;  

  4. import java.io.InputStream;  

  5. import java.io.OutputStream;  

  6. import java.security.Key;  

  7. import java.security.SecureRandom;  

  8. import javax.crypto.Cipher;  

  9. import javax.crypto.CipherInputStream;  

  10. import javax.crypto.CipherOutputStream;  

  11. import javax.crypto.KeyGenerator;  

  12. import sun.misc.BASE64Decoder;  

  13. import sun.misc.BASE64Encoder;  

  14. publicclass DESUtil {  

  15.    Key key ;  

  16. public DESUtil() {  

  17.    }  

  18. public DESUtil(String str) {  

  19.       setKey(str); // 生成密匙

  20.    }  

  21. public Key getKey() {  

  22. return key ;  

  23.    }  

  24. publicvoid setKey(Key key) {  

  25. this . key = key;  

  26.    }  

  27. /**

  28.      * 根据参数生成 KEY

  29.      */

  30. publicvoid setKey(String strKey) {  

  31. try {  

  32.           KeyGenerator _generator = KeyGenerator.getInstance ( "DES" );  

  33.           _generator.init( new SecureRandom(strKey.getBytes()));  

  34. this . key = _generator.generateKey();  

  35.           _generator = null ;  

  36.       } catch (Exception e) {  

  37. thrownew RuntimeException(  

  38. "Error initializing SqlMap class. Cause: " + e);  

  39.       }  

  40.    }  

  41. /**

  42.      * 加密 String 明文输入 ,String 密文输出

  43.      */

  44. public String encryptStr(String strMing) {  

  45. byte [] byteMi = null ;  

  46. byte [] byteMing = null ;  

  47.       String strMi = "" ;  

  48.       BASE64Encoder base64en = new BASE64Encoder();  

  49. try {  

  50.           byteMing = strMing.getBytes( "UTF8" );  

  51.           byteMi = this .encryptByte(byteMing);  

  52.           strMi = base64en.encode(byteMi);  

  53.       } catch (Exception e) {  

  54. thrownew RuntimeException(  

  55. "Error initializing SqlMap class. Cause: " + e);  

  56.       } finally {  

  57.           base64en = null ;  

  58.           byteMing = null ;  

  59.           byteMi = null ;  

  60.       }  

  61. return strMi;  

  62.    }  

  63. /**

  64.      * 解密 以 String 密文输入 ,String 明文输出

  65.      *

  66.      * @param strMi

  67.      * @return

  68.      */

  69. public String decryptStr(String strMi) {  

  70.       BASE64Decoder base64De = new BASE64Decoder();  

  71. byte [] byteMing = null ;  

  72. byte [] byteMi = null ;  

  73.       String strMing = "" ;  

  74. try {  

  75.           byteMi = base64De.decodeBuffer(strMi);  

  76.           byteMing = this .decryptByte(byteMi);  

  77.           strMing = new String(byteMing, "UTF8" );  

  78.       } catch (Exception e) {  

  79. thrownew RuntimeException(  

  80. "Error initializing SqlMap class. Cause: " + e);  

  81.       } finally {  

  82.           base64De = null ;  

  83.           byteMing = null ;  

  84.           byteMi = null ;  

  85.       }  

  86. return strMing;  

  87.    }  

  88. /**

  89.      * 加密以 byte[] 明文输入 ,byte[] 密文输出

  90.      *

  91.      * @param byteS

  92.      * @return

  93.      */

  94. privatebyte [] encryptByte( byte [] byteS) {  

  95. byte [] byteFina = null ;  

  96.       Cipher cipher;  

  97. try {  

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

  99.           cipher.init(Cipher. ENCRYPT_MODE , key );  

  100.           byteFina = cipher.doFinal(byteS);  

  101.       } catch (Exception e) {  

  102. thrownew RuntimeException(  

  103. "Error initializing SqlMap class. Cause: " + e);  

  104.       } finally {  

  105.           cipher = null ;  

  106.       }  

  107. return byteFina;  

  108.    }  

  109. /**

  110.      * 解密以 byte[] 密文输入 , 以 byte[] 明文输出

  111.      *

  112.      * @param byteD

  113.      * @return

  114.      */

  115. privatebyte [] decryptByte( byte [] byteD) {  

  116.       Cipher cipher;  

  117. byte [] byteFina = null ;  

  118. try {  

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

  120.           cipher.init(Cipher. DECRYPT_MODE , key );  

  121.           byteFina = cipher.doFinal(byteD);  

  122.       } catch (Exception e) {  

  123. thrownew RuntimeException(  

  124. "Error initializing SqlMap class. Cause: " + e);  

  125.       } finally {  

  126.           cipher = null ;  

  127.       }  

  128. return byteFina;  

  129.    }  

  130. /**

  131.      * 文件 file 进行加密并保存目标文件 destFile 中

  132.      *

  133.      * @param file

  134.      *             要加密的文件 如 c:/test/srcFile.txt

  135.      * @param destFile

  136.      *             加密后存放的文件名 如 c:/ 加密后文件 .txt

  137.      */

  138. publicvoid encryptFile(String file, String destFile) throws Exception {  

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

  140. // cipher.init(Cipher.ENCRYPT_MODE, getKey());

  141.       cipher.init(Cipher. ENCRYPT_MODE , this . key );  

  142.       InputStream is = new FileInputStream(file);  

  143.       OutputStream out = new FileOutputStream(destFile);  

  144.       CipherInputStream cis = new CipherInputStream(is, cipher);  

  145. byte [] buffer = newbyte [1024];  

  146. int r;  

  147. while ((r = cis.read(buffer)) > 0) {  

  148. out.write(buffer, 0, r);  

  149.       }  

  150.       cis.close();  

  151. is.close();  

  152. out.close();  

  153.    }  

  154. /**

  155.      * 文件采用 DES 算法解密文件

  156.      *

  157.      * @param file

  158.      *             已加密的文件 如 c:/ 加密后文件 .txt *

  159.      * @param destFile

  160.      *             解密后存放的文件名 如 c:/ test/ 解密后文件 .txt

  161.      */

  162. publicvoid decryptFile(String file, String dest) throws Exception {  

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

  164.       cipher.init(Cipher. DECRYPT_MODE , this . key );  

  165.       InputStream is = new FileInputStream(file);  

  166.       OutputStream out = new FileOutputStream(dest);  

  167.       CipherOutputStream cos = new CipherOutputStream(out, cipher);  

  168. byte [] buffer = newbyte [1024];  

  169. int r;  

  170. while ((r = is.read(buffer)) >= 0) {  

  171.           cos.write(buffer, 0, r);  

  172.       }  

  173.       cos.close();  

  174. out.close();  

  175. is.close();  

  176.    }  

  177. publicstaticvoid main(String[] args) throws Exception {  

  178.       DESUtil des = new DESUtil( "1234567" );  

  179. // DES 加密文件

  180. // des.encryptFile("G:/test.doc", "G:/ 加密 test.doc");

  181. // DES 解密文件

  182. // des.decryptFile("G:/ 加密 test.doc", "G:/ 解密 test.doc");

  183.       String str1 = " 要加密的字符串 test" ;  

  184. // DES 加密字符串

  185.       String str2 = des.encryptStr(str1);  

  186. // DES 解密字符串

  187.       String deStr = des.decryptStr(str2);  

  188.       System. out .println( " 加密前: " + str1);  

  189.       System. out .println( " 加密后: " + str2);  

  190.       System. out .println( " 解密后: " + deStr);  

  191.    }  

  192. }  


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