【发布时间】:2015-02-04 05:32:24
【问题描述】:
我用 puttygen 生成了一个公共和私有代码,私钥导出为 openssl,pey 的名称是 public_key.der , private_key.pem 但是当我尝试使用 java 对其进行加密时,我收到此错误:
java.io.FileNotFoundException: public_key.der
代码是:
public static String RSAPublicEncryptuion(String text){
DataInputStream dis = null;
try {
File pubKeyFile = new File("public_key.der");
dis = new DataInputStream(new FileInputStream(pubKeyFile));
byte[] keyBytes = new byte[(int) pubKeyFile.length()];
dis.readFully(keyBytes);
dis.close();
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKey publicKey = (RSAPublicKey)keyFactory.generatePublic(keySpec);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
String textoEncryptado = new String(cipher.doFinal(text.getBytes()), "UTF-8");
return textoEncryptado;
} catch (FileNotFoundException ex) {
Logger.getLogger(RSAEncrypt.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException | InvalidKeyException ex) {
Logger.getLogger(RSAEncrypt.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalBlockSizeException | BadPaddingException ex) {
Logger.getLogger(RSAEncrypt.class.getName()).log(Level.SEVERE, null, ex);
}
return "Error";
}
public_key 与这个类(testras.ras)在同一个包中,我做错了什么? 谢谢大家! 对不起我的英语不好
【问题讨论】:
-
public_key.der 文件在哪里?
-
与.java在同一个包中,当我编译它时,它会在.class所在的同一个地方
标签: java encryption rsa