자바에서 암호화 라이브러리를 사용하는 속도가 안정적이지 않습니다. 첫 번째 라운드는 다른 것보다 많은 시간이 걸리고 잠시 후에 암호화를 수행하는 데 필요한 시간이 안정되어 있습니다. 일부 intail 코드가 필요한 경우 시작, 나는 그것을 할 수 있지만 시간이 그 시간을 amound 걸릴 암호화 메서드의 호출이 필요한 결과를 포함하는 코드와 최종 줄을 참조하십시오.자바에서 AES의 성능을 향상
이 클래스는 이것을 첫 번째 루프는 약 193,650,106 NS 에게 2 루프을
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();
}
}
}
테스트 클래스 150 루프 후 약 126,150 NS에게 걸릴/
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;
}
}
해독 암호화 정적 메소드를 포함 암호화 시간이 11546 ns로 단축됩니다.
죄송합니다. 1 회 투표를 할 수 없으므로, 하드웨어 (Java가 아닌), 네트워크 카드 (예 : 네트워크 카드)가 11000 ns 인 경우 내 메소드에 필요한 시간을 말할 수 있습니다.나는 인증을 추가하고 새로운 프로토콜의 성능을 연구 할 필요가있는 ARP 개선을 연구 중이므로 최적화를 기다리는 동안 새로운 프로토콜에 많은 오버 헤드가 추가됩니다. 이 문지르기를 이클립스를 사용하여 낮추는 데 도움이 될 수 있도록 전체 문서를 읽을 필요가 없습니다. –
이 임계 값을 낮춰야합니까? "-XX : CompileThreshold" –
@MohammedFalha 내 대답의 시작 부분에있는 업데이트를 참조하십시오. 테스트에서 올바른 접근법을 취하는 것 같지 않습니다. – erickson