Encrypt files with AES on Android


Linux4 hope

So I'm working on a personal project for myself and I'm trying to encrypt files on my phone. These files can be anything like documents, photos, etc. Now I am trying to get it working. Whenever I run encrypt it seems to work fine and encrypt the file. When I run decrypt, sometimes it works and sometimes it doesn't. When it fails, I usually get the "Error determining password, padding block is corrupted" error. I also didn't use different test files, so it's not like some files work and others don't. I try the same two files every time.

public static void encryptfile(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream(path);
    FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
    byte[] key = (salt + Pass).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.ENCRYPT_MODE, sks);
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    int b;
    byte[] d = new byte[8];
    while((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    }
    cos.flush();
    cos.close();
    fis.close();
}

public static void decrypt(String path,String Pass) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream(path);
    FileOutputStream fos = new FileOutputStream(path.replace(".crypt",""));
    byte[] key = (salt + Pass).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);
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }
    fos.flush();
    fos.close();
    cis.close();
}

Currently, Salt and passwords are static and will not be changed for testing purposes. Still getting it wrong about half the time.

Does anyone know why this is happening? I've been searching and found some things to try, but to no avail. I've pored over some of the following questions for a solution:

Android decryption: Error finalizing password

Last block of CipherInputStream/CipherOutputStream is incomplete even with padding AES/CBC/PKCS5Padding

Android 4.2 encryption bug

Decryption error: "Expected no IV set"

How to handle "The last block in decryption is incomplete"

Encryption and decryption of image files

Tips for encrypting/decrypting images in Java using AES

Any help is greatly appreciated! I think I'm just missing something simple...

renew!

People are right that it's salt. When I removed the salt, the problem was solved. . . Did some more digging and it turned out that salt + Pass was the problem, but since salt is a byte[] and Pass is a string. I changed the salt to String, then used salt.concat(Pass), problem solved!

Piero Ottuzzi

Maybe I'm missing something, but in my case it works fine. Could you try the following class just by changing the fileToBeCrypted, fileToBeDecrypted and fileDecryptedOutput variables?

package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class TestCrypt{

    private static final String salt = "t784";
    private static final String cryptPassword = "873147cbn9x5'2 79'79314";
    private static final String fileToBeCrypted = "c:\\Temp\\sampleFile.conf";
    private static final String fileToBeDecrypted = "c:\\Temp\\sampleFile.conf.crypt";
    private static final String fileDecryptedOutput = "c:\\Temp\\sampleFile.conf.decrypted";

    public static void main(String[] args) throws Exception
    {
        for (int i=0; i<100; i++)
        {
            encryptfile(fileToBeCrypted, cryptPassword);
            decrypt(fileToBeDecrypted, cryptPassword, fileDecryptedOutput);
            System.out.println(i);
        }
    }

    public static void encryptfile(String path,String password) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
        FileInputStream fis = new FileInputStream(path);
        FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
        byte[] key = (salt + 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.ENCRYPT_MODE, sks);
        CipherOutputStream cos = new CipherOutputStream(fos, cipher);
        int b;
        byte[] d = new byte[8];
        while((b = fis.read(d)) != -1) {
            cos.write(d, 0, b);
        }
        cos.flush();
        cos.close();
        fis.close();
    }

    public static void decrypt(String path,String password, String outPath) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
        FileInputStream fis = new FileInputStream(path);
        FileOutputStream fos = new FileOutputStream(outPath);
        byte[] key = (salt + 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);
        CipherInputStream cis = new CipherInputStream(fis, cipher);
        int b;
        byte[] d = new byte[8];
        while((b = cis.read(d)) != -1) {
            fos.write(d, 0, b);
        }
        fos.flush();
        fos.close();
        cis.close();
    }

}

I can iterate multiple times without error! I am using Oracle JDK 1.8 but running in 1.7 compatibility mode.

Hope this helps you.

goodbye piero

Related


Encrypt files with AES on Android

Linux4 hope So I'm working on a personal project for myself and I'm trying to encrypt files on my phone. These files can be anything like documents, photos, etc. Now I am trying to get it working. Whenever I run encrypt it seems to work fine and encrypt the fi

Encrypt files with AES on Android

Linux4Hope: So I'm working on a personal project for myself and I'm trying to encrypt files on my phone. These files can be anything like documents, photos, etc. Currently, I'm trying to get it working. Whenever I run encrypt it seems to work fine and encrypt

Encrypt files with aes 128

Philip Gorgievsky I want to encrypt a file in linux using this command sudo openssl enc –aes-128-ecb –nosalt -p –in poraka.txt -out poraka.aesbut terminal gives me this errorExtra arguments given. What should I remove from this command line? Simson First check

Encrypt files with AES

Kevin I'm trying to build a program that takes in a file (EXE of arbitrary size), encrypts it and copies it into a structure. Then decrypt it later and make sure it's used the same. I'm having a hard time encrypting and then decrypting the file. It doesn't see

Encrypt files with AES

Kevin I'm trying to build a program that takes in a file (EXE of arbitrary size), encrypts it and copies it into a structure. Then decrypt it later and make sure it's used the same. I'm having a hard time encrypting and then decrypting the file. It doesn't see

Encrypt files with AES

Kevin I'm trying to build a program that takes in a file (EXE of arbitrary size), encrypts it and copies it into a structure. Then decrypt it later and make sure it's used the same. I'm having a hard time encrypting and then decrypting the file. It doesn't see

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;

Efficiently encrypt files with AES in Java

RYN: I'm developing an application that should encrypt some small files (less than 1MB) and large files (around 500MB). How can I encrypt a file efficiently and save the encrypted version somewhere on disk (i.e. fast)? Can I get encryption progress if it takes

Efficiently encrypt files with AES in Java

RYN: I'm developing an application that should encrypt some small files (less than 1MB) and large files (around 500MB). How can I encrypt a file efficiently and save the encrypted version somewhere on disk (i.e. fast)? Can I get encryption progress if it takes

How to quickly encrypt files with AES?

Stefano Palazzo I want to encrypt a file using AES-256. How can I do this quickly and easily, and how can I or someone else decrypt it again? Stefano Palazzo Unfortunately, there is no easy solution to protect your stuff. Thinking about your use case, maybe so

How to quickly encrypt files with AES?

Stefano Palazzo I want to encrypt a file using AES-256. How can I do this quickly and easily, and how can I or someone else decrypt it again? Stefano Palazzo Unfortunately, there is no easy solution to protect your stuff. Thinking about your use case, maybe so

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;

Efficiently encrypt files with AES in Java

RYN: I'm developing an application that should encrypt some small files (less than 1MB) and large files (around 500MB). How can I encrypt a file efficiently and save the encrypted version somewhere on disk (i.e. fast)? Can I get encryption progress if it takes

How to quickly encrypt files with AES?

Stefano Palazzo I want to encrypt a file using AES-256. How can I do this quickly and easily, and how can I or someone else decrypt it again? Stefano Palazzo Unfortunately, there is no easy solution to protect your stuff. Thinking about your use case, maybe so

Encrypt files with AES using a shared secret

Devy I want to encrypt/decrypt via AES using a shared cipher , my code is the same as here . The linked code works fine, but there is no shared password in it. How can I add a shared secret in the following implementation? I need something like String shared="

Encrypt Golang files using crypto/aes lib

W Khan: I am trying to encrypt a file using the Go crypto/aes package. I have so far: func encrypt(source string, localdir string) error { src := filepath.Join("/home/bacula/cloud-backup/"+localdir, source) dst := filepath.Join(src + ".aes") fmt.

Encrypt large files with AES using JAVA

Halcyondayz I have tested my code with files smaller than (10mb, 100mb, 500mb) and the encryption works. However, I'm having issues with files larger than 1gb. I have generated a large file (about 2gb) and I want to encrypt it using AES using JAVA, but I am ge

Encrypt files with AES using a shared secret

Devy I want to encrypt/decrypt via AES using a shared cipher , my code is the same as here . The linked code works fine, but there is no shared password in it. How can I add a shared secret in the following implementation? I need something like String shared="

Encrypt Golang files using crypto/aes lib

W Khan: I am trying to encrypt a file using the Go crypto/aes package. I have so far: func encrypt(source string, localdir string) error { src := filepath.Join("/home/bacula/cloud-backup/"+localdir, source) dst := filepath.Join(src + ".aes") fmt.

Encrypt Golang files using crypto/aes lib

W Khan: I am trying to encrypt a file using the Go crypto/aes package. I have so far: func encrypt(source string, localdir string) error { src := filepath.Join("/home/bacula/cloud-backup/"+localdir, source) dst := filepath.Join(src + ".aes") fmt.

Encrypt large files with AES using JAVA

Halcyondayz I have tested my code with files smaller than (10mb, 100mb, 500mb) and the encryption works. However, I'm having issues with files larger than 1gb. I have generated a large file (about 2gb) and I want to encrypt it using AES using JAVA, but I am ge

Encrypt large files with AES using JAVA

Halcyondayz I have tested my code with files smaller than (10mb, 100mb, 500mb) and the encryption works. However, I'm having issues with files larger than 1gb. I have generated a large file (about 2gb) and I want to encrypt it using AES using JAVA, but I am ge

Encrypt large files with AES using JAVA

Halcyondayz I have tested my code with files smaller than (10mb, 100mb, 500mb) and the encryption works. However, I'm having issues with files larger than 1gb. I have generated a large file (about 2gb) and I want to encrypt it using AES using JAVA, but I am ge

AES-Encrypt-then-MAC large files with .NET

Developer I want to encrypt a large file (say 64 GB) in the most efficient way in .NET. How would I achieve this: Instance created AesManagedto encrypt file stream (64 GB read) save this stream to disk (because it's huge to fit in memory) (write 64 GB) Instanc

AES-Encrypt-then-MAC large files with .NET

Developer I want to encrypt a large file (say 64 GB) in the most efficient way in .NET. How would I achieve this: Instance created AesManagedto encrypt file stream (64 GB read) save this stream to disk (because it's huge to fit in memory) (write 64 GB) Instanc

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

Encrypt files with AES using a shared secret

Devy I want to encrypt/decrypt via AES using a shared cipher , my code is the same as here . The linked code works fine, but there is no shared password in it. How can I add a shared secret in the following implementation? I need something like String shared="

Encrypt Golang files using crypto/aes lib

W Khan: I am trying to encrypt a file using the Go crypto/aes package. I have so far: func encrypt(source string, localdir string) error { src := filepath.Join("/home/bacula/cloud-backup/"+localdir, source) dst := filepath.Join(src + ".aes") fmt.

Encrypt Golang files using crypto/aes lib

W Khan: I am trying to encrypt a file using the Go crypto/aes package. I have so far: func encrypt(source string, localdir string) error { src := filepath.Join("/home/bacula/cloud-backup/"+localdir, source) dst := filepath.Join(src + ".aes") fmt.