`
chuan9966
  • 浏览: 47037 次
文章分类
社区版块
存档分类
最新评论

JAVA实现DES加密

 
阅读更多
AES是美国联邦政府采用的商业及政府数据加密标准,预计将在未来几十年里代替DES在各个领域中得到广泛应用。AES提供128位密钥,因此,128位AES的加密强度是56位DES加密强度的1021倍还多。假设可以制造一部可以在1秒内破解DES密码的机器,那么使用这台机器破解一个128位AES密码需要大约149亿万年的时间。(更深一步比较而言,宇宙一般被认为存在了还不到200亿年)因此可以预计,美国国家标准局倡导的AES即将作为新标准取代DES。

Java代码 收藏代码
  1. packagecom.jshx.utils;
  2. importjava.io.UnsupportedEncodingException;
  3. importjava.security.InvalidKeyException;
  4. importjava.security.NoSuchAlgorithmException;
  5. importjava.security.SecureRandom;
  6. importjavax.crypto.BadPaddingException;
  7. importjavax.crypto.Cipher;
  8. importjavax.crypto.IllegalBlockSizeException;
  9. importjavax.crypto.KeyGenerator;
  10. importjavax.crypto.NoSuchPaddingException;
  11. importjavax.crypto.SecretKey;
  12. importjavax.crypto.spec.SecretKeySpec;
  13. importorg.apache.axis.encoding.Base64;
  14. publicclassAES{
  15. /**
  16. *加密
  17. *
  18. *@paramcontent需要加密的内容
  19. *@parampassword加密密码
  20. *@return
  21. */
  22. publicstaticbyte[]encrypt(Stringcontent,Stringpassword){
  23. try{
  24. KeyGeneratorkgen=KeyGenerator.getInstance("AES");
  25. kgen.init(128,newSecureRandom(password.getBytes()));
  26. SecretKeysecretKey=kgen.generateKey();
  27. byte[]enCodeFormat=secretKey.getEncoded();
  28. SecretKeySpeckey=newSecretKeySpec(enCodeFormat,"AES");
  29. Ciphercipher=Cipher.getInstance("AES");//创建密码器
  30. byte[]byteContent=content.getBytes("utf-8");
  31. cipher.init(Cipher.ENCRYPT_MODE,key);//初始化
  32. byte[]result=cipher.doFinal(byteContent);
  33. returnresult;//加密
  34. }catch(NoSuchAlgorithmExceptione){
  35. e.printStackTrace();
  36. }catch(NoSuchPaddingExceptione){
  37. e.printStackTrace();
  38. }catch(InvalidKeyExceptione){
  39. e.printStackTrace();
  40. }catch(UnsupportedEncodingExceptione){
  41. e.printStackTrace();
  42. }catch(IllegalBlockSizeExceptione){
  43. e.printStackTrace();
  44. }catch(BadPaddingExceptione){
  45. e.printStackTrace();
  46. }
  47. returnnull;
  48. }
  49. /**解密
  50. *@paramcontent待解密内容
  51. *@parampassword解密密钥
  52. *@return
  53. */
  54. publicstaticbyte[]decrypt(byte[]content,Stringpassword){
  55. try{
  56. KeyGeneratorkgen=KeyGenerator.getInstance("AES");
  57. kgen.init(128,newSecureRandom(password.getBytes()));
  58. SecretKeysecretKey=kgen.generateKey();
  59. byte[]enCodeFormat=secretKey.getEncoded();
  60. SecretKeySpeckey=newSecretKeySpec(enCodeFormat,"AES");
  61. Ciphercipher=Cipher.getInstance("AES");//创建密码器
  62. cipher.init(Cipher.DECRYPT_MODE,key);//初始化
  63. byte[]result=cipher.doFinal(content);
  64. returnresult;//加密
  65. }catch(NoSuchAlgorithmExceptione){
  66. e.printStackTrace();
  67. }catch(NoSuchPaddingExceptione){
  68. e.printStackTrace();
  69. }catch(InvalidKeyExceptione){
  70. e.printStackTrace();
  71. }catch(IllegalBlockSizeExceptione){
  72. e.printStackTrace();
  73. }catch(BadPaddingExceptione){
  74. e.printStackTrace();
  75. }
  76. returnnull;
  77. }
  78. /**将二进制转换成16进制
  79. *@parambuf
  80. *@return
  81. */
  82. publicstaticStringparseByte2HexStr(bytebuf[]){
  83. StringBuffersb=newStringBuffer();
  84. for(inti=0;i<buf.length;i++){
  85. Stringhex=Integer.toHexString(buf[i]&0xFF);
  86. if(hex.length()==1){
  87. hex='0'+hex;
  88. }
  89. sb.append(hex.toUpperCase());
  90. }
  91. returnsb.toString();
  92. }
  93. /**将16进制转换为二进制
  94. *@paramhexStr
  95. *@return
  96. */
  97. publicstaticbyte[]parseHexStr2Byte(StringhexStr){
  98. if(hexStr.length()<1)
  99. returnnull;
  100. byte[]result=newbyte[hexStr.length()/2];
  101. for(inti=0;i<hexStr.length()/2;i++){
  102. inthigh=Integer.parseInt(hexStr.substring(i*2,i*2+1),16);
  103. intlow=Integer.parseInt(hexStr.substring(i*2+1,i*2+2),16);
  104. result[i]=(byte)(high*16+low);
  105. }
  106. returnresult;
  107. }
  108. /**
  109. *加密
  110. *
  111. *@paramcontent需要加密的内容
  112. *@parampassword加密密码
  113. *@return
  114. */
  115. publicstaticbyte[]encrypt2(Stringcontent,Stringpassword){
  116. try{
  117. SecretKeySpeckey=newSecretKeySpec(password.getBytes(),"AES");
  118. Ciphercipher=Cipher.getInstance("AES/ECB/NoPadding");
  119. byte[]byteContent=content.getBytes("utf-8");
  120. cipher.init(Cipher.ENCRYPT_MODE,key);//初始化
  121. byte[]result=cipher.doFinal(byteContent);
  122. returnresult;//加密
  123. }catch(NoSuchAlgorithmExceptione){
  124. e.printStackTrace();
  125. }catch(NoSuchPaddingExceptione){
  126. e.printStackTrace();
  127. }catch(InvalidKeyExceptione){
  128. e.printStackTrace();
  129. }catch(UnsupportedEncodingExceptione){
  130. e.printStackTrace();
  131. }catch(IllegalBlockSizeExceptione){
  132. e.printStackTrace();
  133. }catch(BadPaddingExceptione){
  134. e.printStackTrace();
  135. }
  136. returnnull;
  137. }
  138. publicstaticvoidmain(String[]args){
  139. Stringcontent="test";
  140. Stringpassword="12345678";
  141. //加密
  142. System.out.println("加密前:"+content);
  143. byte[]encryptResult=encrypt(content,password);
  144. Stringtt4=Base64.encode(encryptResult);
  145. System.out.println(newString(tt4));
  146. //解密
  147. byte[]decryptResult=decrypt(encryptResult,password);
  148. System.out.println("解密后:"+newString(decryptResult));
  149. }
  150. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics