// Collection of code snippets by Arne VajhÃ¸j 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
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.util.Random;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class RSA3DES {
    public static void main(String[] args) {
        try {
            String s = "ABC DEF GHIJ 123 - ÆØÅæøå - dette er en lang tekst - " +
                       "som gerne skulle være længere end 117 tegn - bla bla " +
                       "bla bla bla bla bla bla bla bla bla bla bla bla bla " +
                       "bla bla bla bla bla bla bla bla bla bla bla bla bla " +
                       "bla bla bla bla bla bla bla bla bla bla bla bla bla";
            byte[] plain = s.getBytes("ISO-8859-1");
            System.out.println(s);
            Random rng = new Random();
            byte[] key = new byte[24];
            rng.nextBytes(key);
            SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DESede");
            SecretKey secretKey = secretKeyFactory.generateSecret(new DESedeKeySpec(key));
            Cipher realcipher = Cipher.getInstance("DESede");
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(1024);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            PrivateKey privateKey = keyPair.getPrivate();
            PublicKey publicKey = keyPair.getPublic();
            Cipher cipher = Cipher.getInstance("RSA");
            realcipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] code = realcipher.doFinal(plain);
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] codekey = cipher.doFinal(secretKey.getEncoded());
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] key2 = cipher.doFinal(codekey);
            realcipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key2, "DESede"));
            byte[] plain2 = realcipher.doFinal(code);
            String s2 = new String(plain2, "ISO-8859-1");
            System.out.println(s2);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
    }
}
