Correct way of Encrypting and Decrypting an Image using AES

后端 未结 3 923
猫巷女王i
猫巷女王i 2021-02-02 04:58

EDIT ::: The code in the question works, but it takes around 10 seconds for getting back to the activity once the image is taken in camera. I gave up this approach and used Face

相关标签:
3条回答
  • 2021-02-02 05:14

    Images can be easily encrypted and decrypted using Java libraries. I present to you two seperate codes using two different methods for encryption and decryption. The following codes can also be extended to use for pdf files.

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.security.Key;
    import java.security.NoSuchAlgorithmException;
    
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    
    public class ImageEncDec {
    
        public static byte[] getFile() {
    
            File f = new File("/home/bridgeit/Desktop/Olympics.jpg");
            InputStream is = null;
            try {
                is = new FileInputStream(f);
            } catch (FileNotFoundException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
            byte[] content = null;
            try {
                content = new byte[is.available()];
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                is.read(content);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return content;
        }
    
        public static byte[] encryptPdfFile(Key key, byte[] content) {
            Cipher cipher;
            byte[] encrypted = null;
            try {
                cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, key);
                encrypted = cipher.doFinal(content);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return encrypted;
    
        }
    
        public static byte[] decryptPdfFile(Key key, byte[] textCryp) {
            Cipher cipher;
            byte[] decrypted = null;
            try {
                cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.DECRYPT_MODE, key);
                decrypted = cipher.doFinal(textCryp);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return decrypted;
        }
    
        public static void saveFile(byte[] bytes) throws IOException {
    
            FileOutputStream fos = new FileOutputStream("/home/bridgeit/Desktop/Olympics-new.jpg");
            fos.write(bytes);
            fos.close();
    
        }
    
        public static void main(String args[])
                throws NoSuchAlgorithmException, InstantiationException, IllegalAccessException, IOException {
    
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(128);
            Key key = keyGenerator.generateKey();
            System.out.println(key);
    
            byte[] content = getFile();
            System.out.println(content);
    
            byte[] encrypted = encryptPdfFile(key, content);
            System.out.println(encrypted);
    
            byte[] decrypted = decryptPdfFile(key, encrypted);
            System.out.println(decrypted);
    
            saveFile(decrypted);
            System.out.println("Done");
    
        }
    
    }
    

    ` this is the second code which generates the same output but with only the exception of generating the same key again and again.

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.Arrays;
    
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.spec.SecretKeySpec;
    
    import org.apache.commons.codec.binary.Base64;
    
    public class Trial {
    
        public static byte[] getFile() {
    
            File f = new File("/home/bridgeit/Desktop/Olympics.jpg");
            InputStream is = null;
            try {
                is = new FileInputStream(f);
            } catch (FileNotFoundException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
            byte[] content = null;
            try {
                content = new byte[is.available()];
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                is.read(content);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return content;
        }
    
        public static byte[] encryptPdfFile(SecretKey secretKey, byte[] content) {
            Cipher cipher;
            byte[] encrypted = null;
            try {
                cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    
                cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    
                encrypted = Base64.encodeBase64(cipher.doFinal(content));
    
            } catch (Exception e) {
    
                System.out.println("Error while encrypting: " + e.toString());
            }
            return encrypted;
    
        }
    
        public static byte[] decryptPdfFile(SecretKey secretKey, byte[] textCryp) {
            Cipher cipher;
            byte[] decrypted = null;
            try {
                cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
    
                cipher.init(Cipher.DECRYPT_MODE, secretKey);
                decrypted = cipher.doFinal(Base64.decodeBase64(textCryp));
    
            } catch (Exception e) {
    
                System.out.println("Error while decrypting: " + e.toString());
            }
            return decrypted;
        }
    
        public static void saveFile(byte[] bytes) throws IOException {
    
            FileOutputStream fos = new FileOutputStream("/home/bridgeit/Desktop/Olympics-new.jpg");
            fos.write(bytes);
            fos.close();
    
        }
    
        public static void main(String args[])
                throws NoSuchAlgorithmException, InstantiationException, IllegalAccessException, IOException {
    
            SecretKeySpec secretKey;
            byte[] key;
            String myKey = "ThisIsAStrongPasswordForEncryptionAndDecryption";
    
            MessageDigest sha = null;
            key = myKey.getBytes("UTF-8");
            System.out.println(key.length);
            sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16); // use only first 128 bit
            System.out.println(key.length);
            System.out.println(new String(key, "UTF-8"));
            secretKey = new SecretKeySpec(key, "AES");
    
            byte[] content = getFile();
            System.out.println(content);
    
            byte[] encrypted = encryptPdfFile(secretKey, content);
            System.out.println(encrypted);
    
            byte[] decrypted = decryptPdfFile(secretKey, encrypted);
            System.out.println(decrypted);
    
            saveFile(decrypted);
            System.out.println("Done");
    
        }
    
    }
    
    0 讨论(0)
  • 2021-02-02 05:23

    The code in the question works, but it takes around 10 seconds for getting back to the activity once the image is taken in camera. I gave up this approach and used Facebook's Conceal Library to encrypt and decrypt images. Link to Facebook's Solution : Facebook Conceal - Image Encryption and Decryption

    0 讨论(0)
  • 2021-02-02 05:31

    You are trying to do too much at once, and are getting lost in all the details.

    Start by simplifying your code to the bare minimum needed for encryption and decryption:

    byte[] key = { 1, 2, 3, ... 14, 15, 16 };
    byte[] IV  = { 5, 5, 5, ... 5, 5, 5 };
    String plaintext = "This is a secret message."
    

    Now cut down your code to encrypt and decrypt that plaintext message back to a readable text string.

    When you have that small program working correctly, add back the other complications one at a time. At each stage, check again that your code can encrypt and decrypt successfully. I suggest that you start by adding back the SecretKeyFactory part, and finish with the file reading and writing part.

    By splitting your program up into smaller parts it will be easier for you to understand what each part of your program is doing, and make it easier for you to identify where you are making mistakes.

    0 讨论(0)
提交回复
热议问题