【问题标题】:Can't restore privateKey RSA无法恢复 privateKey RSA
【发布时间】:2014-05-25 16:44:52
【问题描述】:

我有这个关于 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 无效)。我尝试了很多方法但仍然相同,有人可以指导我我的错误在哪里吗?提前致谢!!!

【问题讨论】:

    标签: java security encryption rsa private-key


    【解决方案1】:

    以下几行:

    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 或更高版本)。

    【讨论】:

      猜你喜欢
      • 2018-12-09
      • 2017-11-24
      • 2013-12-06
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 2012-07-05
      相关资源
      最近更新 更多