Pkcs7 padding in java

Nidhin T T picture Nidhin T T · Apr 26, 2013 · Viewed 7k times · Source

Iam using TripleDes /cbc/pkcs7padding in C#.net to encrypt file. and i need to decrypt in java.In java am using DESede/CBC/PKcs5padding But the file is decrypted,but corrupted.

*In java is it possible to use pkcs7padding? or any other solution to decrypt the file in java with encrypted using pkcs7 padding

C# code

namespace EncryptEpubFiles
 {
public class Encryptor
 {
    public static bool EncryptBook(FileInfo fileToEncrypt,string   outPathWithoutExtension,string keyString)
    {
       try
       {
           byte[] encryptedFileByteArray;
           //Start Encryption service provider
           TripleDESCryptoServiceProvider tDES = new TripleDESCryptoServiceProvider();
           //Read all bytes from input file
           byte[] _fileByteArray = File.ReadAllBytes(fileToEncrypt.FullName);
           tDES.Key = GetBytes(keyString);
           tDES.Mode = CipherMode.CBC;
           //tDES.Padding = PaddingMode.PKCS7;
           tDES.Padding = PaddingMode.PKCS7;
           ICryptoTransform trans = tDES.CreateEncryptor();
           //Create Encrypted file byte array
           encryptedFileByteArray = (trans.TransformFinalBlock(_fileByteArray, 0, ((_fileByteArray).Length)));
           //Write Encrypted file byte array to a filr with proper extension
           System.IO.File.WriteAllBytes((outPathWithoutExtension + ".akr"), encryptedFileByteArray);

           return true;
       }
       catch (Exception ex)
       {
           return false;
       }
   }

Java code

public class DecryptFinal {
private static Cipher dcipher;

private static byte[] iv = {
    (byte)0xB2, (byte)0x12, (byte)0xD5, (byte)0xB2,
    (byte)0x44, (byte)0x21, (byte)0xC3, (byte)0xC3
    };


public static void main(String[] args){

    try {
        String s = "123456789123456789111234";
        AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

        SecretKeyFactory keyfactory=SecretKeyFactory.getInstance("DESede");
        byte[] encodedkey=s.getBytes();
        System.out.println();
         SecretKey key = keyfactory.generateSecret(new DESedeKeySpec(encodedkey));
         System.out.println(new DESedeKeySpec(encodedkey));
        SecretKeySpec(encodedKey,0,encodedKey.length,"DESede" );

        dcipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
        FileInputStream fs =new FileInputStream("E:\\Test1\\Test1\\Encrypted Files\\Wedding bells.akr");
        FileOutputStream os= new FileOutputStream("E:\\Test1\\Test1\\Encrypted Files\\Encrypted Files\\E-pub Totorials");
        byte[] buf = new byte[1024];// bytes read from stream will be decrypted
        CipherInputStream cis = new CipherInputStream(fs, dcipher);// read in the decrypted bytes and write the clear text to out
        int numRead = 0;
        while ((numRead = cis.read(buf)) >= 0) {
            os.write(buf, 0, numRead);
        }
        cis.close();// close all streams
        fs.close();
        os.close();

    }
    catch(FileNotFoundException e) {
        System.out.println("File Not Found:" + e.getMessage());
        return;
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e) {
        System.out.println("I/O Error:" + e.getMessage());
    }
    catch (InvalidKeySpecException e) {
        // TODO: handle exception
        e.printStackTrace();
    }

Answer