I am trying encryption and decryption using cipther,AES algorithm but while decrypting its throwing error bad decrypted file?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



I am trying encryption and decryption using cipther,AES algorithm but while decrypting its throwing error bad decrypted file?


@SuppressWarnings("resource")
public static void encrypt(SecretKey key, /*AlgorithmParameterSpec paramSpec,*/ AlgorithmParameterSpec paramSpec, InputStream in, OutputStream out)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
InvalidAlgorithmParameterException, IOException
try
byte iv = new byte (byte) 0x8E, 0x12, 0x39, (byte) 0x9C,
0x07, 0x72, 0x6F, 0x5A, (byte) 0x8E, 0x12, 0x39, (byte) 0x9C,
0x07, 0x72, 0x6F, 0x5A ;
//generate new AlgorithmParameterSpec
// AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
Cipher c = Cipher.getInstance(ALGO_VIDEO_ENCRYPTOR);
c.init(Cipher.ENCRYPT_MODE, key, paramSpec);
out = new CipherOutputStream(out, c);
Log.d("tag","out"+out);
int count = 0;
byte buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE];
while ((count = in.read(buffer)) >= 0)
out.write(buffer, 0, count);

finally
out.close();




@SuppressWarnings("resource")
public static void decrypt(SecretKey key, /*AlgorithmParameterSpec paramSpec,*/ AlgorithmParameterSpec paramSpec, InputStream in, OutputStream out)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
InvalidAlgorithmParameterException, IOException
try
byte iv = new byte(byte) 0x8E, 0x12, 0x39, (byte) 0x9C,
0x07, 0x72, 0x6F, 0x5A, (byte) 0x8E, 0x12, 0x39, (byte) 0x9C,
0x07, 0x72, 0x6F, 0x5A;
// read from input stream AlgorithmParameterSpec
// AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
Cipher c = Cipher.getInstance(ALGO_VIDEO_ENCRYPTOR);
c.init(Cipher.DECRYPT_MODE, key, paramSpec);
out = new CipherOutputStream(out, c);
int count = 0;
byte buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE];
while ((count = in.read(buffer)) >= 0)
out.write(buffer, 0, count);

// finally
out.close();
catch (Exception e)






//here i am passing video file path
File inFile = new File("/storage/sdcard1/Digilearn/algebriac_expressions_8mk_p1_c6.mp4");
File outFile = new File(String.valueOf(extStore)+"/Digilearn/enc_video");
// File outFile = new File("/storage/sdcard1/Digilearn/enc_video.swf");
File outFile_dec = new File(String.valueOf(extStore)+"/Digilearn/enc_video.mp4");

try
SecretKey key = KeyGenerator.getInstance(ALGO_SECRET_KEY_GENERATOR).generateKey();

byte keyData = key.getEncoded();
SecretKey key2 = new SecretKeySpec(keyData, 0, keyData.length, ALGO_SECRET_KEY_GENERATOR); //if you want to store key bytes to db so its just how to //recreate back key from bytes array

byte iv = new byte[IV_LENGTH];
SecureRandom.getInstance(ALGO_RANDOM_NUM_GENERATOR).nextBytes(iv); // If
// storing
// separately
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
// String key1 = "secretsecretsecretsecretsecretse";

//encrypt(key, paramSpec, new FileInputStream(inFile), new FileOutputStream(outFile));
decrypt(key2, paramSpec, new FileInputStream(outFile), new FileOutputStream(outFile_dec));
Toast.makeText(MainActivity.this,"encrpted successfully",Toast.LENGTH_LONG).show();
catch (Exception e)
e.printStackTrace();





why you decrypt .mp4 file?
– Waleed Asim
Aug 10 at 4:57





We have a private mp4 file which we would like to encrypt and decrypt for security reasons.We have tried the above mentioned code,the file is getting encrypted properly but during decryption the error message is being shown, ie; the encryption and decryption keys are not matching, kindly let me know how to go about the error, thank you.
– pooja
Aug 10 at 5:11






i think you should encrypt and decrypt file using ffmpeg in android
– Waleed Asim
Aug 10 at 5:13





please could you elaborate on FFMPEG, a little. Please ..
– pooja
Aug 10 at 5:15





see this stackoverflow.com/questions/50681293/…
– Waleed Asim
Aug 10 at 5:18









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard