简体   繁体   English

RSA解密打印NULL

[英]RSA Decryption Printing NULL

I believe I am doing this right. 我相信我做对了。 How I see decrypting a file with RSA: 我如何使用RSA解密文件:

  1. Read in each line of the file as a String String读取文件的每一行
  2. Set cipher.init(Cipher.DECRYPT_MODE, privateKey) 设置cipher.init(Cipher.DECRYPT_MODE, privateKey)
  3. Convert String to char[] using Hex.decodeHex(String.toCharArray()) 使用Hex.decodeHex(String.toCharArray())将String转换为char[]
  4. Lastly do cipher.doFinal(x) 最后做cipher.doFinal(x)

Does this sound right? 听起来对吗? I'm doing this but it is not working and DecryptedFile.txt is just 2 lines of "null". 我正在执行此操作,但是它不起作用, DecryptedFile.txt仅是两行“空”。

I'm able to encrypt using pretty much the same exact process but with cipher.init(Cipher.ENCRYPT_MODE, publicKey) obviously. 我能够使用几乎相同的确切过程进行加密,但显然可以使用cipher.init(Cipher.ENCRYPT_MODE, publicKey)进行加密。

Here's my code 这是我的代码

try {
        BufferedReader inStream = new BufferedReader (new FileReader(cryptoFile));

        int k = 0;

        fileContents.add(inStream.readLine());

        while(fileContents.get(k) != null) {
            k++;
            fileContents.add(inStream.readLine());
        }

        inStream.close();

        try {

            PrivateKey privateKey = kp.getPrivate();
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);

            int j = 0;

            while(fileContents.get(j) != null) {

                String text = fileContents.get(j);

                try {                        
                    x = Hex.decodeHex(text.toCharArray());
                    y = cipher.doFinal(x);
                } catch (DecoderException ex) {
                    Logger.getLogger(Crypto.class.getName()).log(Level.SEVERE, null, ex);
                }

                try (PrintWriter file = new PrintWriter(
                        new BufferedWriter(
                        new FileWriter("DecryptedFile.txt", true)))) {
                    file.println(y);
                } catch (IOException e) {
                    System.err.println("IOERROR: " + e.getMessage() + "\n");
                }

                j++;
            }

        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
            Logger.getLogger(Crypto.class.getName()).log(Level.SEVERE, null, ex);
        }

    } catch (FileNotFoundException e) {
        System.err.println("IOERROR: File NOT Found: " + cryptoFile + "\n");
    } catch ( IOException e ) {
        System.err.println("IOERROR: " + e.getMessage() + "\n");
    } finally {
        messagePane.setText(messagePane.getText() + "\n\n"
                + cryptoFile + "is done being decrypted.");
        messagePane.setText(messagePane.getText() + "\n"
                + "Decrypted file saved to \'DecryptedFile.txt\'.");

        cryptoFile = "";
        pathTextField.setText(cryptoFile);
        encryptButton.setEnabled(false);
        decryptButton.setEnabled(false);

    }

You're using the cipher on a character array derived from a String derived from FileContents - this is probably screwing up its encoding. 您正在对从FileContents派生的String派生的字符数组使用密码-这可能会破坏其编码。 Instead, read a byte array from the file and use this as input to the cipher. 而是从文件中读取字节数组,并将其用作密码的输入。

It's a good idea to pad the input if necessary - use Cipher.getInstance("RSA/ECB/PKCS1Padding") 如有必要,最好填充输入-使用Cipher.getInstance("RSA/ECB/PKCS1Padding")

Always specify the character encoding (eg str.getBytes("UTF-8") ) when converting a string to bytes and vice versa - different JVMs use different default character encodings. 在将字符串转换为字节时始终指定字符编码(例如str.getBytes("UTF-8") ),反之亦然-不同的JVM使用不同的默认字符编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM