Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

BadPaddingException: Blocktype mismatch: 0

843811Apr 12 2008 — edited Sep 12 2009
Hello,

I Have a Problem:

javax.crypto.BadPaddingException: Blocktype mismatch: 0
at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
at sun.security.rsa.RSAPadding.unpad(Unknown Source)
at com.sun.crypto.provider.RSACipher.a(DashoA13..)*
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13..)*
at javax.crypto.Cipher.doFinal(DashoA13..)*
at P4.DEC0(P4.java:223)
at P4.main(P4.java:23)

Code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;

public class P4 {
	
public static void main(String[] args) {
   
			EN0();
		    EN1();
		 
		    DEC1();
		    DEC0(); // <-- ERROR :/ Why ??? 
}
	
public static KeyPair genKEY()
{
		KeyPair keyPair = null;
		try
		{
			 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
	         keyPairGenerator.initialize(1024);
	         keyPair = keyPairGenerator.generateKeyPair();	
	         
		}catch (Exception e) {}
	 return keyPair;
}
	

public static void EN0()
{
		try{
	     KeyPair keyPair0	= genKEY();
	     PrivateKey privateKey0 = keyPair0.getPrivate();
         PublicKey publicKey0   = keyPair0.getPublic();


         ObjectOutputStream objectOutputStream = new ObjectOutputStream( new FileOutputStream(new File("C:\\priv0.txt")) );
         objectOutputStream.writeObject(privateKey0);
         objectOutputStream.flush();
         objectOutputStream.close();

         objectOutputStream = new ObjectOutputStream( new FileOutputStream(new File("C:\\pub0.txt")) );
         objectOutputStream.writeObject(publicKey0);
         objectOutputStream.flush();
         objectOutputStream.close();
         

         FileInputStream fileInputStream = new FileInputStream("C:\\text.txt");
         byte[] decryptedFileBytes = new byte[fileInputStream.available()];
         fileInputStream.read(decryptedFileBytes);
         fileInputStream.close();
		
         //Encrypt
         Cipher cipher = Cipher.getInstance("RSA");
         cipher.init(Cipher.ENCRYPT_MODE, publicKey0);
      
         // ENCRYPT Pub0
         int decryptedFileBytesChunkLength = 100;
         int numberenOfDecryptedChunks = (decryptedFileBytes.length-1) / decryptedFileBytesChunkLength + 1;

         int encryptedFileBytesChunkLength = 128;
         int encryptedFileBytesLength = numberenOfDecryptedChunks * encryptedFileBytesChunkLength;

         byte[] encryptedFileBytes = new byte[ encryptedFileBytesLength ];

        //Counters
         int decryptedByteIndex = 0;
         int encryptedByteIndex = 0;

       for(int i = 0; i < numberenOfDecryptedChunks; i++)
       {
                   if(i < numberenOfDecryptedChunks - 1)
                   {
                 encryptedByteIndex = encryptedByteIndex + cipher.doFinal(decryptedFileBytes, decryptedByteIndex, decryptedFileBytesChunkLength, encryptedFileBytes, encryptedByteIndex);
                 decryptedByteIndex = decryptedByteIndex + decryptedFileBytesChunkLength;
                   }
                   else
                   {
                         cipher.doFinal(decryptedFileBytes, decryptedByteIndex, decryptedFileBytes.length - decryptedByteIndex, encryptedFileBytes, encryptedByteIndex);
                   }
             }

       FileOutputStream fileOutputStream = new FileOutputStream("C:\\ENCRYPT-0.txt");
       fileOutputStream.write(encryptedFileBytes);
       fileOutputStream.flush();
       fileOutputStream.close();

       System.out.println("length: "+ decryptedFileBytes.length);
       System.out.println("length: "+ encryptedFileBytes.length);
       System.out.println("Encryption done");

		}catch (Exception e) {
			 e.printStackTrace();
		}
}


public static void EN1()
{
		try{
 
         KeyPair keyPair1	= genKEY();
	     PrivateKey privateKey1 = keyPair1.getPrivate();
         PublicKey publicKey1 = keyPair1.getPublic();
 
         //Save keys 
        ObjectOutputStream objectOutputStream1 = new ObjectOutputStream( new FileOutputStream(new File("C:\\priv1.txt")) );
        objectOutputStream1.writeObject(privateKey1);
        objectOutputStream1.flush();
        objectOutputStream1.close();

        objectOutputStream1 = new ObjectOutputStream( new FileOutputStream(new File("C:\\pub1.txt")) );
        objectOutputStream1.writeObject(publicKey1);
        objectOutputStream1.flush();
        objectOutputStream1.close();
		
		
         //Get  text
         FileInputStream fileInputStream = new FileInputStream("C:\\ENCRYPT-0.txt");
         byte[] decryptedFileBytes = new byte[fileInputStream.available()];
         fileInputStream.read(decryptedFileBytes);
         fileInputStream.close();
		
         //Encrypt
         Cipher cipher = Cipher.getInstance("RSA");
         cipher.init(Cipher.ENCRYPT_MODE, publicKey1);
      
         //ENCRYPT Pub1
         int decryptedFileBytesChunkLength = 100;
         int numberenOfDecryptedChunks = (decryptedFileBytes.length-1) / decryptedFileBytesChunkLength + 1;


         int encryptedFileBytesChunkLength = 128;
         int encryptedFileBytesLength = numberenOfDecryptedChunks * encryptedFileBytesChunkLength;


        byte[] encryptedFileBytes = new byte[ encryptedFileBytesLength ];

        int decryptedByteIndex = 0;
        int encryptedByteIndex = 0;

       for(int i = 0; i < numberenOfDecryptedChunks; i++)
       {
                   if(i < numberenOfDecryptedChunks - 1)
                   {
                 encryptedByteIndex = encryptedByteIndex + cipher.doFinal(decryptedFileBytes, decryptedByteIndex, decryptedFileBytesChunkLength, encryptedFileBytes, encryptedByteIndex);
                 decryptedByteIndex = decryptedByteIndex + decryptedFileBytesChunkLength;
                   }
                   else
                   {
                         cipher.doFinal(decryptedFileBytes, decryptedByteIndex, decryptedFileBytes.length - decryptedByteIndex, encryptedFileBytes, encryptedByteIndex);
                   }
      }

       FileOutputStream fileOutputStream = new FileOutputStream("C:\\ENCRYPT-1.txt");
       fileOutputStream.write(encryptedFileBytes);
       fileOutputStream.flush();
       fileOutputStream.close();

       System.out.println("length: "+ decryptedFileBytes.length);
       System.out.println("length: "+ encryptedFileBytes.length);
       System.out.println("Encryption done");

		}catch (Exception e) {
			 e.printStackTrace();
		}
}
	
	

public static void DEC0()
{
		 FileInputStream fileInputStream = null;
		  FileOutputStream fileOutputStream  = null;
		  try
          {
			  fileInputStream = new FileInputStream("C:\\DECRYPT-1.txt");
              byte[] encryptedFileBytes = new byte[fileInputStream.available()];
              fileInputStream.read(encryptedFileBytes);
              fileInputStream.close(); 
              
              ObjectInputStream objectInputStream = new ObjectInputStream( new FileInputStream( new File("C:\\priv0.txt") ) );
              PrivateKey prv0 = (PrivateKey)objectInputStream.readObject();

              Cipher cipher = Cipher.getInstance("RSA");
              cipher.init(Cipher.DECRYPT_MODE, prv0); 

            
              int encryptedFileBytesChunkLength = 128;
              int numberOfEncryptedChunks = encryptedFileBytes.length / encryptedFileBytesChunkLength;
              
              int decryptedFileBytesChunkLength = 100;
              int decryptedFileBytesLength = numberOfEncryptedChunks * encryptedFileBytesChunkLength;
              

              byte[] decryptedFileBytes = new byte[decryptedFileBytesLength];


              int decryptedByteIndex = 0;
              int encryptedByteIndex = 0;
              
              
              for(int i = 0; i < numberOfEncryptedChunks; i++)
              {
                    if( i < numberOfEncryptedChunks -1 )
                    {
                          decryptedByteIndex = decryptedByteIndex + cipher.doFinal(encryptedFileBytes, encryptedByteIndex, encryptedFileBytesChunkLength, decryptedFileBytes, decryptedByteIndex);
                          encryptedByteIndex = encryptedByteIndex + encryptedFileBytesChunkLength;
                    }
                    else
                    {
                          decryptedByteIndex = decryptedByteIndex + cipher.doFinal(encryptedFileBytes, encryptedByteIndex, encryptedFileBytes.length - encryptedByteIndex, decryptedFileBytes, decryptedByteIndex);
                    }
              }

              fileOutputStream = new FileOutputStream("C:\\DECRYPT!!!!.txt");
              fileOutputStream.write(decryptedFileBytes);
              fileOutputStream.flush();
              fileOutputStream.close();
              
              System.out.println("length: "+ encryptedFileBytes.length);
              System.out.println("length: "+ decryptedFileBytes.length);
              System.out.println("Decryption doneV1");
			  
          }catch (Exception e) {
        	  e.printStackTrace();
		}
} 
	

public static void DEC1()
{

		  try
         {
			  FileInputStream fileInputStream = new FileInputStream("C:\\ENCRYPT-1.txt");
             byte[] encryptedFileBytes = new byte[fileInputStream.available()];
             fileInputStream.read(encryptedFileBytes);
             fileInputStream.close(); 
             
             //Get key
             ObjectInputStream objectInputStream = new ObjectInputStream( new FileInputStream( new File("C:\\priv1.txt") ) );
             PrivateKey prv1 = (PrivateKey)objectInputStream.readObject();
             //Decrypt
             Cipher cipher = Cipher.getInstance("RSA");
             cipher.init(Cipher.DECRYPT_MODE, prv1); 

           
             int encryptedFileBytesChunkLength = 128;
             int numberOfEncryptedChunks = encryptedFileBytes.length / encryptedFileBytesChunkLength;
             
             int decryptedFileBytesChunkLength = 100;
             int decryptedFileBytesLength = numberOfEncryptedChunks * encryptedFileBytesChunkLength;
             

             byte[] decryptedFileBytes = new byte[decryptedFileBytesLength];

             //Counters
             int decryptedByteIndex = 0;
             int encryptedByteIndex = 0;
             
             
             for(int i = 0; i < numberOfEncryptedChunks; i++)
             {
                   if( i < numberOfEncryptedChunks -1 )
                   {
                         decryptedByteIndex = decryptedByteIndex + cipher.doFinal(encryptedFileBytes, encryptedByteIndex, encryptedFileBytesChunkLength, decryptedFileBytes, decryptedByteIndex);
                         encryptedByteIndex = encryptedByteIndex + encryptedFileBytesChunkLength;
                   }
                   else
                   {
                         decryptedByteIndex = decryptedByteIndex + cipher.doFinal(encryptedFileBytes, encryptedByteIndex, encryptedFileBytes.length - encryptedByteIndex, decryptedFileBytes, decryptedByteIndex);
                   }
             }


             FileOutputStream fileOutputStream = new FileOutputStream("C:\\DECRYPT-1.txt");
             fileOutputStream.write(decryptedFileBytes);
             fileOutputStream.flush();
             fileOutputStream.close();
             
             System.out.println("length: "+ encryptedFileBytes.length);
             System.out.println("length: "+ decryptedFileBytes.length);
             System.out.println("Decryption doneV1");
			  
         }catch (Exception e) {
       	  e.printStackTrace();
		}
} 	

}
please help me





Edited by: PavelZ on Apr 12, 2008 6:44 AM

Edited by: PavelZ on Apr 12, 2008 6:46 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 10 2009
Added on Apr 12 2008
6 comments
2,473 views