Created
February 12, 2013 21:52
-
-
Save aaronzirbes/4773776 to your computer and use it in GitHub Desktop.
Ensure encryption algos encrypt to the same field length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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