Skip to content

Instantly share code, notes, and snippets.

@aaronzirbes
Created February 12, 2013 21:52
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 aaronzirbes/4773776 to your computer and use it in GitHub Desktop.
Save aaronzirbes/4773776 to your computer and use it in GitHub Desktop.
Ensure encryption algos encrypt to the same field length
@Grapes([
@Grab(group='org.bouncycastle', module='bcprov-jdk15on', version='1.47'),
@Grab(group='org.jasypt', module='jasypt', version='1.9.0')
])
import java.security.Provider
import java.security.Security
import java.security.NoSuchAlgorithmException
import org.jasypt.exceptions.EncryptionInitializationException
import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor
final String SAMPLE_STRING = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM+_)(*&^%$#@!1234567890[]{};:\'"<>,./?`~'
final String PROVIDER = 'BC'
final String ENCRYPTION_PASSWORD = 's3kr1t'
Security.addProvider(new BouncyCastleProvider());
String fromAlgorithm = 'PBEWITHSHA256AND128BITAES-CBC-BC'
String toAlgorithm = 'PBEWITHMD5AND128BITAES-CBC-OPENSSL'
println 'Provider'.padRight(32) + 'Algorithm'
println '-'.padRight(31, '-') + ' '.padRight(64, '-')
Provider bcProvider = Security.providers.find{ it.getName() == 'BC' }
StandardPBEByteEncryptor fromEncryptor = new StandardPBEByteEncryptor()
fromEncryptor.setPassword(ENCRYPTION_PASSWORD)
fromEncryptor.setProvider(bcProvider)
fromEncryptor.setAlgorithm(fromAlgorithm)
fromEncryptor.initialize()
StandardPBEByteEncryptor toEncryptor = new StandardPBEByteEncryptor()
toEncryptor.setPassword(ENCRYPTION_PASSWORD)
toEncryptor.setProvider(bcProvider)
toEncryptor.setAlgorithm(toAlgorithm)
toEncryptor.initialize()
512.times{
String testString = ''.padRight(it, 'A')
assert testString.length() == it
byte[] testArray = testString.toCharArray()
byte[] fromEncrypted = fromEncryptor.encrypt(testArray)
byte[] fromDecrypted = fromEncryptor.decrypt(fromEncrypted)
byte[] toEncrypted = fromEncryptor.encrypt(testArray)
byte[] toDecrypted = fromEncryptor.decrypt(toEncrypted)
assert fromEncrypted != toEncrypted
assert fromEncrypted.length == toEncrypted.length
println "${it} ==> ${toEncrypted.length}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment