Note that there are some explanatory texts on larger screens.

plurals
  1. POPkcs7 padding in java
    primarykey
    data
    text
    <p>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.</p> <p>*In java is it possible to use pkcs7padding? or any other solution to decrypt the file in java with encrypted using pkcs7 padding</p> <p>C# code</p> <pre><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; } } </code></pre> <p>Java code</p> <pre><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)) &gt;= 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(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload