Created
February 6, 2015 18:20
-
-
Save vinothchandar/4b2a86b196285b819158 to your computer and use it in GitHub Desktop.
Assymmetric Encryption Speed
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
import javax.crypto.Cipher; | |
import java.security.*; | |
import java.util.Random; | |
/** | |
* Created by vinoth on 2/6/15. | |
*/ | |
abstract class EncryptionProvider { | |
PublicKey publicKey; | |
PrivateKey privateKey; | |
Random rand; | |
static final int DATA_SIZE = 200; | |
EncryptionProvider(PublicKey publicKey, PrivateKey privateKey) { | |
this.publicKey = publicKey; | |
this.privateKey = privateKey; | |
this.rand = new Random(); | |
} | |
abstract byte[] encrypt() throws Exception ; | |
abstract byte[] decrypt(byte[] encrypted) throws Exception ; | |
} | |
class RSAEncryptionProvider extends EncryptionProvider{ | |
RSAEncryptionProvider(PublicKey publicKey, PrivateKey privateKey) { | |
super(publicKey, privateKey); | |
} | |
@Override byte[] encrypt() throws Exception { | |
byte[] d = new byte[DATA_SIZE]; | |
rand.nextBytes(d); | |
return AssymetricEncryptionBenchmark.encrypt(d, this.publicKey); | |
} | |
@Override byte[] decrypt(byte[] encrypted) throws Exception { | |
return AssymetricEncryptionBenchmark.decrypt(encrypted, this.privateKey); | |
} | |
} | |
class PassthruEncryptionProvider extends EncryptionProvider{ | |
PassthruEncryptionProvider(PublicKey publicKey, PrivateKey privateKey) { | |
super(publicKey, privateKey); | |
} | |
@Override byte[] encrypt() throws Exception { | |
byte[] d = new byte[DATA_SIZE]; | |
rand.nextBytes(d); | |
return d; | |
} | |
@Override byte[] decrypt(byte[] encrypted) throws Exception { | |
return encrypted; | |
} | |
} | |
public class AssymetricEncryptionBenchmark { | |
public static byte[] encrypt(byte[] data, PublicKey key) { | |
byte[] cipherText = null; | |
try { | |
final Cipher cipher = Cipher.getInstance("RSA"); | |
cipher.init(Cipher.ENCRYPT_MODE, key); | |
cipherText = cipher.doFinal(data); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return cipherText; | |
} | |
public static byte[] decrypt(byte[] data, PrivateKey key) { | |
byte[] dectyptedText = null; | |
try { | |
final Cipher cipher = Cipher.getInstance("RSA"); | |
cipher.init(Cipher.DECRYPT_MODE, key); | |
dectyptedText = cipher.doFinal(data); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
return dectyptedText; | |
} | |
public static void main(String[] args) throws Exception { | |
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); | |
SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); | |
keyGen.initialize(2048, random); | |
KeyPair pair = keyGen.generateKeyPair(); | |
PrivateKey priv = pair.getPrivate(); | |
PublicKey pub = pair.getPublic(); | |
EncryptionProvider[] providers = new EncryptionProvider[]{new PassthruEncryptionProvider(pub, priv), new RSAEncryptionProvider(pub, priv)}; | |
long totalRecords = 10000; | |
for (EncryptionProvider provider: providers){ | |
long startNs = System.nanoTime(); | |
for (int d=0; d < totalRecords; d++){ | |
provider.decrypt(provider.encrypt()); | |
} | |
System.out.println(provider.getClass().getName()+": Ns Per record "+ (System.nanoTime()-startNs)/totalRecords); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment