0

I have problem in for loop. I am trying to pass the file in the encryption function to encrypt the file as well as decryption function. I am stuck of how to pass the file. I searched for how to pass the file but I couldn't find a good resource solving this problem. I found this link How to decrypt file in Java encrypted with openssl command using AES? But I could not understand it well. Any help would be appreciated. Thank you

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

public class AES {

int mul = 0;
long begin, end;

SecretKey myAesKey;
Cipher aesCipher;

/**
 * @param args
 */
public static void main(String[] args) {
    AES demo = new AES();
}

public AES() {
    // File file = new File("plainText.txt");
    // FileReader fr = null;
    BufferedReader br = null;
    // try {
    String currentLine;
    // //fr = new FileReader();
    // } catch (FileNotFoundException e1) {
    // // TODO Auto-generated catch block
    // e1.printStackTrace();
    // }

    byte[] key = "01234567".getBytes();
    byte[] plain = "A message".getBytes();
    File file = new File("plainText.txt");
    FileReader fr = null;
    try {
        fr = new FileReader(file);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    br = new BufferedReader(fr);

    String cipher_text;

    long totaltime = 0;
    long begin;
    int i = 0;


    // do operation 1000 times...
    for (i = 0; i < 1000; i++) {
        begin = System.currentTimeMillis();
        try {
            initiateAES();
            cipher_text = Encrypt(aesCipher, br.readLine(),
                 myAesKey).toString();
            System.out.println(cipher_text);
            currentLine = Decrypt(aesCipher, cipher_text,
                 myAesKey).toString();
            System.out.println(new String(currentLine, "UTF-8"));

        } catch (Exception ex) {
            System.out.println(ex);
        }
        // do operation that you want to time in here.....

        totaltime += System.currentTimeMillis() - begin;

    }
    // at end take average nano seconds.
    System.out.println("time:" + totaltime / i);

}

private void initiateAES() {
    try {

        // create a random 128 bit key
        KeyGenerator keygenerator = KeyGenerator.getInstance("AES");
        keygenerator.init(128);
        myAesKey = keygenerator.generateKey();

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

    } catch (Exception ex) {

    }
}

private byte[] Encrypt(Cipher cipher, String b, SecretKey key)
        throws Exception {

    cipher.init(Cipher.ENCRYPT_MODE, key);

    return cipher.doFinal();

}

private byte[] Decrypt(Cipher cipher, String r, SecretKey key)
        throws Exception {

    cipher.init(Cipher.DECRYPT_MODE, key,
            new IvParameterSpec(cipher.getIV()));

    return cipher.doFinal();

}

}

Community
  • 1
  • 1
Harbi
  • 1
  • 3
  • There are a lot of beginner mistakes evident here, not that there's anything wrong with that, we were all beginners once. The toString() method doesn't return a useful value for byte arrays. Most people prefer to output byte arrays for debugging using a hex encoding of the array contents. You need to study up on the differences between character IO and binary IO. – President James K. Polk Feb 17 '15 at 17:02
  • Your code doesn't compile currently, as `currentLine` is already a string. Please try the code for compilation before posting (or use an IDE that does this automatically, such as Eclipse, of course). – Maarten Bodewes Feb 17 '15 at 17:43

0 Answers0