JavaRSA解密

Java RSA Decryption

我在解密一些文本时遇到问题

public class mainClass {

    private static Cipher cipher;

    private static KeyPairGenerator keyGen;
    private static KeyPair pair;
    private static PrivateKey privateKey;
    private static PublicKey publicKey;

    public static void createKeys() {
        pair = keyGen.generateKeyPair();
        privateKey = pair.getPrivate();
        publicKey = pair.getPublic();
    }

    public static String encryptText(String msg)
            throws NoSuchAlgorithmException, NoSuchPaddingException,
            UnsupportedEncodingException, IllegalBlockSizeException,
            BadPaddingException, InvalidKeyException {
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        return Base64.getEncoder().encodeToString((cipher.doFinal(msg.getBytes("UTF-8"))));
    }

    public static String decryptText(String msg)
            throws InvalidKeyException, UnsupportedEncodingException,
            IllegalBlockSizeException, BadPaddingException {

        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        return new String(cipher.doFinal(Base64.getDecoder().decode(msg)), "UTF-8");
    }

    public static void main(String args[]) throws Exception {
        int keylength = 1024;

        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");

        keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(keylength, random);
        cipher = Cipher.getInstance("RSA");
        createKeys();

        FileInputStream f = new FileInputStream("C:\Users\caleb.baker\Downloads\test100k.db");
        byte[] b = new byte[f.available()];
        int offset = 0;
        String asf = "";
        f.read(b);
        String test = new String(b);
        for (int x = 0; x < b.length; x += 117) {
            try {
                asf += encryptText(test.substring(x, x + 117));
            } catch (StringIndexOutOfBoundsException e) {
                asf += encryptText(test.substring(x));
            }
        }
        String t = asf;
        asf = "";
        for (int x = 0; x < t.length(); x += 117) {
            System.out.println("run");
            try {
                asf += decryptText(test.substring(x, x + 117));
            } catch (StringIndexOutOfBoundsException e) {
                asf += decryptText(test.substring(x));
            }
        }
    }
}

我得到的错误是 线程异常 "main"

java.lang.IllegalArgumentException: Last unit does not have enough valid bits

at java.util.Base64$Decoder.decode0(Unknown Source)

at java.util.Base64$Decoder.decode(Unknown Source)

at java.util.Base64$Decoder.decode(Unknown Source)

at mainClass.decryptText(mainClass.java:60)

at mainClass.main(mainClass.java:162)

我对加密还很陌生。对称 AES 工作得很好,我知道它更快,但出于我自己的好奇心,我想为 RSA 计时。我将得到的另一个错误是错误的填充异常。我一直在研究它,但我认为它可能在我的代码中某个地方等着我。

加密也很好。我没有检查它是否正确,但我确实打印了长度以确保字符串足够长。解密循环永远不会超过第一个循环

您的程序存在多个问题

  • 如前所述-使用了错误的密钥(私钥用于解密,public密钥用于加密)

  • RSA 仅用于加密有限数量的数据,而不是任何长度的文本。可以这样使用,但在某些情况下可能不安全

  • Last unit does not have enough valid bits 表示 base64 编码值无效。解码base64就是将4个字节的6位编码转换成3个字节的8位。因此解码部分的长度必须是 4 的倍数(1177 不是),加密循环也是如此,只是连接多个 base64 字符串不一定会产生有效的 b64 值。

  • 我没明白程序中循环的目的。恕我直言,它太复杂了,你应该保持简单。你想达到什么目的?

  • Another error I will get is a bad padding exception. RSA(默认密码 RSA/ECB/pkcs1padding)向要加密的值添加填充。所以你不能只取任何值并解密它,否则填充将无效(这发生在带有某些子字符串的循环中)

前段时间我写了一篇blog about encryption in Java你可能会从中得到一些启发