fork download
  1. import java.security.Key;
  2.  
  3. import javax.crypto.Cipher;
  4. import javax.crypto.KeyGenerator;
  5. import javax.crypto.SecretKey;
  6. import javax.crypto.SecretKeyFactory;
  7. import javax.crypto.spec.PBEKeySpec;
  8. import javax.crypto.spec.PBEParameterSpec;
  9.  
  10. public class MainClass {
  11. public static void main(String[] args) throws Exception {
  12. KeyGenerator kg = KeyGenerator.getInstance("DESede");
  13. Key sharedKey = kg.generateKey();
  14.  
  15. String password = "password";
  16. byte[] salt = "salt1234".getBytes();
  17. PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 20);
  18. PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
  19. SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
  20. SecretKey passwordKey = kf.generateSecret(keySpec);
  21. Cipher c = Cipher.getInstance("PBEWithMD5AndDES");
  22. c.init(Cipher.WRAP_MODE, passwordKey, paramSpec);
  23. byte[] wrappedKey = c.wrap(sharedKey);
  24.  
  25. c = Cipher.getInstance("DESede");
  26. c.init(Cipher.ENCRYPT_MODE, sharedKey);
  27. byte[] input = "input".getBytes();
  28. byte[] encrypted = c.doFinal(input);
  29.  
  30. c = Cipher.getInstance("PBEWithMD5AndDES");
  31.  
  32. c.init(Cipher.UNWRAP_MODE, passwordKey, paramSpec);
  33. Key unwrappedKey = c.unwrap(wrappedKey, "DESede", Cipher.SECRET_KEY);
  34.  
  35. c = Cipher.getInstance("DESede");
  36. c.init(Cipher.DECRYPT_MODE, unwrappedKey);
  37. System.out.println(new String(c.doFinal(encrypted)));
  38. }
  39. }
Success #stdin #stdout 0.02s 26188KB
stdin
KARINA2005
stdout
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

public class MainClass {
  public static void main(String[] args) throws Exception {
    KeyGenerator kg = KeyGenerator.getInstance("DESede");
    Key sharedKey = kg.generateKey();

    String password = "password";
    byte[] salt = "salt1234".getBytes();
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 20);
    PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
    SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey passwordKey = kf.generateSecret(keySpec);
    Cipher c = Cipher.getInstance("PBEWithMD5AndDES");
    c.init(Cipher.WRAP_MODE, passwordKey, paramSpec);
    byte[] wrappedKey = c.wrap(sharedKey);

    c = Cipher.getInstance("DESede");
    c.init(Cipher.ENCRYPT_MODE, sharedKey);
    byte[] input = "input".getBytes();
    byte[] encrypted = c.doFinal(input);

    c = Cipher.getInstance("PBEWithMD5AndDES");

    c.init(Cipher.UNWRAP_MODE, passwordKey, paramSpec);
    Key unwrappedKey = c.unwrap(wrappedKey, "DESede", Cipher.SECRET_KEY);

    c = Cipher.getInstance("DESede");
    c.init(Cipher.DECRYPT_MODE, unwrappedKey);
    System.out.println(new String(c.doFinal(encrypted)));
  }
}