【问题标题】:incomplete RSA decryption with large XML file type - JAVA具有大型 XML 文件类型的不完整 RSA 解密 - JAVA
【发布时间】:2014-10-29 04:36:32
【问题描述】:

我在解密 XML 类型时遇到问题,我的文件返回不完整的数据算法和稀有符号。

public File decryptFile(File fileInput, X509Certificate certificate) throws BadPaddingException, Exception { try (DataInputStream dis = new DataInputStream(new FileInputStream(fileInput))) { byte[] encryptedKeyBytes = new byte[dis.readInt()]; dis.readFully(encryptedKeyBytes); PublicKey publicKey = certificate.getPublicKey(); rsaCipher.init(Cipher.DECRYPT_MODE, publicKey); byte[] rijndaelKeyBytes = rsaCipher.doFinal(encryptedKeyBytes); SecretKey rijndaelKey = new SecretKeySpec(rijndaelKeyBytes, "Rijndael"); byte[] iv = new byte[16]; dis.read(iv); IvParameterSpec spec = new IvParameterSpec(iv); Cipher cipher = Cipher.getInstance("Rijndael/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, rijndaelKey, spec); try (CipherInputStream cis = new CipherInputStream(dis, cipher)) { try (FileOutputStream fos = new FileOutputStream(fileInput.getAbsolutePath() + ".xml")) { byte[] data = new byte[16]; int theByte; while ((theByte = cis.read(data)) != -1) { System.out.print(new String(data)); fos.write(data, 0, theByte); } System.out.println("\n\n"); } } } return new File(fileInput.getAbsolutePath() + ".xml"); }

这段代码将数据返回给我

</ctaAbonBenef><distPago>00000</distPago><item>00000</item><pagoPoder>N</p�|���[�[W�Z�5��Q�

我认为这与UTF-8有关,但我无法解决。

现在我也可以相信是用的加密算法了,我留下以防万一。

public static void generateFileEncrypt(File fileInput, PrivateKey privateKey, String folderSave) throws Exception { String fileOutput = folderSave + "\" + fileInput.getName() + ENCRYPTED_FILENAME_SUFFIX; DataOutputStream output = new DataOutputStream(new FileOutputStream(fileOutput)); Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); rsaCipher.init(Cipher.ENCRYPT_MODE, privateKey); KeyGenerator rijndaelKeyGenerator = KeyGenerator.getInstance("Rijndael"); rijndaelKeyGenerator.init(128); Key rijndaelKey = rijndaelKeyGenerator.generateKey(); byte[] encodedKeyBytes = rsaCipher.doFinal(rijndaelKey.getEncoded()); output.writeInt(encodedKeyBytes.length); output.write(encodedKeyBytes); SecureRandom random = new SecureRandom(); byte[] iv = new byte[16]; random.nextBytes(iv); output.write(iv); IvParameterSpec spec = new IvParameterSpec(iv); Cipher symmetricCipher = Cipher.getInstance("Rijndael/CBC/PKCS5Padding"); symmetricCipher.init(Cipher.ENCRYPT_MODE, rijndaelKey, spec); try ( CipherOutputStream cos = new CipherOutputStream(output, symmetricCipher); FileInputStream fis = new FileInputStream(fileInput)) { int theByte; byte[] data = new byte[16]; while ((theByte = fis.read(data)) != -1) { System.out.print(new String(data)); cos.write(data, 0, theByte); } System.out.println("\n\n"); cos.flush(); } }

提前致谢。

【问题讨论】:

    标签: java xml security encryption encryption-asymmetric


    【解决方案1】:

    我还没有消化你所有的代码;当看到你试图用公钥解密并用私钥加密时,我停下了脚步。这有点像数字签名,但你的填充将是错误的,如果这是你真正想要做的,你应该使用 Signature 类。

    公钥用于加密或验证数字签名。使用私钥解密,看看能否解决您的问题。


    你还是做错了。如果密钥不是私有的,请不要将其称为“加密”。

    但无论如何,我认为打印到标准输出看起来是错误的,因为您正在将整个缓冲区转换为文本。最后一个块可能会被填充,因此它不会解码为有效文本——它是填充;它不是输入文件的一部分,并且您没有将其写入解密文件,但您正在打印它。

    改用公钥加密,用私钥解密,然后把你的打印改成这样:

    System.out.print(new String(data, 0, theByte));
    

    最好指定数据的字符集(可能是 UTF-8,因为它是 XML 的默认值)。

    【讨论】:

    • 我想要做的是用私钥加密文件,正如您在方法 generateFileEncrypt 中看到的那样,但用公钥解密文件,它会返回带有奇数符号的数据。
    • @rodrixd 查看我的更新。并停止在无用的数学上浪费周期。
    【解决方案2】:

    我认为你应该做相反的事情。用公钥加密,用私钥解密..

    【讨论】:

    • 一个 RSA 密钥对可以双向加密。
    • @christopher 加密提供机密性。如果您使用公钥,则没有机密性,因此它不是“加密”,也不是 RSA。这只是一个无用的模幂运算。
    • 但是这里的问题是软件解密不正确。从数学上讲,这仍然应该有效。这与问题无关。我添加了这条评论,因为如果回答者认为使用私钥加密会导致这种情况,那他们就错了。如果他们没有,那么他们的答案不应该是一个答案。除此之外,我同意你所说的一切。不过,也有使用私钥加密的用例,例如身份验证。
    猜你喜欢
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 2012-04-17
    • 2021-05-09
    • 2012-08-30
    • 1970-01-01
    相关资源
    最近更新 更多