Skip to content
Snippets Groups Projects
AES.java 702 B
Newer Older
20041679 .'s avatar
20041679 . committed
import javax.crypto.*;
20041679 .'s avatar
20041679 . committed
import java.security.Key;
20041679 .'s avatar
20041679 . committed

public class AES {
20041679 .'s avatar
20041679 . committed
    public byte[] encryptCtr(Key key, byte[] pt) {
20041679 .'s avatar
20041679 . committed
        try {
20041679 .'s avatar
20041679 . committed
            Cipher c;
20041679 .'s avatar
20041679 . committed
            c = Cipher.getInstance("AES/CTR/NoPadding");
            c.init(Cipher.ENCRYPT_MODE, key);
20041679 .'s avatar
20041679 . committed
            return c.doFinal(pt);
        } catch (Exception e) {
20041679 .'s avatar
20041679 . committed
            throw new RuntimeException(e);
        }
20041679 .'s avatar
20041679 . committed
    }

    public byte[] decryptCtr(Key key, byte[] ct) {
20041679 .'s avatar
20041679 . committed
        try {
20041679 .'s avatar
20041679 . committed
            Cipher c;
            c = Cipher.getInstance("AES/CTR/NoPadding");
            c.init(Cipher.DECRYPT_MODE, key);
            return c.doFinal(ct);
        } catch (Exception e) {
20041679 .'s avatar
20041679 . committed
            throw new RuntimeException(e);
        }
    }
}