PageRenderTime 46ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/javaSecu/src/com/liwei/javasecu/corejava/Eryptogram.java

http://liweistudy.googlecode.com/
Java | 145 lines | 74 code | 18 blank | 53 comment | 9 complexity | 3fcc81407979ebbac18c25e4f2844c1c MD5 | raw file
  1. package com.liwei.javasecu.corejava;
  2. /**
  3. * @(#)Eryptogram.java 1.00 04/03/11
  4. *
  5. * Copyright (c) 2003-2004 Abacus,Ltd.
  6. *
  7. * ?????
  8. *
  9. *
  10. *
  11. */
  12. import javax.crypto.Cipher;
  13. import javax.crypto.KeyGenerator;
  14. import javax.crypto.SecretKey;
  15. /**
  16. * ?????
  17. *
  18. * @author WangHu
  19. * @version 1.00 2004?03?18?
  20. */
  21. public class Eryptogram {
  22. private static String Algorithm = "DES";
  23. // ?? ????,?? DES,DESede,Blowfish
  24. static boolean debug = false;
  25. /**
  26. * ?????.
  27. */
  28. public Eryptogram() {
  29. }
  30. /**
  31. * ????
  32. *
  33. * @return byte[] ???????
  34. * @throws exception
  35. * ????.
  36. */
  37. public static byte[] getSecretKey() throws Exception {
  38. KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
  39. SecretKey deskey = keygen.generateKey();
  40. if (debug)
  41. System.out.println("????:" + byte2hex(deskey.getEncoded()));
  42. return deskey.getEncoded();
  43. }
  44. /**
  45. * ?????????????????
  46. *
  47. * @param input
  48. * ???????
  49. * @param key
  50. * ??
  51. * @return byte[] ??????
  52. * @throws Exception
  53. */
  54. public static byte[] encryptData(byte[] input, byte[] key) throws Exception {
  55. SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, Algorithm);
  56. if (debug) {
  57. System.out.println("???????:" + byte2hex(input));
  58. System.out.println("???????:" + new String(input));
  59. }
  60. Cipher c1 = Cipher.getInstance(Algorithm);
  61. c1.init(Cipher.ENCRYPT_MODE, deskey);
  62. byte[] cipherByte = c1.doFinal(input);
  63. if (debug)
  64. System.out.println("???????:" + byte2hex(cipherByte));
  65. return cipherByte;
  66. }
  67. /**
  68. * ?????????????????????
  69. *
  70. * @param input
  71. * ??????
  72. * @param key
  73. * ??
  74. * @return byte[] ??????
  75. * @throws Exception
  76. */
  77. public static byte[] decryptData(byte[] input, byte[] key) throws Exception {
  78. SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, Algorithm);
  79. if (debug)
  80. System.out.println("??????:" + byte2hex(input));
  81. Cipher c1 = Cipher.getInstance(Algorithm);
  82. c1.init(Cipher.DECRYPT_MODE, deskey);
  83. byte[] clearByte = c1.doFinal(input);
  84. if (debug) {
  85. System.out.println("???????:" + byte2hex(clearByte));
  86. System.out.println("???????:" + (new String(clearByte)));
  87. }
  88. return clearByte;
  89. }
  90. /**
  91. * ??????16?????
  92. *
  93. * @param byte[] b ?????????
  94. * @return String ??????16?????
  95. */
  96. public static String byte2hex(byte[] b) {
  97. String hs = "";
  98. String stmp = "";
  99. for (int n = 0; n < b.length; n++) {
  100. stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
  101. if (stmp.length() == 1)
  102. hs = hs + "0" + stmp;
  103. else
  104. hs = hs + stmp;
  105. if (n < b.length - 1)
  106. hs = hs + ":";
  107. }
  108. return hs.toUpperCase();
  109. }
  110. public static void main(String[] args) {
  111. try {
  112. debug = false;
  113. Eryptogram etg = new Eryptogram();
  114. byte[] key = etg.getSecretKey();
  115. System.out.println("key = " + key);
  116. String aa = "1234567";
  117. byte[] data = aa.getBytes();
  118. System.out.println(data);
  119. byte[] en = etg.encryptData(data, key);
  120. System.out.println("encryptData = " + new String(en));
  121. byte[] de = etg.decryptData(en, key);
  122. System.out.println("decryptData = " + new String(de));
  123. } catch (Exception e) {
  124. e.printStackTrace();
  125. }
  126. }
  127. }