Pages

This tutorial covers the basics of the science of cryptography. It explains how programmers and network professionals can use cryptography to maintain the privacy of computer data. Starting with the origins of cryptography, it moves on to explain cryptosystems, various traditional and modern ciphers, public key encryption, data integration, message authentication, and digital signatures.

Wednesday 15 April 2020

Java Cryptography : Encrypt and Decrypt Video Using AES 256 Java

I wrote a sample program to encrypt and decrypt a video file. 

public static String initVector = "1234567812345678";

//Method Encrypt Video   
  public static void encrypt(String key, String filePath, String outPath) throws FileNotFoundException, IOException {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

            try (FileInputStream fis = new FileInputStream(filePath)) {

                FileOutputStream fos = new FileOutputStream(outPath);
                // Write bytes
                try (CipherOutputStream cos1 = new CipherOutputStream(fos, cipher)) {
                    // Write bytes
                    int b;
                    byte[] d = new byte[8];
                    while ((b = fis.read(d)) != -1) {
                        cos1.write(d, 0, b);
                        
                    }
                    // Flush and close streams.
                    cos1.flush();
                }
            }

        } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) {

            System.out.print(ex.getMessage());
        }
    }

//Method Decrypt Video    
public static void decrypt(String key, String outPath, String inPath) throws FileNotFoundException, IOException {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            FileInputStream fis = new FileInputStream(outPath);

            CipherInputStream cis1;
            try (FileOutputStream fos = new FileOutputStream(inPath)) {
                cis1 = new CipherInputStream(fis, cipher);
                //     CipherInputStream cis2 = new CipherInputStream(fis, cipher2);
                int b;
                byte[] d = new byte[8];
                while ((b = cis1.read(d)) != -1) {
                    fos.write(d, 0, b);
                }   fos.flush();
            }
            //     CipherInputStream cis2 = new CipherInputStream(fis, cipher2);
            cis1.close();

        } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) {

            System.out.print(ex.getMessage());
        }

    }

//Main Class     public static void main(String[] args) throws Exception {
        String key = "uxjdNijiyJDyOJ3RuxjdNijiyJDyOJ3";

        System.out.print("ENCRYPTION :");

        encrypt(key, "C:\\Users\\CRYPTOGRAPHY\\Documents\\EncryptFileAES.mp4", "C:\\Users\\CRYPTOGRAPHY\\Documents\\Encrypted.enc");
        System.out.println("DONE");
        
        
        System.out.print("DECRYPTION :");
        decrypt(key, "C:\\Users\\CRYPTOGRAPHY\\Documents\\Encrypted.enc", "C:\\Users\\CRYPTOGRAPHY\\Documents\\DecrypFile.mp4");
        System.out.println("DONE");
    }


0 comments:

Post a Comment