3

I need to create two simple methods for string DES encryption/decruption. The goal is to have these two methods in the following form

public static String desEcnrypt(String key, String clearMessage) { ..... }

public static String desDecrypt(String key, String encryptedMessage) { ..... }

I haven't found yet any example in this form.

1
  • 1
    Most examples use arrays of bytes because that is what encryption algorithms operate on, not strings. You can simply convert between the bytes and an encoding of your choice, however. Also, don't use DES, AES is superior. Commented Jul 17, 2016 at 4:52

1 Answer 1

3

Use the "not-yet-commons-ssl.jar" from http://juliusdavies.ca/commons-ssl/.

http://juliusdavies.ca/commons-ssl/pbe.html

PBE code example (DES-3):*

char[] password = {'c','h','a','n','g','e','i','t'};
byte[] data = "Hello World!".getBytes();

// Encrypt!
byte[] encrypted = OpenSSL.encrypt("des3", password, data);
System.out.println("ENCRYPTED: [" + new String(encrypted) + "]");

// Decrypt results of previous!
data = OpenSSL.decrypt("des3", password, encrypted);
System.out.println("DECRYPTED: [" + new String(data) + "]");


OUTPUT:
=======================
ENCRYPTED: [U2FsdGVkX19qplb9qVDVVEYxH8wjJDGpMS+F4/2pS2c=]
DECRYPTED: [Hello World!]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.