Java RSA解密

时间:2018-04-25 18:56:41

标签: java encryption rsa

我无法解密某些文字

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。我将得到的另一个错误是填充异常错误。我一直在研究它,但我认为可能在我的代码中等待着让我到处等待。

此外,加密工作正常。我没有检查是否正确,但我打印出长度以确保字符串足够长。解密循环永远不会超过第一个循环

1 个答案:

答案 0 :(得分:0)

您的计划存在多个问题

  • 已经注释 - 使用了错误的密钥(私钥用于解密,公钥用于加密)

  • 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)将padding添加到要加密的值。所以你不能只取任何值并解密它,否则填充不会有效(这在带有一些子串的循环中发生)

前段时间我写了一篇blog about encryption in Java你可能会从

中获得灵感