使用RSA解密加密的字符串

时间:2014-03-20 15:18:18

标签: java encryption cryptography rsa

我试图允许用户输入已经用我的系统加密的数据,但是当我尝试这样做时,我得到一个java.lang.IllegalArgumentException:Null输入缓冲区异常。

当通过加密方法生成密文时,代码运行正常,但是当我尝试手动更改输入时,代码运行不正常。

我要求解密文本的方法

StringBuilder sb = new StringBuilder(10);
    // Decrypt the cipher text using the private key.
    // Decrypt the cipher text using the private key.
    inputStream = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
    final PrivateKey privateKey = (PrivateKey) inputStream.readObject();
    String[] decryptedText = new String[cipherText.length];
    for (int i = 0; i < cipherText.length; i++) {

        decryptedText[i] = decrypt(cipherText[i], privateKey); LINE RSA 217
        sb.append(decryptedText[i]);
        sb.append(" ");
    }
    g = sb.toString();

称为解密方法

byte[] dectyptedText = null;
    try {
        // get an RSA cipher object and print the provider
        final Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        dectyptedText = cipher.doFinal(text); LINE RSA:136

    } catch (InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException ex) {
    }

    return new String(dectyptedText);

我从

获取加密文本的位置
String a = "[B@7fc7efa0 [B@1f4532ea [B@7811b0af [B@5166eebe [B@3cc425b2 [B@7186e848 [B@4ffc0932 ";
        byte[][] ab;
       ab = new byte[a.length()][];
       rsaInstance.setCipherText(null);
        rsaInstance.setCipherText(ab);
        rsaInstance.no(); LINE ENCRRYPTIONFRMAE 721

这是出现的例外情况。

线程中的异常&#34; AWT-EventQueue-0&#34; java.lang.IllegalArgumentException:空输入缓冲区     在javax.crypto.Cipher.doFinal(Cipher.java:2082)     在finalhandin.RSA.decrypt(RSA.java:136)     在finalhandin.RSA.no(RSA.java:217)     在finalhandin.EncryptionFrame.btnDecryptPanelActionPerformed(EncryptionFrame.java:721)

调用加密的方法

   try {
        // Check if the pair of keys are present else generate those.
        if (!areKeysPresent()) {
            // Method generates a pair of keys using the RSA algorithm and stores it
            // in their respective files
            generateKey();
        }

        String[] splited = originalText.split("\\s+");
        StringBuilder sb = new StringBuilder();
        inputStream = null;

        // Encrypt the string using the public key
        inputStream = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
        final PublicKey publicKey = (PublicKey) inputStream.readObject();
        cipherText = new byte[splited.length][];
        for (int i = 0; i < splited.length; i++) {
            cipherText[i] = encrypt(splited[i], publicKey);
            System.out.println(cipherText[i]);
            sb.append(cipherText[i]);
            sb.append(" ");
        }
        encryptedText = sb.toString();

加密方法

  byte[] cipherText = null;
    try {
        // get an RSA cipher object and print the provider
        final Cipher cipher = Cipher.getInstance(ALGORITHM);
        // encrypt the plain text using the public key
        cipher.init(Cipher.ENCRYPT_MODE, key);
        cipherText = cipher.doFinal(text.getBytes());
    } catch (InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) {
    }
    return cipherText;

0 个答案:

没有答案