using encryption library in java is not stable in speed the first round take a lot time than the others and after some time the time needed to perform encryption is stable why is this and how to reach stability from the beginning, if some intail code is needed i can do it but i do know need the the call of encrypt method to take that amound of time please see the code and the final line that contain results.
This class contain static methods to encrypt/ decrypt
package karp.generalutil.common;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Encryptor {
/**
* @param args
*/
static Cipher eCipher,dCipher;
public Encryptor()
{
try {
eCipher=Cipher.getInstance("AES");
dCipher=Cipher.getInstance("AES");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/// for test
public static void main (String args[])
{
try {
KeyGenerator aes;
aes = KeyGenerator.getInstance("AES");
aes.init(128);
SecretKey key = aes.generateKey();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String encrypt(String clearText,String keyString) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException
{
SecretKey key=loadKey(keyString);
String encryptedText;
eCipher.init(Cipher.ENCRYPT_MODE,key );
encryptedText=new String(eCipher.doFinal(clearText.getBytes()));
return encryptedText;
}
public static String decrypt(String encryptedText,String keyString) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException
{
SecretKey key=loadKey(keyString);
String clearText;
Cipher dCipher=Cipher.getInstance("AES");
dCipher.init(Cipher.DECRYPT_MODE,key);
clearText=new String(dCipher.doFinal(encryptedText.getBytes()));
return clearText;
}
public static byte[] encrypt(byte [] clearByteArray,String keyString)throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException
{
SecretKey key=loadKey(keyString);
byte[] encryptedByteArray;
Cipher eCipher=Cipher.getInstance("AES");
eCipher.init(Cipher.ENCRYPT_MODE,key );
encryptedByteArray=eCipher.doFinal(clearByteArray);
return encryptedByteArray;
}
public static byte[] decrypt(byte [] encryptedByteArray,String keyString)throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException
{
SecretKey key=loadKey(keyString);
byte[] clearByteArray;
Cipher dCipher=Cipher.getInstance("AES");
dCipher.init(Cipher.DECRYPT_MODE,key );
clearByteArray=dCipher.doFinal(encryptedByteArray);
return clearByteArray;
}
public static SecretKey loadKey(String keyString) {
byte[] encoded = keyString.getBytes();
SecretKey key = new SecretKeySpec(encoded, "AES");
return key;
}
}
This the test class
import java.io.ByteArrayInputStream;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.util.Date;
public class tesst {
/**
* @param args
*/
public static void main(String[] args) {
try {
long y1,y2;
y1=System.nanoTime();
Encryptor e=new Encryptor();
String keyString="”{¶¹û¼«I?q-׫л•";
e.encrypt(new byte[31], "”{¶¹û¼«I?q-׫л•");
y2=System.nanoTime()-y1;
System.out.println("ini:"+y2);
for(int i=0;i<100000;i++)
{ byte [] x=new byte [31];
y1=System.nanoTime();
byte [] y=e.encrypt(x, "”{¶¹û¼«I?q-׫л•");
y2=System.nanoTime()-y1;
System.out.println(y2);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The first loop take about 193650106 ns the second loop take about 126150 ns after 150 loop the time to encrypt is decreased to 11546 ns
To perform a meaningful benchmark, use the Java Microbenchmark Harness (JMH) tool.
Java virtual machines profile running code for some time before optimizing it for the underlying hardware architecture. This causes all code, not just AES encryption, to run faster after some time.
With regard to your encryption code itself, it's got a few problems. (They won't affect speed, but it doesn't matter how fast buggy code is.)
First, you should be explicit about the mode and padding your ciphers are using. Normally, instead of "AES"
, you should use something like "AES/CBC/PKCS5Padding"
. That way, when you inter-operate with another application, you'll be able to tell them what algorithms you used.
Second, the result of encryption looks like random bytes. The chances of those bytes being a valid encoding of a character string are almost zero. So, when you simply create a new String
object from your cipher text, you are likely to lose some information because the character decoder will replace any unrecognized bytes with the � character. Instead, use something like Base-64–encoding to encode your binary data to ASCII characters. The same applies to your "key string."