fork(8) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.security.spec.*;
  7. import javax.crypto.*;
  8. import javax.crypto.spec.*;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. private static String byteToHex(byte[] bytes) {
  14. StringBuilder sb = new StringBuilder();
  15. for (byte b : bytes) {
  16. sb.append(String.format("%02X ", b));
  17. }
  18. return sb.toString();
  19. }
  20.  
  21. public static void main (String[] args) throws java.lang.Exception
  22. {
  23. final byte[] SALT= { (byte) 0x21, (byte) 0x21, (byte) 0xF0, (byte) 0x55,
  24. (byte) 0xC3, (byte) 0x9F, (byte) 0x5A, (byte) 0x75 };
  25. final int ITERATION_COUNT = 31;
  26. KeySpec keySpec = new PBEKeySpec("test".toCharArray(), SALT, ITERATION_COUNT);
  27. AlgorithmParameterSpec paramSpec = new PBEParameterSpec(SALT, ITERATION_COUNT);
  28.  
  29. SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
  30.  
  31. Cipher c = Cipher.getInstance("PBEWithMD5AndDES");
  32. c.init(Cipher.ENCRYPT_MODE, key, paramSpec);
  33.  
  34. byte[] ct = c.doFinal("test".getBytes("UTF-8"));
  35.  
  36. System.out.println(byteToHex(ct));
  37. }
  38. }
Success #stdin #stdout 0.23s 321920KB
stdin
Standard input is empty
stdout
AA 81 01 A7 D6 30 93 C6