From a1901f53c12ef44bd05742fdb407dccc826c5143 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz>
Date: Mon, 3 May 2021 15:53:20 +0200
Subject: [PATCH] solution

---
 .../java/java2/koz01/lab13/KeyPairLoader.java | 33 +++++++++
 .../java/java2/koz01/lab13/Lab13Decrypt.java  | 59 ++++++++++++++++
 .../java/java2/koz01/lab13/Lab13Encrypt.java  | 68 +++++++++++++++++++
 .../java2/koz01/lab13/Lab13GenerateKeys.java  | 44 ++++++++++++
 4 files changed, 204 insertions(+)
 create mode 100644 src/main/java/java2/koz01/lab13/KeyPairLoader.java
 create mode 100644 src/main/java/java2/koz01/lab13/Lab13Decrypt.java
 create mode 100644 src/main/java/java2/koz01/lab13/Lab13Encrypt.java
 create mode 100644 src/main/java/java2/koz01/lab13/Lab13GenerateKeys.java

diff --git a/src/main/java/java2/koz01/lab13/KeyPairLoader.java b/src/main/java/java2/koz01/lab13/KeyPairLoader.java
new file mode 100644
index 0000000..2846724
--- /dev/null
+++ b/src/main/java/java2/koz01/lab13/KeyPairLoader.java
@@ -0,0 +1,33 @@
+package java2.koz01.lab13;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.math.BigInteger;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.security.KeyFactory;
+import java.security.KeyPair;
+import java.security.NoSuchAlgorithmException;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.RSAPrivateKeySpec;
+import java.security.spec.RSAPublicKeySpec;
+
+public class KeyPairLoader {
+
+	private KeyFactory kf;
+
+	public KeyPairLoader() throws NoSuchAlgorithmException {
+		kf = KeyFactory.getInstance("RSA");
+	}
+
+	public KeyPair load(String file) throws IOException, InvalidKeySpecException {
+		try (BufferedReader br = Files.newBufferedReader(Paths.get(file))) {
+			BigInteger modulus = new BigInteger(br.readLine());
+			BigInteger pubExp = new BigInteger(br.readLine());
+			BigInteger privExp = new BigInteger(br.readLine());
+			RSAPrivateKeySpec priv = new RSAPrivateKeySpec(modulus, privExp);
+			RSAPublicKeySpec pub = new RSAPublicKeySpec(modulus, pubExp);
+			return new KeyPair(kf.generatePublic(pub), kf.generatePrivate(priv));
+		}
+	}
+}
diff --git a/src/main/java/java2/koz01/lab13/Lab13Decrypt.java b/src/main/java/java2/koz01/lab13/Lab13Decrypt.java
new file mode 100644
index 0000000..1af91c7
--- /dev/null
+++ b/src/main/java/java2/koz01/lab13/Lab13Decrypt.java
@@ -0,0 +1,59 @@
+package java2.koz01.lab13;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.security.InvalidKeyException;
+import java.security.KeyPair;
+import java.security.NoSuchAlgorithmException;
+import java.security.spec.InvalidKeySpecException;
+
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.CipherInputStream;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.spec.SecretKeySpec;
+
+import lombok.extern.log4j.Log4j2;
+
+@Log4j2
+public class Lab13Decrypt {
+
+	public static void main(String[] args) throws InvalidKeyException,
+		NoSuchAlgorithmException, NoSuchPaddingException, IOException,
+		InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException
+	{
+
+		KeyPairLoader keyPairLoader = new KeyPairLoader();
+		KeyPair kp = keyPairLoader.load("keys.rsa");
+
+		
+
+		InputStream nis = new FileInputStream(args[0]);
+		Cipher aCipher = Cipher.getInstance("RSA");
+		aCipher.init(Cipher.DECRYPT_MODE, kp.getPrivate());
+		byte[] encrypted = new byte[256];
+		nis.read(encrypted);
+		byte[] keyData = aCipher.doFinal(encrypted);
+		SecretKeySpec skp = new SecretKeySpec(keyData, "AES");
+
+		Cipher cipher = Cipher.getInstance("AES");
+		cipher.init(Cipher.DECRYPT_MODE, skp);
+		
+
+		try (InputStream is = new CipherInputStream(nis,
+			cipher);
+				 OutputStream os = new FileOutputStream(args[1]))
+		{
+			byte [] buffer = new byte[1024];
+			int len;
+			while (-1 != (len = is.read(buffer))) {
+				os.write(buffer, 0, len);
+			}
+		}
+		log.info("decrypted");
+	}
+}
diff --git a/src/main/java/java2/koz01/lab13/Lab13Encrypt.java b/src/main/java/java2/koz01/lab13/Lab13Encrypt.java
new file mode 100644
index 0000000..6acb767
--- /dev/null
+++ b/src/main/java/java2/koz01/lab13/Lab13Encrypt.java
@@ -0,0 +1,68 @@
+package java2.koz01.lab13;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.security.InvalidKeyException;
+import java.security.KeyPair;
+import java.security.NoSuchAlgorithmException;
+import java.security.spec.InvalidKeySpecException;
+
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.CipherOutputStream;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.KeyGenerator;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.SecretKey;
+
+import lombok.extern.log4j.Log4j2;
+
+@Log4j2
+public class Lab13Encrypt {
+
+	public static void main(String[] args) throws NoSuchAlgorithmException,
+		IOException, NoSuchPaddingException, InvalidKeyException,
+		InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException
+	{
+
+		KeyPairLoader keyPairLoader = new KeyPairLoader();
+		KeyPair kp = keyPairLoader.load("keys.rsa");
+		KeyGenerator kg = KeyGenerator.getInstance("AES");
+		kg.init(256);
+		SecretKey sk = kg.generateKey();
+
+
+		/*try (OutputStream os = Files.newOutputStream(Paths.get("secretkey.aes"))) {
+			os.write(sk.getEncoded());
+		}*/
+
+
+		Cipher cipher = Cipher.getInstance("AES");
+		cipher.init(Cipher.ENCRYPT_MODE, sk);
+		// 1. argument - input
+		// 2. argument - output
+		byte[] buffer = new byte[1024];
+		OutputStream nos;
+		try (InputStream is = new FileInputStream(args[0]);
+				OutputStream os = new CipherOutputStream(nos = new FileOutputStream(
+					args[1]),
+					cipher))
+		{
+			Cipher aCipher = Cipher.getInstance("RSA");
+			aCipher.init(Cipher.ENCRYPT_MODE, kp.getPublic());
+			log.info("size of key {}", sk.getEncoded().length);
+			byte[] encrypted = aCipher.doFinal(sk.getEncoded());
+			log.info("size of encrypted {}", encrypted.length);
+			nos.write(encrypted);
+			nos.flush();
+			int len;
+			while (-1 != (len = is.read(buffer))) {
+				os.write(buffer, 0, len);
+			}
+		}
+		log.info("encryption done!");
+	}
+}
diff --git a/src/main/java/java2/koz01/lab13/Lab13GenerateKeys.java b/src/main/java/java2/koz01/lab13/Lab13GenerateKeys.java
new file mode 100644
index 0000000..40f1722
--- /dev/null
+++ b/src/main/java/java2/koz01/lab13/Lab13GenerateKeys.java
@@ -0,0 +1,44 @@
+package java2.koz01.lab13;
+
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.security.KeyFactory;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.RSAPrivateKeySpec;
+import java.security.spec.RSAPublicKeySpec;
+
+import lombok.extern.log4j.Log4j2;
+
+@Log4j2
+public class Lab13GenerateKeys {
+
+	public static void main(String[] args) throws NoSuchAlgorithmException,
+		IOException, InvalidKeySpecException
+	{
+
+		KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
+		kpg.initialize(2048);
+		KeyPair kp = kpg.generateKeyPair();
+
+		log.info("private: {}", kp.getPrivate());
+		log.info("public: {}", kp.getPublic());
+		KeyFactory kf = KeyFactory.getInstance("RSA");
+		RSAPrivateKeySpec pri = kf.getKeySpec(kp.getPrivate(),
+			RSAPrivateKeySpec.class);
+		RSAPublicKeySpec pub = kf.getKeySpec(kp.getPublic(),
+			RSAPublicKeySpec.class);
+
+		try (BufferedWriter bw = Files.newBufferedWriter(Paths.get("keys.rsa"))) {
+			bw.write(pub.getModulus().toString());
+			bw.newLine();
+			bw.write(pub.getPublicExponent().toString());
+			bw.newLine();
+			bw.write(pri.getPrivateExponent().toString());
+		}
+	}
+}
-- 
GitLab