Decrypt files using AES method in Android


Faisal Shaikh:

I have encrypted a file using AES encryption in php using the following code.

$ALGORITHM = 'AES-128-CBC';
$IV = '12dasdq3g5b2434b';
$password = '123';
openssl_encrypt($contenuto, $ALGORITHM, $password, 0, $IV);

Now I'm trying to decrypt it in Android but I keep getting InvalidKeyException: Key length is not 128/192/256 bit error. Here is the android code:

String initializationVector = "12dasdq3g5b2434b";
String password = "123";
FileInputStream fis = new FileInputStream(cryptFilepath);
FileOutputStream fos = new FileOutputStream(outputFilePath);
byte[] key = (password).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key,16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks, new IvParameterSpec(initializationVector.getBytes()));
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[16];
while((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();

Can anyone suggest me what to do. Any help would be greatly appreciated.

topaz:

The original code posted in the question uses streams to read, decrypt and write to the corresponding files. This makes it possible to process files larger than available memory.

However, the originally posted code lacks Base64 decoding, which is required because the ciphertext of the PHP code is Base64 encoded.

BASE64 decoding can be comfortably implemented using Apache Encyclopedia-like codecs, which operate between and and are therefore easy to integrate:Base64InputStreamFileInputStreamCipherInputStream

import org.apache.commons.codec.binary.Base64InputStream;

...

public static void decrypt(String ciphertextFilepath, String decryptedFilePath) throws Exception {
    
    String password = "123";
    String initializationVector = "12dasdq3g5b2434b";

    byte[] key = new byte[16];
    byte[] passwordBytes = password.getBytes(StandardCharsets.UTF_8);
    System.arraycopy(passwordBytes, 0, key, 0, passwordBytes.length);
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
    IvParameterSpec ivParameterSpec = new IvParameterSpec(initializationVector.getBytes(StandardCharsets.UTF_8));
    
    Cipher cipher = Cipher.getInstance("AES/CBC/Pkcs5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
    
    try (FileInputStream fis = new FileInputStream(ciphertextFilepath);
         Base64InputStream b64is = new Base64InputStream(fis);
         CipherInputStream cis = new CipherInputStream(b64is, cipher);
         FileOutputStream fos = new FileOutputStream(decryptedFilePath)) {
            
            int read;
            byte[] buffersize = new byte[16]; // 16 bytes for testing, in practice use a suitable size (depending on your RAM size), e.g. 64 Mi
            while((read = cis.read(buffersize)) != -1) {
                fos.write(buffersize, 0, read);
        }
    }
}

Other fixed bugs/optimization points are (see also other answers/comments):

  • Use CBC mode similar to PHP code
  • Using the key from the PHP code
  • A clear specification of the encoding used

Related


Decrypt files using AES method in Android

Faisal Shaikh: I have encrypted a file using AES encryption in php using the following code. $ALGORITHM = 'AES-128-CBC'; $IV = '12dasdq3g5b2434b'; $password = '123'; openssl_encrypt($contenuto, $ALGORITHM, $password, 0, $IV); Now I'm trying to decrypt it in A

Decrypt files using AES method in Android

Faisal Shaikh: I have encrypted a file using AES encryption in php using the following code. $ALGORITHM = 'AES-128-CBC'; $IV = '12dasdq3g5b2434b'; $password = '123'; openssl_encrypt($contenuto, $ALGORITHM, $password, 0, $IV); Now I'm trying to decrypt it in A

Decrypt files using AES method in Android

Faisal Shaikh: I encrypted a file using AES encryption in php using the following code. $ALGORITHM = 'AES-128-CBC'; $IV = '12dasdq3g5b2434b'; $password = '123'; openssl_encrypt($contenuto, $ALGORITHM, $password, 0, $IV); Now I'm trying to decrypt it in Androi

Decrypt files using AES method in Android

Faisal Shaikh: I encrypted a file using AES encryption in php using the following code. $ALGORITHM = 'AES-128-CBC'; $IV = '12dasdq3g5b2434b'; $password = '123'; openssl_encrypt($contenuto, $ALGORITHM, $password, 0, $IV); Now I'm trying to decrypt it in Androi

Decrypt files using AES method in Android

Faisal Shaikh: I have encrypted a file using AES encryption in php using the following code. $ALGORITHM = 'AES-128-CBC'; $IV = '12dasdq3g5b2434b'; $password = '123'; openssl_encrypt($contenuto, $ALGORITHM, $password, 0, $IV); Now I'm trying to decrypt it in A

Decrypt AES key using RSA in Android

Sijo Chosan I'm trying to encrypt a small file using AES (128 bit) and then encrypt the AES key using RSA (1024 bit). This is good. As a logical next step, I tried decrypting the AES key using RSA. Decrypting with RSA returns a 128 byte block, but my AES key i

Decrypt AES key using RSA in Android

Sijo Chosan I'm trying to encrypt a small file using AES (128 bit) and then encrypt the AES key using RSA (1024 bit). This is good. As a logical next step, I tried decrypting the AES key using RSA. Decrypting with RSA returns a 128 byte block, but my AES key i

Decrypt files encrypted with openssl using Aes-cbc-256

Madhav: I have encrypted the file using the following command openssl rand 32> test.key openssl enc -aes-256-cbc -iter 10000 -pbkdf2 -salt -in test.txt -out test.txt.enc -pass文件:test.key Now I am trying to decrypt it using Java. Been trying for the past few da

Decrypt files encrypted with openssl using Aes-cbc-256

Madhav: I have encrypted the file using the following command openssl rand 32> test.key openssl enc -aes-256-cbc -iter 10000 -pbkdf2 -salt -in test.txt -out test.txt.enc -pass文件:test.key Now I am trying to decrypt it using Java. Been trying for the past few da

Decrypt files encrypted with openssl using Aes-cbc-256

Madhav: I have encrypted the file using the following command openssl rand 32> test.key openssl enc -aes-256-cbc -iter 10000 -pbkdf2 -salt -in test.txt -out test.txt.enc -pass文件:test.key Now I am trying to decrypt it using Java. Been trying for the past few da

Decrypt files encrypted with openssl using Aes-cbc-256

Madhav: I have encrypted the file using the following command openssl rand 32> test.key openssl enc -aes-256-cbc -iter 10000 -pbkdf2 -salt -in test.txt -out test.txt.enc -pass文件:test.key Now I am trying to decrypt it using Java. Been trying for the past few da

Decrypt files encrypted with openssl using Aes-cbc-256

Madhav: I have encrypted the file using the following command openssl rand 32> test.key openssl enc -aes-256-cbc -iter 10000 -pbkdf2 -salt -in test.txt -out test.txt.enc -pass文件:test.key Now I am trying to decrypt it using Java. Been trying for the past few da

Decrypt files encrypted with openssl using Aes-cbc-256

Madhav: I have encrypted the file using the following command openssl rand 32> test.key openssl enc -aes-256-cbc -iter 10000 -pbkdf2 -salt -in test.txt -out test.txt.enc -pass文件:test.key Now I am trying to decrypt it using Java. Been trying for the past few da

How to encrypt and decrypt files using RSA and AES algorithms

Sofala I want to encrypt test.txt file, I am using this java class for encryption and decryption. In my directory I have three files private.txt for saving the private key, public.txt for the public key and test.txt for encryption. package EncryptionDecryp

Encrypt and decrypt large files with AES

JLLMNCHR: I'm trying to encrypt a large file using AES , then decrypt it and compare it to the original . This lesson summarizes the work. It works for .txt files, but not for .mp3, .pdf, etc. Help would be greatly appreciated. import java.io.FileInputStream;

Encrypt and decrypt large files with AES

JLLMNCHR: I'm trying to encrypt a large file using AES , then decrypt it and compare it to the original . This lesson summarizes the work. It works for .txt files, but not for .mp3, .pdf, etc. Help would be greatly appreciated. import java.io.FileInputStream;

Decrypt OpenSSL encrypted files on Android using Java

arter97 I'm currently trying to implement file decryption on my Android app. The file will be encrypted on the host (Linux) using: openssl aes-128-ecb -salt -k $HASH -in somefile.in -out somefile openssl aes-256-cbc -salt -K $HASH -iv $IV -md sha1 -in somefile

Decrypt Android AES example on iOS

Martijn Mellens I have an Android example of AES encryption/decryption. https://github.com/itog/CryptoSample/blob/master/src/com/pigmal/android/ex/crypto/Crypto.java#L35 I want to decrypt on iOS. It seems almost impossible. I found this to make it easier to im

Decrypt android AES example on iOS

Martijn Mellens i have an Android example of AES encryption/decription. https://github.com/itog/CryptoSample/blob/master/src/com/pigmal/android/ex/crypto/Crypto.java#L35 I want to decrypt this on iOS. It looks allmost impossible. I've found this to make it mor

How to decrypt AES using PHP?

User 4027821 I am using this library to create an AES encrypted string on Android and add this string to MySQL. Here are the parameters for lib: Now I want to decrypt the string via PHP: Here is the encrypted string: Plaintext: a z8OVzJvtKHPCdT6PeFoEww==(encry

Encrypt and decrypt using AES algorithm

Giant I found a tutorial on encryption and decryption, but I ran into a small problem when trying to implement it. I'm wondering if it's permanent, but String seedValue = "This Is MySecure";when I try to change the text to something else I get a line , there's

How to decrypt with CryptoJS using AES?

User 3197788 With the desired options (AES, ECB mode and PKCS7), as the question suggests, I can't seem to get the decrypted value right. I am encrypting like this: var ENC_KEY = "bXlrZXk="; //"mykey" var encrypted = CryptoJS.AES.encrypt("hello", Crypto

How to decrypt with CryptoJS using AES?

User 3197788 With the desired options (AES, ECB mode and PKCS7), as the question suggests, I can't seem to get the decrypted value right. I am encrypting like this: var ENC_KEY = "bXlrZXk="; //"mykey" var encrypted = CryptoJS.AES.encrypt("hello", Crypto

How to encrypt/decrypt larger files with RSA/AES

mobeji Ok, so I'm looking for a way to encrypt/decrypt larger files using RSA and AES. I don't quite understand what I need to do. The situation is that I have larger files (anywhere between 200kb-50mb). I want to be able to encrypt a specific file keeping a k

How to encrypt/decrypt larger files with RSA/AES

mobeji Ok, so I'm looking for a way to encrypt/decrypt larger files using RSA and AES. I don't quite understand what I need to do. The situation is that I have larger files (anywhere between 200kb-50mb). I want to be able to encrypt a specific file keeping a k

AES BadTag exception when trying to decrypt in Android

AndréNogueira : I am trying to encrypt a simple string using AES encryption. Currently I am using generate key and I am KeyGeneratorusing 16 bytes to generate random IVSecure Random The problem is when I run this code: @RequiresApi(Build.VERSION_CODES.M)

Encrypt and decrypt by AES algorithm in both python and android

sisterhood I have python and android code for AES encryption. When I encrypt text in android, it decrypts successfully on python but not on android side. Does anyone have an idea? Python code: import base64 import hashlib from Crypto import Random from Crypto.

Encrypt and decrypt by AES algorithm in both python and android

sisterhood I have python and android code for AES encryption. When I encrypt text in android, it decrypts successfully on python but not on android side. Does anyone have an idea? Python code: import base64 import hashlib from Crypto import Random from Crypto.

Android Studio can't decrypt message in AES

Zamri 94 Hi I'm trying to implement AES in some open source code messaging application. For encrypted mail, it's working to find me. But I'm having a hard time deciphering the reply. In this class I can encrypt the message and it works fine. MessageActivity.ja