我正在尝试在 JDK 1.6.0_35 中使用 SHA1 和私钥进行加密。
但我得到以下异常:
线程“主”java.security.NoSuchAlgorithmException 中的异常:找不到任何支持 SHA1 的提供程序
相同的代码适用于 AES。这是代码:
public class ExecuteEncryptDecryptSample {
private static String method="SHA1";
public static SecretKeySpec getKeySpec() throws IOException, NoSuchAlgorithmException {
byte[] bytes = new byte[16];
File f = new File("sample_aes_key");
SecretKey key = null;
SecretKeySpec spec = null;
if (f.exists()) {
new FileInputStream(f).read(bytes);
} else {
//KeyGenerator kgen = KeyGenerator.getInstance("SHA1");//PBKDF2WithHmacSHA1
KeyGenerator kgen = KeyGenerator.getInstance(method);
kgen.init(256);
key = kgen.generateKey();
bytes = key.getEncoded();
new FileOutputStream(f).write(bytes);
}
spec = new SecretKeySpec(bytes,method);
return spec;
}
public static void encrypt(String text) throws Exception {
SecretKeySpec spec = getKeySpec();
Cipher cipher = Cipher.getInstance(method);
cipher.init(Cipher.ENCRYPT_MODE, spec);
BASE64Encoder enc = new BASE64Encoder();
System.out.println(enc.encode(cipher.doFinal(text.getBytes())));
}
public static void main(String[] args) throws Exception {
String text = "1234000156237828282873773";
//Security security;
//security.getProviders();
System.out.println();
encrypt(text);
}
}
jdk 6中是否有SHA1的提供者..?
任何帮助将不胜感激。
谢谢。