Skip to content
Snippets Groups Projects
AES.java 1.06 KiB
Newer Older
20041679 .'s avatar
20041679 . committed
import it.uniupo.byteLib.Tools;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class AES {
    public byte[] ctr(byte[] pt) {
        Cipher c;
        try {
            c = Cipher.getInstance("AES/CTR/NoPadding");
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            throw new RuntimeException(e);
        }
        KeyGenerator keyGenerator;
        try {
            keyGenerator = KeyGenerator.getInstance("AES");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        var key = keyGenerator.generateKey();
        Tools.writeByteFlow(key.getEncoded(), "key");
        try {
            c.init(Cipher.ENCRYPT_MODE, key);
        } catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }
        try {
            c.doFinal(pt);
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            throw new RuntimeException(e);
        }
    }
}