// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package encrypt;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
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 java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class TestAsymmetric {
    // encrypt: byte[] plain + byte[] private key -> byte[] cipher
    // decrypt: byte[] cipher + byte[] public key -> byte[] plain
    public static byte[] encrypt(String algchainpad, String alg, byte[] plain, byte[] serprivkey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
        KeyFactory kf = KeyFactory.getInstance(alg);
        PrivateKey privkey = kf.generatePrivate(new PKCS8EncodedKeySpec(serprivkey));
        Cipher c = Cipher.getInstance(algchainpad);
        c.init(Cipher.ENCRYPT_MODE, privkey);
        return c.doFinal(plain);
    }
    public static byte[] decrypt(String algchainpad, String alg, byte[] cipher, byte[] serpubkey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
        KeyFactory kf = KeyFactory.getInstance(alg);
        PublicKey pubkey = kf.generatePublic(new X509EncodedKeySpec(serpubkey));
        Cipher c = Cipher.getInstance(algchainpad);
        c.init(Cipher.DECRYPT_MODE, pubkey);
        return c.doFinal(cipher);
    }
    // Base64 encode: byte[] -> String
    // Base64 decode: String -> byte[]
    private static String encode(byte[] b) {
        return Base64.getEncoder().encodeToString(b);
    }
    private static byte[] decode(String s) {
        return Base64.getDecoder().decode(s);
    }
    // encrypt: String plain + byte[] private key -> Base64 String cipher
    // decrypt: Base64 String cipher + byte[] public key -> String plain
    public static String encrypt(String algchainpad, String alg, String plain, byte[] serprivkey) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
        return encode(encrypt(algchainpad, alg, plain.getBytes("UTF-8"), serprivkey));
    }
    public static String decrypt(String algchainpad, String alg, String cipher, byte[] serpubkey) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
        return new String(decrypt(algchainpad, alg, decode(cipher), serpubkey), "UTF-8");
    }
    // test
    private static final String TEXT = "This is some random text to be used to test encryption.";
    private static void test(String label, String algchainpad, String alg, int bits) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
        System.out.println(label + ":");
        // generate keys
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(alg);
        kpg.initialize(bits);
        KeyPair kp = kpg.generateKeyPair();
        PrivateKey privkey = kp.getPrivate();
        PublicKey pubkey = kp.getPublic();
        byte[] serprivkey = privkey.getEncoded();
        byte[] serpubkey = pubkey.getEncoded();
        // real test
        System.out.println("  " + TEXT);
        String cipher = encrypt(algchainpad, alg, TEXT, serprivkey);
        System.out.println("  " + cipher);
        String plain = decrypt(algchainpad, alg, cipher, serpubkey);
        System.out.println("  " + plain);
    }
    public static void main(String[] args) throws Exception {
        test("RSA 2048 bit", "RSA/ECB/PKCS1Padding", "RSA", 2048);
        test("RSA 3072 bit", "RSA/ECB/PKCS1Padding", "RSA", 3072);
    }
}
