AES encryption and decryption in Java


death:

I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in the repository, and the encryption key is read in as an environment variable. I'm assuming that even including the decryptor in the repo is incorrect, since the algorithm/mode/padding is visible in the code as shown below.

Besides that, after some research, it looks like if an attacker knows the initialization vector, they will be able to decrypt the first block in the encrypted file. do i understand? Also, I've read that the vector should be randomly generated every time, but I don't know how to do this since the encryptor will only run once unless I change how the file is encrypted.

One last question... does it make sense to set another environment variable to store the initialization vector being used? Due to the randomness of the vector to look for, I don't think.

    private static byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    private static IvParameterSpec ivspec = new IvParameterSpec(iv);
    private static byte[] decodedKey = Base64.getDecoder().decode(System.getenv("SECRET_KEY"));
    private static SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");

    //any encrypted input file that is not a database will be read in as an input stream
    public static CipherInputStream decryptInputStream(InputStream inputStream) {
        try{
            // Initialize the cipher by specifying the algorithm/mode/padding
            Cipher aes2 = Cipher.getInstance("AES/CBC/PKCS5Padding");
            //
            aes2.init(Cipher.DECRYPT_MODE, originalKey, ivspec);

            CipherInputStream in = new CipherInputStream(inputStream, aes2);

            return in;
        } catch(NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException e){
            logger.info(e.getMessage());
            return null;
        }
    }
Taste 2:

But I'm not sure I'm going about it the right way.

I have some examples of my own using cryptocurrencies .

Besides the static IV (already commented), you're missing some form of integrity control (each for "authenticated encryption"). This can be achieved using modes with built-in integrity (eg AES-CGM) or using HMAC.

I'm assuming that even including the decryptor in the repo is incorrect, since the algorithm/mode/padding is visible in the code as shown below.

Whether the algorithm/pattern/padding is visible or not has no effect on solution security. The only thing you need to protect is the key.

Besides that, after some research, it looks like if an attacker knows the initialization vector, they will be able to decrypt the first block in the encrypted file.

This is not true. The IV must be unique (for CBC, the IV must be unpredictable/random) for each encryption operation performed with the same key (thx Maarten). A known IV doesn't help an attacker decrypt anything.

As mentioned earlier, the IV is usually passed as part of the ciphertext. Often, the encrypted result looks like IV || ciphertext || MACit ||represents a concatenation. The size of the IV and MAC are fixed, so when decrypting, splitting the parameters is simple.

do i understand? Also, I read that the vector should be randomly generated each time

affim

So the IV should only be randomly assigned between files, not between runs?

The whole point of using IVs is that there are some weaknesses in security if multiple inputs are encrypted with the same key. In practice, this weakness can be avoided by using a unique or random IV.

Related


AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

Java AES encryption and decryption

Praneeth I want to encrypt and decrypt a password using 128 bit AES encryption and a 16 byte key. There was an error javax.crypto.BadPaddingExceptiondecrypting the value . Do I lose anything when decrypting? public static void main(String args[]) { Test t

Java AES encryption and decryption

Praneeth I want to encrypt and decrypt a password using 128 bit AES encryption and a 16 byte key. There was an error javax.crypto.BadPaddingExceptiondecrypting the value . Do I lose anything when decrypting? public static void main(String args[]) { Test t

AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

Java AES encryption and decryption

Praneeth I want to encrypt and decrypt a password using 128 bit AES encryption and a 16 byte key. There was an error javax.crypto.BadPaddingExceptiondecrypting the value . Do I lose anything when decrypting? public static void main(String args[]) { Test t

AES encryption and decryption in Java

death: I'm pretty new to encryption/decryption and have to encrypt some data files, but not entirely sure I'm going about it the right way. Right now, I have a script to encrypt all files that are not included in my repository, but the decryptor is included in

AES javascript encryption and Java decryption

Encoder I have implemented RSA encryption in javascrypt and RSA decryption in java, it's just a simple process. But the problem is that I have to encrypt a lot of data in one go, which is not possible with RSA, either I have to split the data to be encrypted (

AES javascript encryption and Java decryption

Encoder I have implemented RSA encryption in javascrypt and RSA decryption in java, it's just a simple process. But the problem is that I have to encrypt a lot of data in one go, which is not possible with RSA, either I have to split the data to be encrypted (

AES javascript encryption and Java decryption

Encoder I have implemented RSA encryption in javascrypt and RSA decryption in java, it's just a simple process. But the problem is that I have to encrypt a lot of data in one go, which is not possible with RSA, either I have to split the data to be encrypted (

AES encryption and decryption using Java

Yogesh D Here's what I'm doing, it might seem a little clunky, but it might help you out. I get one BadPaddingException. Read almost all related topics but couldn't find a proper solution. I am new to encryption decryption programming and need to implement it

AES encryption and decryption using Java

Yogesh D Here's what I'm doing, it might seem a little clunky, but it might help you out. I get one BadPaddingException. Read almost all related topics but couldn't find a proper solution. I am new to encryption decryption programming and need to implement it

AES encryption in JavaScript and decryption in Java

Zenz 0 I have an existing web service that encrypts and decrypts with AES, now I have to encrypt in the same way as Java, but with javascript. I've read all the threads about doing this in javascript but haven't found any useful solution yet. Javascript is alw

AES javascript encryption and Java decryption

Encoder I have implemented RSA encryption in javascrypt and RSA decryption in java, it's just a simple process. But the problem is that I have to encrypt a lot of data in one go, which is not possible with RSA, either I have to split the data to be encrypted (

AES javascript encryption and Java decryption

Encoder I have implemented RSA encryption in javascrypt and RSA decryption in java, it's just a simple process. But the problem is that I have to encrypt a lot of data in one go, which is not possible with RSA, either I have to split the data to be encrypted (

AES encryption in JavaScript and decryption in Java

Zenz 0 I have an existing web service that encrypts and decrypts using AES, now I have to encrypt in the same way as in Java, but in javascript. I've read all the threads about doing this in javascript but haven't found any useful solution yet. Javascript is a

AES encryption using Java and decryption using Java

rkj : I'm making an application that requires Java-based AES encryption and JavaScript-based decryption. I am using the following code as base form for encryption. public class AESencrp { private static final String ALGO = "AES"; private static final byte

AES encryption using Java and decryption using Java

j I'm making an application that requires Java-based AES encryption and JavaScript-based decryption. I am using the following code as base form for encryption. public class AESencrp { private static final String ALGO = "AES"; private static final byte[] k

AES encryption using Java and decryption using Java

rkj : I'm making an application that requires Java-based AES encryption and JavaScript-based decryption. I am using the following code as base form for encryption. public class AESencrp { private static final String ALGO = "AES"; private static final byte

AES encryption/decryption in Node JS similar to Java

PRASHANT Kumar Sharma: I am trying to replicate the Java code for AES encryption and decryption in Node JS . Java code SecretKeySpec skeySpec; String key = "a4e1112f45e84f785358bb86ba750f48"; public void encryptString(String key) throws Exception

Java AES encryption and decryption with static secret

Aaron I have an application that needs to store some secret passwords in a config file like database and ftp passwords/details. I've looked around and found many encryption/decryption solutions using AES, but I can't seem to figure out how to make it work with

Simple AES encryption in PHP, decryption in Java

MH I want to implement a simple and safe way to send some information from a PHP script to a Java client. I've pored over several implementations here, but none have worked so far, and now I'm frustrated. What I ended up doing with some edits was this: PHP: fu

Java simple helloworld AES encryption and decryption

Qwertyzw I got a code snippet from another SO question and modified it a bit, but it doesn't seem to work. Can anyone find out why? Currently printing [B@405e70bc or similar. All I have to do is store the password in encrypted form with the sole purpose of kee

Encryption and decryption AES for all types of files in Java

Insaf Safa: How can I modify this AES encryption code so that it can encrypt and decrypt any kind of file (pdf, docx....) because when I decrypt the pdf file or whatever, I don't get the original file. public EncryptData(File originalFile, File encrypted, Secr

AES 128 encryption in Java decryption (in PHP)

Shanawasi I've been trying to decrypt a string using AES-128 CBC which was originally encrypted using JAVA AES encryption. In Java, PKCS7 padding is used. And I try to encrypt and decrypt using similar PHP code. But I get different results. my java code import