package krypto;

/**
 *
 * @author kama
 *
 */
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;


public class symetric {

    private String algorithm;
    private Cipher cipher;
    private SecretKey key;


    public symetric(String algorithm) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
        this.algorithm = algorithm;
        this.cipher = Cipher.getInstance(algorithm);
        KeyGenerator kg=KeyGenerator.getInstance(algorithm);
        this.key = kg.generateKey();
        this.cipher = Cipher.getInstance(algorithm);
    }

    public void setAlgorithm(String alg){
        this.algorithm=alg;
    }
//zwraca wygenerowany klucz w bajtach
    public byte[] get_key() {
        return this.key.getEncoded();
    }
//szyfrowanie - zwraca bajty
    public byte[] enc_Cipher(byte[] data, SecretKey key) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
        this.cipher.init(Cipher.ENCRYPT_MODE, this.key);
        return this.cipher.doFinal(data);
    }
//deszyfrowanie-- zwraca bajty
    public byte[] dec_Cipher(byte[] data1, SecretKey key) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
        this.cipher.init(Cipher.DECRYPT_MODE, this.key);
        return this.cipher.doFinal(data1);
    }

    public SecretKey getSecretKey(){
        return this.key;
    }