KeyPairGenerator 不可用
KeyPairGenerator not available
我正在创建一个程序来生成密钥对,但出现以下错误:
Exception in thread "main" java.lang.NullPointerException
at generateAESKey.main(generateAESKey.java:43)
这就是我目前的工作。
key.getEncoded());
未读。
public class generateAESKey {
public static void main(String[] args) throws Exception {
generateAESKey generatekey = new generateAESKey();
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
SecretKey skey = keyGen.generateKey();
byte[] raw = skey.getEncoded();
FileOutputStream fos2 = new FileOutputStream("C:\Users\win8.1\Desktop\AES\test.txt");
fos2.write(raw);
fos2.close();
KeyPairGenerator keyGen1 = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen1.initialize(1024, random);
KeyPair generatedKeyPair = keyGen1.genKeyPair();
savePublicKey(generatedKeyPair.getPublic(), "C:\Users\win8.1\Desktop\AES\public");
savePrivateKey(generatedKeyPair.getPrivate(), "C:\Users\win8.1\Desktop\AES\private");
PublicKey key = null;
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
key.getEncoded());
FileOutputStream fos3 = new FileOutputStream("C:\Users\win8.1\Desktop\AES\public.txt");
fos3.write(x509EncodedKeySpec.getEncoded());
fos3.close();
PrivateKey key1 = null;
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
key1.getEncoded());
FileOutputStream fos4 = new FileOutputStream("C:\Users\win8.1\Desktop\AES\private.txt");
fos4.write(pkcs8EncodedKeySpec.getEncoded());
fos4.close();
System.out.println("SAVE");
}
private static void savePrivateKey(PrivateKey private1, String string) {
// TODO Auto-generated method stub
}
private static void savePublicKey(PublicKey public1, String string) {
// TODO Auto-generated method stub
}
public SecretKeySpec getKeySpec() throws IOException, NoSuchAlgorithmException {
byte[] bytes = new byte[16];
File f = new File("aes.key");
SecretKey key = null;
SecretKeySpec spec = null;
if (f.exists()) {
new FileInputStream(f).read(bytes);
} else {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(256); //128 kalagay
key = kgen.generateKey();
bytes = key.getEncoded();
new FileOutputStream(f).write(bytes);
}
spec = new SecretKeySpec(bytes,"AES");
return spec;
}
}
任何帮助将不胜感激。谢谢。
我已经编辑过了。
这一行是错误的:
KeyPairGenerator keyGen1 = KeyPairGenerator.getInstance("ALGORITHM");
您必须提供要使用的算法的名称,例如:
KeyPairGenerator keyGen1 = KeyPairGenerator.getInstance("RSA");
第一个错误: KeyPairGenerator.getInstance(String algorithm)
只能处理以下输入 String
s:
- 迪菲赫尔曼
- 动态搜索广告
- RSA
- EC
更多信息请看in the docs
可能来电
KeyPairGenerator.getInstance("RSA");
第二个错误 (nullptr):
你必须创建一个PublicKey
!你正在做 PublicKey key = null;
然后你跌倒:
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
key.getEncoded());//key is null so nullpointer will occur
您必须为 key
变量赋值。我假设你想使用你创建的 PublicKey
所以这样做:
PublicKey key = generatedKeyPair.getPublic();
我正在创建一个程序来生成密钥对,但出现以下错误:
Exception in thread "main" java.lang.NullPointerException
at generateAESKey.main(generateAESKey.java:43)
这就是我目前的工作。
key.getEncoded());
未读。
public class generateAESKey {
public static void main(String[] args) throws Exception {
generateAESKey generatekey = new generateAESKey();
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
SecretKey skey = keyGen.generateKey();
byte[] raw = skey.getEncoded();
FileOutputStream fos2 = new FileOutputStream("C:\Users\win8.1\Desktop\AES\test.txt");
fos2.write(raw);
fos2.close();
KeyPairGenerator keyGen1 = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen1.initialize(1024, random);
KeyPair generatedKeyPair = keyGen1.genKeyPair();
savePublicKey(generatedKeyPair.getPublic(), "C:\Users\win8.1\Desktop\AES\public");
savePrivateKey(generatedKeyPair.getPrivate(), "C:\Users\win8.1\Desktop\AES\private");
PublicKey key = null;
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
key.getEncoded());
FileOutputStream fos3 = new FileOutputStream("C:\Users\win8.1\Desktop\AES\public.txt");
fos3.write(x509EncodedKeySpec.getEncoded());
fos3.close();
PrivateKey key1 = null;
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
key1.getEncoded());
FileOutputStream fos4 = new FileOutputStream("C:\Users\win8.1\Desktop\AES\private.txt");
fos4.write(pkcs8EncodedKeySpec.getEncoded());
fos4.close();
System.out.println("SAVE");
}
private static void savePrivateKey(PrivateKey private1, String string) {
// TODO Auto-generated method stub
}
private static void savePublicKey(PublicKey public1, String string) {
// TODO Auto-generated method stub
}
public SecretKeySpec getKeySpec() throws IOException, NoSuchAlgorithmException {
byte[] bytes = new byte[16];
File f = new File("aes.key");
SecretKey key = null;
SecretKeySpec spec = null;
if (f.exists()) {
new FileInputStream(f).read(bytes);
} else {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(256); //128 kalagay
key = kgen.generateKey();
bytes = key.getEncoded();
new FileOutputStream(f).write(bytes);
}
spec = new SecretKeySpec(bytes,"AES");
return spec;
}
}
任何帮助将不胜感激。谢谢。
我已经编辑过了。
这一行是错误的:
KeyPairGenerator keyGen1 = KeyPairGenerator.getInstance("ALGORITHM");
您必须提供要使用的算法的名称,例如:
KeyPairGenerator keyGen1 = KeyPairGenerator.getInstance("RSA");
第一个错误: KeyPairGenerator.getInstance(String algorithm)
只能处理以下输入 String
s:
- 迪菲赫尔曼
- 动态搜索广告
- RSA
- EC
更多信息请看in the docs
可能来电
KeyPairGenerator.getInstance("RSA");
第二个错误 (nullptr):
你必须创建一个PublicKey
!你正在做 PublicKey key = null;
然后你跌倒:
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
key.getEncoded());//key is null so nullpointer will occur
您必须为 key
变量赋值。我假设你想使用你创建的 PublicKey
所以这样做:
PublicKey key = generatedKeyPair.getPublic();