简体   繁体   English

解密加密的消息摘要时出现非法的块大小异常

[英]Illegal block size exception while decrypting the encrypted message digest

I want to decrpyt the encrypted message digest. 我想解密加密的消息摘要。 i hav this code in my java program: 我在我的Java程序中有以下代码:

 String bobSignedMsg = SignedMsg;

    //At the receiving end, Bob extracts the msg 
    // length, the msg text, and the digital
    // signature from the signed msg.
    //Get the message length.
    int MsgLen = Integer.parseInt(bobSignedMsg.trim().substring(bobSignedMsg.length()-6));
    System.out.println(
               "\n12. Bob's calculated msg len: "
                                 + MsgLen);

    //Get the message text.
    String bobMsgText = bobSignedMsg.substring(
                                 0,MsgLen);
    System.out.println(
               "\n13. Bob's extracted msg text: "
                             + bobMsgText);

    //Bob knows that everything following the msg
    // text except for the four characters at the
    // end that indicate the message length is
    // the encoded and encrypted version of the
    // extended digital signature.  He extracts
    // it.
     String bobExtractedSignature =
          bobSignedMsg.substring(
            MsgLen,bobSignedMsg.length() - 6);
    System.out.println(
        "\n14. Bob's extracted extended digital "
                    + "signature: " 
                        + bobExtractedSignature);
    byte[] strtodecrypt=bobExtractedSignature.getBytes();

            byte[] decryptedCardNo = obj.rsaDecrypt(strtodecrypt,PbkeyPath);
         String decryptedform = obj.byteArrayToHexStr(decryptedCardNo);
         System.out.println("After Decryption: "+decryptedform);

In the above lines of code 在上面的代码行中

byte[] decryptedCardNo = obj.rsaDecrypt(strtodecrypt,PbkeyPath);

calls the function: 调用函数:

public byte[] rsaDecrypt(byte[] sampleText,String pbkeypath) {
    PublicKey pubKey = null;
    try {
        pubKey = readKeyFromFile(pbkeypath);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Cipher cipher = null;
    try {
        cipher = Cipher.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        cipher.init(Cipher.DECRYPT_MODE, pubKey);
    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    byte[] cipherData = null;
    try {
        cipherData = cipher.doFinal(sampleText);
        // cipherData = cipher.
    } catch (IllegalBlockSizeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return cipherData;
}

But it gives the following error: 但是它给出了以下错误:

javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes
at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)

I dont understand how to resolve the error for block size exception.....Please if anybody can help me with some ideas it wud be a great help in my project. 我不明白如何解决块大小异常的错误.....如果有人可以帮助我一些想法,它将对我的项目有很大的帮助。

Block ciphers, such as RSA, can only encrypt no more than blockSize bytes. 分组密码(例如RSA)只能加密不超过blockSize字节的字节。 If you want to encrypt an arbitrary large amount of data with the same key, you would split it to parts of blockSize and encrypt each block individually. 如果要使用相同的密钥加密任意数量的数据,可以将其拆分为blockSize的一部分,然后分别加密每个块。 The same applies to decryption. 解密同样如此。

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

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