Added AES class
This commit is contained in:
parent
21010a04e5
commit
131b296bb1
40
src/main/java/Main/AES.java
Normal file
40
src/main/java/Main/AES.java
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user