/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package encryption;


import mdot.Main;
import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.PBEParametersGenerator;
import org.bouncycastle.crypto.engines.AESLightEngine;
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;


/**
 * class pbeEncryption
 * Handles encryping context data before it is sent via UDP.
 * Uses Password Based Encryption.
 */
public class pbeEncryption {

    private static byte[] salt = {(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
                                  (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03};
    private static int iteration = 19;

    public static byte[] encrypt(String password, String input) {
        String pass = password;
        //String in = input;
        //byte[] result = null;
         byte[] textByteArray = null;
         byte[] cipherByteArray = null;
        try {


            PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
            generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(pass.toCharArray()), salt, iteration);
            CipherParameters key = generator.generateDerivedParameters(128);

            BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESLightEngine()));
            cipher.init(true, key);

            textByteArray = input.getBytes();
            cipherByteArray = new byte[cipher.getOutputSize(textByteArray.length)];

            int length = cipher.processBytes(textByteArray, 0, textByteArray.length, cipherByteArray, 0);
            cipher.doFinal(cipherByteArray, length);

       
 
        }catch(Exception ex){
            ex.printStackTrace();
        }

        //return result;
        return cipherByteArray;
    }

     public static byte[] decrypt(byte[] input) {
        String pass = Main.password;
        //byte[] in = input;
        //byte[] result= null;
        byte [] cipherByteArray = null;
        byte [] textByteArray = null;
        try {
            PBEParametersGenerator generator = new PKCS5S2ParametersGenerator();
            generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(pass.toCharArray()), salt, iteration);
            CipherParameters key = generator.generateDerivedParameters(128);

             BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESLightEngine()));
            cipher.init(false, key);


            cipherByteArray = input;
            textByteArray = new byte[cipher.getOutputSize(cipherByteArray.length)];

            int length = cipher.processBytes(cipherByteArray, 0, cipherByteArray.length, textByteArray, 0);
            cipher.doFinal(textByteArray, length);

          
        }catch(Exception ex){
            ex.printStackTrace();
        }

        //return result;
        return textByteArray;
    }
}
