Skip to content

Instantly share code, notes, and snippets.

@bitsofinfo
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitsofinfo/145ea7279dfc18d1dc9d to your computer and use it in GitHub Desktop.
Save bitsofinfo/145ea7279dfc18d1dc9d to your computer and use it in GitHub Desktop.
package org.bitsofinfo;
import java.security.Provider;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
import org.jasypt.salt.RandomSaltGenerator;
/**
*
* a) You need the following JARs in your classpath
* - jasypt-1.9.1.jar
* - bcprov-ext-jdk15on-151.jar
* - bcprov-jdk15on-151.jar
*
* b) Run w/ Java 7
*
*
* @author bitsofinfo
*
*/
public class CryptTest {
public static RandomSaltGenerator saltGen = new RandomSaltGenerator();
public static void main(String[] args) {
/*
Security.addProvider(new BouncyCastleProvider());
for (Provider prov : Security.getProviders()) {
System.out.println(prov.getName());
for (Service srv : prov.getServices()) {
System.out.println("\tType:" + srv.getType());
System.out.println("\t Algo: " +srv.getAlgorithm());
}
}
*/
new CryptTest().run();
}
public void run() {
boolean testBouncyCastle = false;
String algorithm = "PBEWithMD5AndTripleDES";
Provider customProvider = null; // default sun
if (testBouncyCastle) {
customProvider = new BouncyCastleProvider();
algorithm = "PBEWITHSHA256AND256BITAES-CBC-BC";
}
for (int b=0; b<1000; b++) {
JasyptPBEEncryptor jte1 = new JasyptPBEEncryptor(algorithm, 1, "1232232".toCharArray(),customProvider);
JasyptPBEEncryptor jte2 = new JasyptPBEEncryptor(algorithm, 1, "abc8890".toCharArray(),customProvider);
JasyptPBEEncryptor[] cryptors = new JasyptPBEEncryptor[]{jte1, jte2};
for (int i=0; i<cryptors.length; i++) {
JasyptPBEEncryptor cryptor = cryptors[i];
try {
byte[] crypted1 = cryptor.encrypt("hello world".getBytes("UTF-8"));
System.out.println("ENCRYPTED WITH " + (i+1));
for (int j=0; j<cryptors.length; j++) {
JasyptPBEEncryptor decryptor = cryptors[j];
try {
byte[] decrypted = decryptor.decrypt(crypted1);
System.out.println("\tDECRYPTOR " + (j+1) + " decrypted OK: " + new String(decrypted,"UTF-8"));
String x = new String(decrypted,"UTF-8");
if (!x.equals("hello world")) {
throw new RuntimeException("Decryption did not fail however output does not match " +
"'hello world' why did we not get EncryptionOperationNotPossibleException???! OUTPUT=" + x);
}
} catch(EncryptionOperationNotPossibleException e) {
// expected
}
}
} catch(Exception e) {
System.out.println("FAIL: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
}
}
public class JasyptPBEEncryptor {
private StandardPBEByteEncryptor crypter = null;
public JasyptPBEEncryptor(String algorithm, int iterations, char[] key, Provider customProvider) {
this.crypter = new StandardPBEByteEncryptor();
if (customProvider != null) {
this.crypter.setProvider(customProvider);
}
this.crypter.setSaltGenerator(saltGen);
this.crypter.setAlgorithm(algorithm);
this.crypter.setKeyObtentionIterations(iterations);
this.crypter.setPasswordCharArray(key);
this.crypter.initialize();
}
public byte[] decrypt(byte[] cipher) throws Exception {
return this.crypter.decrypt(cipher);
}
public byte[] encrypt(byte[] clear) throws Exception {
return this.crypter.encrypt(clear);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment