Menu

#43 BasicTextEncryptor encryptor issue

v1.5
open
nobody
None
5
2015-11-21
2015-11-21
sagar
No

// see the comment in main method

package test;

import java.util.Random;

import org.jasypt.util.text.BasicTextEncryptor;

public class Main {
public static int getRandomNumberBetween(int high, int low) {
return new Random().nextInt(high - low) + low;
}

public static String getRandomStringofLength(int length) {
    final int asciicode33 = 33, asciicode = 126;
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < length; i++) {
        char asciiCode = (char) getRandomNumberBetween(asciicode, asciicode33);
        builder.append(asciiCode);
    }
    return builder.toString();
}

public static void main(String[] args) {

    String text = "some text";
    String key = getRandomStringofLength(30);
    String encryptedText = encryptEmail(text, key);

    String decryptedText = decryptEmail(encryptedText + "!", key);
    System.out.println("Original Text: " + text);
    System.out.println("Decrypted Text: " + decryptedText); //should this really match the original text?
}

public static String encryptEmail(String email, String key) {
    BasicTextEncryptor encryptor = new BasicTextEncryptor();
    encryptor.setPassword(key);
    return encryptor.encrypt(email);
}

public static String decryptEmail(String input, String key) {
    BasicTextEncryptor encryptor = new BasicTextEncryptor();
    encryptor.setPassword(key);
    return encryptor.decrypt(input);
}

}

Discussion


Log in to post a comment.

MongoDB Logo MongoDB