If you want a consistent experience across platforms I would recommend
Chilkat. That's what we use and it makes encryption so much easier.
On Apr 4, 2013 11:58 PM, "Hiren Dabhi" <[email protected]> wrote:

> i don't know the encryption-decryption, you can try to change the last
> line for decrypt as,
>
> c.decrypt(new FileInputStream(fileNameInput)**, new FileOutputStream(**
> fileNameOutput));
>
>
> On Sunday, 18 March 2012 04:20:41 UTC+5:30, Italo Mendonça Rocha wrote:
>>
>> I'm trying to encrypt and decrypt a file using a key that the user typed. All
>> these operations are done in Android. At the moment I decrypt I get a
>> javax.crypto.**BadPaddingException: pad block corrupted. The same code works
>> normally on Windows PC. Why?
>>
>> Here is my code:
>>
>> *The call:*
>> *Encrypt:*
>>             String fileNameInput = Environment.**
>> getExternalStorageDirectory().**toString() + File.separator +
>> SAVE_FOLDER + File.separator + "file";
>>             String fileNameOutput = Environment.**
>> getExternalStorageDirectory().**toString() + File.separator + AVE_FOLDER
>> + File.separator + "file_crypted";
>>             Crypto c = new Crypto(userKey);
>>             c.encrypt(new FileInputStream(fileNameInput)**, new
>> FileOutputStream(**fileNameOutput));
>> *Decrypt:*
>>             String fileNameInput = Environment.**
>> getExternalStorageDirectory().**toString() + File.separator +
>> SAVE_FOLDER + File.separator + "file_crypted";
>>             String fileNameOutput = Environment.**
>> getExternalStorageDirectory().**toString() + File.separator + AVE_FOLDER
>> + File.separator + "file_decrypted";
>>             Crypto c = new Crypto(userKey);
>>             c.encrypt(new FileInputStream(fileNameInput)**, new
>> FileOutputStream(**fileNameOutput));
>>
>> *The class Crypto:*
>> import java.io.ByteArrayOutputStream;
>> import java.io.FileInputStream;
>> import java.io.FileOutputStream;
>> import java.io.InputStream;
>> import java.io.OutputStream;
>> import java.security.MessageDigest;
>> import java.security.SecureRandom;
>> import java.security.spec.**AlgorithmParameterSpec;
>>
>> import javax.crypto.Cipher;
>> import javax.crypto.**CipherInputStream;
>> import javax.crypto.**CipherOutputStream;
>> import javax.crypto.KeyGenerator;
>> import javax.crypto.SecretKey;
>> import javax.crypto.spec.**IvParameterSpec;
>> import javax.crypto.spec.**SecretKeySpec;
>>
>> import android.graphics.Bitmap;
>> import br.com.italomr.photocrypt.**util.Domain;
>> import br.com.italomr.photocrypt.**util.StringOutputStream;
>>
>> public class Crypto {
>>     private Cipher ecipher;
>>     private Cipher dcipher;
>>
>>     private static final String AES = "AES";
>>     private static final String AES_PADDING = "AES/CBC/PKCS5Padding";
>>     private static final String MD5 = "MD5";
>>     private static final String HEXES = "0123456789ABCDEF";
>>
>>     /**
>>      * Input a string that will be md5 hashed to create the key.
>>      *
>>      * @return void, cipher initialized
>>      */
>>
>>     public Crypto() {
>>         try {
>>             KeyGenerator kgen = KeyGenerator.getInstance(AES);
>>             kgen.init(128);
>>             this.setupCrypto(kgen.**generateKey());
>>         } catch (Exception e) {
>>             e.printStackTrace();
>>         }
>>     }
>>
>>     public Crypto(String key) {
>>         SecretKeySpec skey = new SecretKeySpec(getMD5(key), AES);
>>         this.setupCrypto(skey);
>>     }
>>
>>     private void setupCrypto(SecretKey key) {
>>         // Create an 8-byte initialization vector
>>         byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
>> 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
>>
>>         AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
>>         try {
>>             ecipher = Cipher.getInstance(AES_**PADDING);
>>             dcipher = Cipher.getInstance(AES_**PADDING);
>>
>>             // CBC requires an initialization vector
>>             ecipher.init(Cipher.ENCRYPT_**MODE, key, paramSpec);
>>             dcipher.init(Cipher.DECRYPT_**MODE, key, paramSpec);
>>         } catch (Exception e) {
>>             e.printStackTrace();
>>         }
>>     }
>>
>>     // Buffer used to transport the bytes from one stream to another
>>     byte[] buf = new byte[1024];
>>
>>     public void encrypt(InputStream in, OutputStream out) {
>>         try {
>>             // Bytes written to out will be encrypted
>>             out = new CipherOutputStream(out, ecipher);
>>
>>             // Read in the cleartext bytes and write to out to encrypt
>>             int numRead = 0;
>>             while ((numRead = in.read(buf)) >= 0) {
>>                 out.write(buf, 0, numRead);
>>             }
>>             out.close();
>>         } catch (java.io.IOException e) {
>>             e.printStackTrace();
>>         }
>>     }
>>
>>     public byte[] encrypt(byte[] data) {
>>         try {
>>             byte[] ciphertext = ecipher.doFinal(data);
>>             return ciphertext;
>>         } catch (Exception e) {
>>             e.printStackTrace();
>>             return null;
>>         }
>>     }
>>
>>     public void decrypt(InputStream in, OutputStream out) {
>>         try {
>>             // Bytes read from in will be decrypted
>>             in = new CipherInputStream(in, dcipher);
>>
>>             // Read in the decrypted bytes and write the cleartext to out
>>             int numRead = 0;
>>             while ((numRead = in.read(buf)) >= 0) {
>>                 out.write(buf, 0, numRead);
>>             }
>>
>>             out.close();
>>         } catch (java.io.IOException e) {
>>             e.printStackTrace();
>>         }
>>     }
>>
>>     public byte[] decrypt(byte[] ciphertext) {
>>         try {
>>             // To convert to String
>>             // String plaintext = new String(dcipher.doFinal(**
>> ciphertext),
>>             // Domain.UTF8);
>>             return dcipher.doFinal(ciphertext);
>>         } catch (Exception e) {
>>             e.printStackTrace();
>>             return null;
>>         }
>>     }
>>
>>     private static byte[] getMD5(String input) {
>>         try {
>>             byte[] bytesOfMessage = input.getBytes(Domain.UTF8);
>>             MessageDigest md = MessageDigest.getInstance(MD5)**;
>>             return md.digest(bytesOfMessage);
>>         } catch (Exception e) {
>>             return null;
>>         }
>>     }
>>
>>     public static String byteToHex(byte[] raw) {
>>         if (raw == null) {
>>             return null;
>>         }
>>         final StringBuilder hex = new StringBuilder(2 * raw.length);
>>         for (final byte b : raw) {
>>             hex.append(HEXES.charAt((b & 0xF0) >>
>> 4)).append(HEXES.charAt((b & 0x0F)));
>>         }
>>         return hex.toString();
>>     }
>>
>>     public static byte[] hexToByte(String hexString) {
>>         int len = hexString.length();
>>         byte[] ba = new byte[len / 2];
>>         for (int i = 0; i < len; i += 2) {
>>             ba[i / 2] = (byte) ((Character.digit(hexString.**charAt(i),
>> 16) << 4) + Character.digit(hexString.**charAt(i + 1), 16));
>>         }
>>         return ba;
>>     }
>>
>>
>> }
>>
>> Thanks in advance.
>> Ítalo Rocha
>>
>  --
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Android Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to