fork download
  1. import java.security.MessageDigest;
  2. import java.util.Arrays;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.SecretKey;
  5. import javax.crypto.spec.IvParameterSpec;
  6. import javax.crypto.spec.SecretKeySpec;
  7.  
  8. class TripleDESTest {
  9.  
  10. public static void main(String[] args) throws Exception {
  11.  
  12. Class1 key = new Class1();
  13. String text = System.out.println(key);
  14.  
  15.  
  16.  
  17.  
  18. byte[] codedtext = new TripleDESTest().encrypt(text);
  19. String decodedtext = new TripleDESTest().decrypt(codedtext);
  20.  
  21. System.out.println(codedtext); // this is a byte array, you'll just see a reference to an array
  22. System.out.println(decodedtext); // This correctly shows "kyle boon"
  23. }
  24.  
  25. public byte[] encrypt(String message) throws Exception {
  26. final MessageDigest md = MessageDigest.getInstance("md5");
  27. final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
  28. .getBytes("utf-8"));
  29. final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
  30. for (int j = 0, k = 16; j < 8;) {
  31. keyBytes[k++] = keyBytes[j++];
  32. }
  33.  
  34. final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
  35. final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
  36. final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
  37. cipher.init(Cipher.ENCRYPT_MODE, key, iv);
  38.  
  39. final byte[] plainTextBytes = message.getBytes("utf-8");
  40. final byte[] cipherText = cipher.doFinal(plainTextBytes);
  41. // final String encodedCipherText = new sun.misc.BASE64Encoder()
  42. // .encode(cipherText);
  43.  
  44. return cipherText;
  45. }
  46.  
  47. public String decrypt(byte[] message) throws Exception {
  48. final MessageDigest md = MessageDigest.getInstance("md5");
  49. final byte[] digestOfPassword = md.digest("HG58YZ3CR9"
  50. .getBytes("utf-8"));
  51. final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
  52. for (int j = 0, k = 16; j < 8;) {
  53. keyBytes[k++] = keyBytes[j++];
  54. }
  55.  
  56. final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
  57. final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
  58. final Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
  59. decipher.init(Cipher.DECRYPT_MODE, key, iv);
  60.  
  61. // final byte[] encData = new
  62. // sun.misc.BASE64Decoder().decodeBuffer(message);
  63. final byte[] plainText = decipher.doFinal(message);
  64.  
  65. return new String(plainText, "UTF-8");
  66. }
  67. }
  68. class Class1
  69. {
  70. private final char[] lst = { 'B', 'A', 'R', 'C', 'O', '-', 'S', 'T', 'R', 'I', 'N', 'G', 'E', 'N', 'C', 'R', 'Y', 'P', 'T', 'I', 'O', 'N', '-', 'U', 'L' };
  71.  
  72. Class1() {}
  73.  
  74. public String toString()
  75. {
  76. return new String(this.lst);
  77. }
  78. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:13: error: incompatible types: void cannot be converted to String
		String text = System.out.println(key);
		                                ^
1 error
stdout
Standard output is empty