我正在创建一个生成密钥对的程序,但我有这个错误:
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;
}
}
任何帮助将不胜感激。谢谢。
我已经编辑过了。
答案 0 :(得分:1)
第一个错误:KeyPairGenerator.getInstance(String algorithm)
只能处理以下输入String
:
对于mor infos look 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();
答案 1 :(得分:0)
这一行错了:
<div class="portfolio-first-content">
<h1> Paintings </h1>
<img src="images/painting 2.png" id="main-painting2">
<div class="second-row-images">
<img src="images/painting7.jpg">
<img src="images/painting1.jpg">
</div>
<img src="images/painting6.jpg" id="main-painting3">
<img src="images/painting8.jpg" class="main-painting4">
</div>
您必须提供要使用的算法的名称,例如:
KeyPairGenerator keyGen1 = KeyPairGenerator.getInstance("ALGORITHM");