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!

Urgent Solution is required for Exception :: pad block corrupted

843811Oct 16 2007 — edited Oct 18 2007
Plz refer below code here I m trying to Decrypt a file which is Encrypted_ in DOT NET_ using following Algo,mode and Padding Scheme
ALGORITHM : AES
MODE : ECB
PADDING SCHEME : PKCS7Padding

Now I have to decrypt it in java using third party provider class BouncyCastleProvider for PKCS7Padding but I m not getting success and getting....Exception like

org.bouncycastle.crypto.InvalidCipherTextException: pad block corrupted

The PassPhrase I m using is also same which is used @ the time of Encryption.

I m in need of Urgent solution of this.Is there any other mechanism available then also I m ready to use it.
Ur reply would be appreciated........!!
 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.UnsupportedEncodingException; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
import java.security.Security; 
import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec; 

public class Forum { 
  public static void main(String[] args) throws Exception { 
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); 
     
String passPhrase = "pAsSPhraSe"; 
String encFileName = "EncData.xls.enc";; 
String decFileName = "DecData.xls";; 
FileInputStream encFileIn = null; 
FileOutputStream decFileOut = null; 
File f = null; 
byte[] message; 
     
try { 
f = new File(encFileName); 
encFileIn = new FileInputStream(f); 
decFileOut = new FileOutputStream(decFileName); 

message = new byte[encFileIn.available()]; //Read the encrypted file in from disk 
encFileIn.read(message); 

    byte[] input = message; 
    byte[] keyBytes = passwordToKey (passPhrase); 

    SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); 
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); 

    System.out.println(new String(input)); 


    
    cipher.init(Cipher.DECRYPT_MODE, key); 

    //Decryption pass 
    int ctLength = cipher.update(input, 0, input.length, input, 0); 
    byte[] plainText = new byte[cipher.getOutputSize(ctLength)]; 
    int ptLength = cipher.update(input, 0, ctLength, plainText, 0); 
    ptLength += cipher.doFinal(plainText, ptLength); 
    System.out.println(new String(plainText)); 
    System.out.println(ptLength); 
    decFileOut.write(plainText); 
} catch (Exception e) { 
System.out.println(e); 
} 

  } 
/** From a password, generates a 128-bit key. 
* The method is very simple (simply generating a MD5 hash) 
* and is not advisable to use it; use some another 
* method such as that defined in "Password-Based Encryption" schemes. 
*/ 
public static byte[] passwordToKey (String password) { 
MessageDigest md = null; 
try { 
md = MessageDigest.getInstance ("MD5"); 
} catch (NoSuchAlgorithmException ex) { 
return new byte[0]; 
} 
byte[] passwordBytes = null; 
try { 
passwordBytes = password.getBytes ("ISO-8859-1"); 
} catch (UnsupportedEncodingException ex) { 
passwordBytes = new byte[0]; 
} 
return md.digest(passwordBytes); 
} 
} 
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 15 2007
Added on Oct 16 2007
17 comments
1,721 views