package com.decentralizeddatabase.reno;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

/*Initialize and use this class to deal with block encryption and decryption*/
public class CryptoBlock {

    private Cipher cipher;

    public CryptoBlock() throws NoSuchPaddingException, NoSuchAlgorithmException {
        this.cipher = Cipher.getInstance("AES");
    }

    /*
        Inputs: String (data block), String (16 byte secret key)
        Outputs: byte[] (encrypted data block)
     */
    public byte[] encrypt(String block, String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, aesKey);
        byte[] encrypted = cipher.doFinal(block.getBytes());
        return encrypted;
    }

    /*
        Inputs: byte[] (encrypted data block), String (16 byte secret key)
        Outputs: String (decrypted data block)
     */
    public String decrypt(byte[] block, String key) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
        cipher.init(Cipher.DECRYPT_MODE, aesKey);
        String decrypted = new String(cipher.doFinal(block));
        return decrypted;
    }
}

@@ -354,87 +354,8 @@
 n;%0A%0A
-/*Initialize and use this class to deal with block encryption and decryption*/%0A
 publ
@@ -390,16 +390,22 @@
 private 
+final 
 Cipher c
@@ -549,39 +549,40 @@
 );%0A    %7D%0A%0A    /*
+*
 %0A     
- 
+*
   Inputs: String
@@ -621,33 +621,33 @@
 ecret key)%0A     
- 
+*
   Outputs: byte%5B
@@ -709,21 +709,33 @@
 ypt(
-String
+final byte%5B%5D
  block,
+ final
  Str
@@ -766,32 +766,43 @@
 ddingException, 
+%0A%09%09%09%09%09%09%09   
 NoSuchAlgorithmE
@@ -811,16 +811,27 @@
 eption, 
+%0A%09%09%09%09%09%09%09   
 InvalidK
@@ -835,32 +835,43 @@
 idKeyException, 
+%0A%09%09%09%09%09%09%09   
 BadPaddingExcept
@@ -867,32 +867,43 @@
 ddingException, 
+%0A%09%09%09%09%09%09%09   
 IllegalBlockSize
@@ -914,32 +914,38 @@
 ption %7B%0A        
+final 
 Key aesKey = new
@@ -1041,16 +1041,22 @@
         
+final 
 byte%5B%5D e
@@ -1086,27 +1086,16 @@
 al(block
-.getBytes()
 );%0A     
@@ -1128,22 +1128,24 @@
 %0A%0A    /*
+*
 %0A     
+*
    Input
@@ -1211,16 +1211,17 @@
 y)%0A     
+*
    Outpu
@@ -1288,16 +1288,22 @@
 decrypt(
+final 
 byte%5B%5D b
@@ -1307,16 +1307,22 @@
 %5D block,
+ final
  String 
@@ -1354,16 +1354,27 @@
 eption, 
+%0A%09%09%09%09%09%09%09   
 BadPaddi
@@ -1386,16 +1386,27 @@
 eption, 
+%0A%09%09%09%09%09%09%09   
 IllegalB

Change encrypt to use byte array