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.
Showing posts with label aes 256 java. Show all posts
Showing posts with label aes 256 java. Show all posts

Sunday, 19 April 2020

[SOLVED] java.io.IOException: keystore password was incorrect

PROBLEM
Exception in thread "main" java.io.IOException: keystore password was incorrect
at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:2059)
at sun.security.provider.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:238)
at sun.security.provider.JavaKeyStore$DualFormatJKS.engineLoad(JavaKeyStore.java:70)
at java.security.KeyStore.load(KeyStore.java:1445)
at getprivatekeyfrompkcs12.GetPrivateKeyFromPKCS12.main(GetPrivateKeyFromPKCS12.java:21)

Caused by: java.security.UnrecoverableKeyException: failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.

SOLVED
you must enter the passphrase correctly.

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");
    }


Tuesday, 14 April 2020

[SOLVED] Invalid AES key length: 31 bytes

PROBLEM :
Invalid AES key length: 31 bytes

CAUSE
key length AES256 must 32 bytes

SOLVED
Edit key until key length 32 bytes