我想在HotSpot编译器中查看硬件aes-ni指令。我正在使用OpenJDK版本“ 1.8.0_191”
我为此目的编写了一个Java程序,并使用以下命令运行它:
java -XX:+UnlockDiagnosticVMOptions -XX:+UseAES -XX:+UseAESIntrinsics -XX:+PrintAssembly AES > assembly.txt
但是我看不到硬件aes-ni(例如aesenc)说明。
https://bugs.openjdk.java.net/browse/JDK-7184394说jdk支持aes-ni指令。
我确保该方法正在编译并启用如下所示的+ XX:PrintIntrinsics:
@ 41 com.sun.crypto.provider.AESCrypt::decryptBlock (3852 bytes) (intrinsic) @ 41
com.sun.crypto.provider.AESCrypt::encryptBlock (953 bytes) (intrinsic) @ 12
java.lang.Math::addExact (26 bytes) (intrinsic) @ 572
java.lang.System::arraycopy (0 bytes) (intrinsic) @ 41
com.sun.crypto.provider.AESCrypt::decryptBlock (3852 bytes) (intrinsic) @ 41
com.sun.crypto.provider.AESCrypt::encryptBlock (953 bytes) (intrinsic) @ 41
com.sun.crypto.provider.AESCrypt::decryptBlock (3852 bytes) (intrinsic) @ 41
com.sun.crypto.provider.AESCrypt::encryptBlock (953 bytes) (intrinsic)
有人可以帮忙吗? 谢谢
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class AES {
private static SecretKeySpec secretKey;
private static byte[] key;
public static void setKey(String myKey)
{
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static String encrypt(String strToEncrypt, String secret)
{
try
{
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
}
catch (Exception e)
{
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}