无法恢复privateKey RSA

时间:2014-05-25 16:44:52

标签: java security encryption rsa private-key

我有关于java加密的这个问题。 我从用户那里获取密码,然后我产生一个私钥和一个公钥。从公钥我创建密码,然后我存储私钥和密码。

然后从我的第二个应用程序中,我再次从用户,密码文件和私钥中读取密码,然后我尝试将密码与密码和私钥的解密相匹配。


我的第一个申请:

private static byte[] encrypt(byte[] inpBytes, PublicKey key,
String xform) throws Exception {
 Cipher cipher = Cipher.getInstance(xform);
 cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(inpBytes);
}


 String xform = "RSA";
 // Generate a key-pair
 KeyPairGenerator kpg = null;
        try {
            kpg = KeyPairGenerator.getInstance("RSA");
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Efarmogi_1.class.getName()).log(Level.SEVERE, null, ex);
        }
 kpg.initialize(512); // 512 is the keysize.
 KeyPair kp = kpg.generateKeyPair();
 /create public and private key
 PublicKey pubk = kp.getPublic();
 PrivateKey prvk = kp.getPrivate();

 //password from user 
 String password = T_Password.getText();

byte[] dataBytes = password.getBytes();
//create cipher
byte[] encBytes = null;
        try {
            encBytes = encrypt(dataBytes, pubk, xform);
        } catch (Exception ex) {
            Logger.getLogger(Efarmogi_1.class.getName()).log(Level.SEVERE, null, ex);
        }

//storing
//cipher
  FileOutputStream cipher = null;
        try {
            cipher = new FileOutputStream( "Xrhstes\\"+T_Username.getText()+"\\hash_"+T_Username.getText());

            cipher.write(encBytes);//write with bytes

            cipher.close();
        } catch (IOException ex) {
            Logger.getLogger(Efarmogi_1.class.getName()).log(Level.SEVERE, null, ex);
        }

//private key
 byte[] key2 = prvk.getEncoded();
            FileOutputStream keyfos2 = null;
        try {
            keyfos2 = new FileOutputStream("Xrhstes\\"+T_Username.getText()+"\\private_"+ T_Username.getText()+".pem");

            keyfos2.write(key2);

            keyfos2.close();

这是第二个申请:

   private static byte[] decrypt(byte[] inpBytes, PrivateKey key,String xform) throws Exception{
Cipher cipher = Cipher.getInstance(xform);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(inpBytes);
}

//fetch private key           
byte[] prvk1 = new byte[(int)new File("C:\\...\\"+T_Username.getText()+"\\private_"+ T_Username.getText()).length()]; 

//make it from bytes to private key
  KeyFactory kf= KeyFactory.getInstance("RSA");
  PrivateKey prvk=kf.generatePrivate(new PKCS8EncodedKeySpec(prvk1));

//fetch cipher
 byte[] encBytes = new byte[(int)new File("C:\\...\\"+T_Username.getText()+"\\hash_"+ T_Username.getText()).length()]; 

//decrypt with our password given from user
    String xform = "RSA";

         byte[] decBytes = decrypt(encBytes,prvk, xform);

        boolean expected = java.util.Arrays.equals(password, decBytes);
        System.out.println("Test " + (expected ? "SUCCEEDED!" : "FAILED!"));

我的问题是当我尝试重新转换保存的字节时,回到privateKey我得到多个错误,密钥类型无效(问题从解码PKCS8EncodedKeySpec开始,注意到KeySpec无效)。我尝试了很多方法,但仍然是相同的有人可以指导我,我的错误在哪里?提前致谢!!!

1 个答案:

答案 0 :(得分:0)

使用以下行:

byte[] prvk1 = new byte[(int)new File("C:\\...\\"+T_Username.getText()+"\\private_"+ T_Username.getText()).length()];

byte[] encBytes = new byte[(int)new File("C:\\...\\"+T_Username.getText()+"\\hash_"+ T_Username.getText()).length()];

实际上你创建了两个空字节数组。您只能使用File.length()指示数组的大小,这显然是不正确的。实际读取文件后再试一次,例如使用readAllBytes方法(需要Java 8或更高版本)。