Pages

2011年9月7日 星期三

使用Triple DES 加密字串

最近因為有需求,需要使用加密的方法來讓連線的帳號密碼用成亂碼來連線。
因此找到了使用 Tripple DES的方式加密字串。

1. 首先,先寄建立Decrypter與Encrypter
  1. package security.crypto;
  2.  
  3. import java.io.UnsupportedEncodingException;
  4. import javax.crypto.Cipher;
  5. import javax.crypto.IllegalBlockSizeException;
  6. import javax.crypto.SecretKey;
  7.  
  8. public classDecrypter {
  9. Cipher decipher;
  10.  
  11. public Decrypter(SecretKey key) {
  12. try {
  13. decipher = Cipher.getInstance("DESede");
  14. decipher.init(Cipher.DECRYPT_MODE, key);
  15.  
  16. } catch (javax.crypto.NoSuchPaddingException e) {
  17. e.printStackTrace();
  18. } catch (java.security.NoSuchAlgorithmException e) {
  19. e.printStackTrace();
  20. } catch (java.security.InvalidKeyException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24.  
  25. public String decrypt(String str) {
  26. try {
  27. byte[] decipher1 = new sun.misc.BASE64Decoder().decodeBuffer(str);
  28.  
  29. byte[] decryptedBytes = decipher.doFinal(decipher1);
  30.  
  31. return new String(decryptedBytes, "UTF8");
  32. } catch (javax.crypto.BadPaddingException e) {
  33. e.printStackTrace();
  34. } catch (IllegalBlockSizeException e) {
  35. e.printStackTrace();
  36. } catch (UnsupportedEncodingException e) {
  37. e.printStackTrace();
  38. } catch (java.io.IOException e) {
  39. e.printStackTrace();
  40. }
  41. return null;
  42. }
  43. }

  1. package security.crypto;
  2. import java.io.UnsupportedEncodingException;
  3.  
  4. import javax.crypto.Cipher;
  5. import javax.crypto.IllegalBlockSizeException;
  6. import javax.crypto.SecretKey;
  7.  
  8. import sun.misc.BASE64Encoder;
  9.  
  10. public class Encrypter {
  11. private Cipher encipher;
  12.  
  13. public Encrypter(SecretKey key) {
  14. try {
  15. encipher = Cipher.getInstance("DESede");
  16. encipher.init(Cipher.ENCRYPT_MODE, key);
  17.  
  18. } catch (javax.crypto.NoSuchPaddingException e) {
  19. e.printStackTrace();
  20. } catch (java.security.NoSuchAlgorithmException e) {
  21. e.printStackTrace();
  22. } catch (java.security.InvalidKeyException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26.  
  27. public String encrypt(String str) {
  28. try {
  29. byte[] encipher1 = str.getBytes("UTF8");
  30.  
  31. byte[] encrpyedBytes = encipher.doFinal(encipher1);
  32.  
  33. return new BASE64Encoder().encode(encrpyedBytes);
  34. } catch (javax.crypto.BadPaddingException e) {
  35. e.printStackTrace();
  36. } catch (IllegalBlockSizeException e) {
  37. e.printStackTrace();
  38. } catch (UnsupportedEncodingException e) {
  39. e.printStackTrace();
  40. }
  41. return null;
  42. }
  43. }

2. 接著建立使用該方法的連接介面
  1. package security;
  2.  
  3. import javax.crypto.SecretKey;
  4. import javax.crypto.spec.SecretKeySpec;
  5.  
  6. import wn.security.crypto.TripleDESDecrypter;
  7. import wn.security.crypto.TripleDESEncrypter;
  8.  
  9. /**
  10. *
  11. * @author asqqsa
  12. *
  13. * Here is a basic implementation of encrpytion and decryption using TripleDES.
  14. *
  15. */
  16.  
  17. public class DES{
  18.  
  19. public String getDecryptoString(String string) {
  20. String decrypted = null;
  21. try {
  22. Decrypter decrypter = new Decrypter(getKey());
  23. decrypted = decrypter.decrypt(string);
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. return decrypted;
  28. }
  29. public String getEncryptoString(String string) {
  30. String encrypted = null;
  31. try {
  32. Encrypter encrypter = new Encrypter(getKey());
  33. encrypted = encrypter.encrypt(string);
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. return encrypted;
  38. }
  39.  
  40. private SecretKey getKey() {
  41. // there are 32 chars
  42. String keyString ="1234567890abcdefghijklmnopqrstuv";
  43. byte[] keyB = new byte[24]; // a Triple DES key is a byte[24] array
  44. for (int i = 0; i < keyString.length() && i < keyB.length; i++) {
  45. keyB[i] = (byte) keyString.charAt(i);
  46. }
  47. SecretKey key = new SecretKeySpec(keyB, "DESede");
  48. return key;
  49. }
  50. /*****Random Generate Key Vlaue***
  51. public SecretKey getKey() {
  52. try {
  53. return KeyGenerator.getInstance("DESede").generateKey();
  54. } catch (NoSuchAlgorithmException e) {
  55. e.printStackTrace();
  56. }
  57. return null;
  58. }
  59. **/
  60. }
3. 建立JSP介面測試
  1. <%@ page import="security.DES" %>
  2. <%@ page contentType="text/html;charset=big5" %>
  3. <html>
  4. <head></head>
  5. <body>
  6. <%
  7. DESCrypto des = new DESCrypto();
  8. String plain = null;
  9. String encrp = null;
  10. encrp = request.getParameter("plaintext");
  11. plain = request.getParameter("secrettext");
  12. %>
  13. <form method='post' name='des' action='Crypto.jsp'>
  14. <table border = 1>
  15. <tr><td colspan='2'>字串Triple DES 使用單一Key 加解密</td> </tr>
  16. <tr>
  17. <td>plaintext encrypto</td>
  18. <td>secrettext decrypto</td>
  19. </tr>
  20. <tr>
  21. <td><input name='plaintext' type='text'></inpute></td>
  22. <td><input name='secrettext' type='text'></inpute></td>
  23. </tr>
  24. <tr>
  25. <td><%= (encrp == null || encrp.equals(""))? " " : des.getEncryptoString(encrp)%></td>
  26. <td><%= (plain == null || plain.equals(""))? " ": des.getDecryptoString(plain)%></td>
  27. </tr>
  28. <tr>
  29. <td colspan='2'><input type='submit' value='submit'></inpute></td>
  30. </tr>
  31. </table>
  32. </form>
  33. </body>
  34. </html>

Related Posts:

  • Linux下打包壓縮war、解壓war包和jar命令把project_a文件夾下的文件打包成project.war 1. 打包  jar -xvf project.war /project_a -c   創建war包 -v   顯示過程信息 -f   指定 JAR 檔案名,通常這個參數是必須的 -M  不產生所有項的清單(MANIFEST〕檔,此參數會忽略 -m 參數 -0   這個是阿拉伯數字,只打包不壓縮的意思 2. 特定檔案… Read More
  • 自動擷取憑證與Https頁面資料在寫JSP使用Redirect的時候,如果是導向HTTPS頁面,也可能發生某些異常。 1. java.net.MalformedURLException: unknown protocol https ANS: 使用的方法不是 HTTPS 的方式,將jce.jar與jsse.jar導入classpath,在使用https連線函式 2. 頁面停留過久 ANS: 就像打開IE一樣,會叫你點選接受憑證,這時用程式跑就自動停住了,居然不會timeout … Read More
  • Big5部分中文顯示?處理最近在抓股票資料,但是因為有些上市櫃公司中文字,在預設Big5編碼中不支援,文字抓出來後會顯示?號,因此需要指定特定的編碼方式,才能正確的寫入資料庫中,目前Java的的解決方式如下: File sotckFile = new File(fileName); InputStreamReader isr = null; BufferedReader br = null; try { isr = new InputStreamReader(… Read More
  • 使用Triple DES 加密字串最近因為有需求,需要使用加密的方法來讓連線的帳號密碼用成亂碼來連線。 因此找到了使用 Tripple DES的方式加密字串。 1. 首先,先寄建立Decrypter與Encrypter package security.crypto; import java.io.UnsupportedEncodingException; import javax.crypto.Cipher; import javax.crypto.IllegalBloc… Read More
  • Java使用Tesseract進行OCR辨識的一些心得最近有個專案想抓取PDF的檔案內容,進行檔案rename的動作,因此才開始瞭解Tesseract相關的用法,以下是使用的一些心得。1. Windows上安裝Tesseract 5.0後,如果想轉移到其他台Windows PC,該PC要先安裝 Visual Studio 2015 的 Visual C++ 可轉散發套件,再將Tesseract安裝的目錄整個zip起來後,在其他台Windows PC上解壓縮後,設定Tesseract相關windows… Read More

0 意見: