Created
February 12, 2013 21:05
-
-
Save aaronzirbes/4773392 to your computer and use it in GitHub Desktop.
Finding Fast Encryption Algorythms
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' | |
List<String> testWords = [ | |
'Aenean', | |
'Cras', | |
'Fusce', | |
'Fusce', | |
'Fusce', | |
'Lorem', | |
'Maecenas', | |
'Nam', | |
'Praesent', | |
'Sed', | |
'Vivamus', | |
'a', | |
'ac', | |
'adipiscing', | |
'adipiscing', | |
'aliquam', | |
'aliquet', | |
'amet', | |
'arcu', | |
'at', | |
'auctor', | |
'bibendum', | |
'bibendum', | |
'consectetur', | |
'dapibus', | |
'dictum', | |
'dolor', | |
'dui', | |
'egestas', | |
'eget', | |
'eleifend', | |
'elit', | |
'et', | |
'eu', | |
'facilisis', | |
'fermentum', | |
'fringilla', | |
'iaculis', | |
'id', | |
'in', | |
'interdum', | |
'ipsum', | |
'lacus', | |
'lectus', | |
'leo', | |
'libero', | |
'ligula', | |
'massa', | |
'mauris', | |
'nec', | |
'nisl', | |
'nulla', | |
'nunc', | |
'orci', | |
'pharetra', | |
'posuere', | |
'pretium', | |
'purus', | |
'quam', | |
'quis', | |
'sagittis', | |
'sit', | |
'tristique', | |
'turpis', | |
'ullamcorper', | |
'ultrices', | |
'urna', | |
'ut', | |
'vel', | |
'viverra', | |
'vulputate' | |
] | |
Security.addProvider(new BouncyCastleProvider()); | |
assert SAMPLE_STRING == SAMPLE_STRING | |
byte[] sourceCharArray = SAMPLE_STRING.toCharArray() | |
Map<String, Integer> speed = [:] | |
println 'Provider'.padRight(32) + 'Algorithm' | |
println '-'.padRight(31, '-') + ' '.padRight(64, '-') | |
Security.providers.each{ Provider provider -> | |
String providerName = provider.getName() | |
provider.getServices().each{ Provider.Service service -> | |
String algorithm = service.getAlgorithm() | |
print providerName.padRight(16) + algorithm.padRight(48) | |
try { | |
StandardPBEByteEncryptor encryptor = new StandardPBEByteEncryptor() | |
encryptor.setPassword(ENCRYPTION_PASSWORD) | |
encryptor.setProvider(provider) | |
encryptor.setAlgorithm(algorithm) | |
encryptor.initialize() | |
print 'encrypy: ' | |
byte[] encrypted = encryptor.encrypt(sourceCharArray) | |
print encrypted.size() | |
print ' decrypt: ' | |
byte[] decrypted = encryptor.decrypt(encrypted) | |
assert decrypted == sourceCharArray | |
print 'SUCCESS' | |
long startTime = System.currentTimeMillis() | |
10.times { | |
testWords.each{ word -> | |
byte[] testArray = word.toCharArray() | |
byte[] testEncrypted = encryptor.encrypt(sourceCharArray) | |
byte[] testDecrypted = encryptor.decrypt(encrypted) | |
} | |
} | |
long endTime = System.currentTimeMillis() | |
lap = endTime - startTime | |
speed[algorithm] = lap | |
} catch (NoSuchAlgorithmException ex) { | |
print "Unsupported" | |
} catch (EncryptionInitializationException ex) { | |
print "Unsupported" | |
} catch (Exception ex) { | |
print "FAILED ${ex}" | |
} | |
println '' | |
} | |
} | |
speed.sort{ it.value }.each{ k, v -> | |
println "${v}\t\t${k}" | |
} | |
/** | |
* | |
* Results: | |
8 PBEWITHMD5AND128BITAES-CBC-OPENSSL | |
8 PBEWITHMD5AND192BITAES-CBC-OPENSSL | |
11 PBEWITHMD5AND256BITAES-CBC-OPENSSL | |
389 PBEWithMD5AndDES | |
410 PBEWITHMD5ANDRC2 | |
412 PBEWITHMD5ANDDES | |
651 PBEWITHSHA1ANDRC2 | |
659 PBEWITHSHA1ANDDES | |
664 PBEWITHSHAAND40BITRC4 | |
666 PBEWITHSHAAND128BITRC4 | |
816 PBEWithMD5AndTripleDES | |
1226 PBEWithSHA1AndRC2_40 | |
1312 PBEWITHSHAAND128BITAES-CBC-BC | |
1315 PBEWITHSHAAND40BITRC2-CBC | |
1318 PBEWITHSHAAND128BITRC2-CBC | |
1843 PBEWithSHA1AndDESede | |
1966 PBEWITHSHAAND192BITAES-CBC-BC | |
1968 PBEWITHSHAAND256BITAES-CBC-BC | |
2018 PBEWITHSHAAND3-KEYTRIPLEDES-CBC | |
2018 PBEWITHSHAANDTWOFISH-CBC | |
2030 PBEWITHSHA256AND256BITAES-CBC-BC | |
2036 PBEWITHSHA256AND128BITAES-CBC-BC | |
2041 PBEWITHSHA256AND192BITAES-CBC-BC | |
2485 PBEWITHSHAAND2-KEYTRIPLEDES-CBC | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment