1

I want to decrpyt the encrypted message digest. i hav this code in my java program:

 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.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
Binay
  • 149
  • 1
  • 3
  • 12
  • Your function name talks about signatues, and the code uses RSA encryption. That doesn't fit together. To sign with RSA, you need a scheme like RSA-PSS. – CodesInChaos Sep 09 '12 at 17:32

1 Answers1

0

Block ciphers, such as RSA, can only encrypt no more than blockSize bytes. 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. The same applies to decryption.

Tadas S
  • 1,955
  • 19
  • 33
  • 2
    I wouldn't call RSA a block cipher, even if it operates on blocks. It's almost never a good idea to split a message into blocks, and apply RSA to each of them. – CodesInChaos Sep 09 '12 at 17:31