package krypto;

import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

/**
 *
 * @author kama
 */
public class asymmetric {

    private PublicKey pub_key = null;
    private PrivateKey priv_key = null;
    private String algorithm = null;
     private KeyFactory keyFactory=null;


    Cipher cipher = null;
//generowanie kluczy
    public asymmetric(String alg) throws NoSuchAlgorithmException, NoSuchPaddingException {
        this.algorithm = alg;
        KeyPairGenerator kp = KeyPairGenerator.getInstance(this.algorithm);
        kp.initialize(1024);
        KeyPair key=kp.genKeyPair();
        this.priv_key = key.getPrivate();
        this.pub_key = key.getPublic();
        this.cipher = Cipher.getInstance(this.algorithm);
        this.keyFactory=KeyFactory.getInstance(this.algorithm);
    }
    //zwraca klucz publiczny w bajtach
    public PublicKey getPublic(){
        return pub_key;
    }
    //zwraca klucz prywatny w bajtach
    public PrivateKey getPriv(){
        return priv_key;
    }
//szyfrowanie zwraca bajty
    public byte[] enc_Cipher(byte[] data, PrivateKey priv) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        this.cipher.init(Cipher.ENCRYPT_MODE, priv);
        return this.cipher.doFinal(data);
    }
//deszyfrowanie zwraca bajty
    public byte[] dec_Cipher(byte[] ciph, PublicKey pub) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        this.cipher.init(Cipher.DECRYPT_MODE, pub);
        return this.cipher.doFinal(ciph);
    }