Added AES class

This commit is contained in:
Marcin Kostrzewski 2020-11-16 12:11:03 +01:00
parent 21010a04e5
commit 131b296bb1

View File

@ -0,0 +1,40 @@
package Main;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
public class AES {
public static byte[] encrypt(byte[] data, SecretKey secretKey, byte[] iv)
{
try
{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
return cipher.doFinal(data);
}
catch (Exception e)
{
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
public static byte[] decrypt(byte[] data, SecretKey secretKey, byte[] iv)
{
try
{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
return cipher.doFinal(data);
}
catch (Exception e)
{
System.out.println("Error while decrypting: " + e.toString());
}
return null;
}
}