diff --git a/src/main/java/java2/koz01/lab12/Prog1.java b/src/main/java/java2/koz01/lab12/Prog1.java new file mode 100644 index 0000000000000000000000000000000000000000..604a7c11269bd6004796136b45d5da18f06c1c93 --- /dev/null +++ b/src/main/java/java2/koz01/lab12/Prog1.java @@ -0,0 +1,27 @@ +package java2.koz01.lab12; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.security.NoSuchAlgorithmException; + +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; + +import lombok.extern.log4j.Log4j2; + +@Log4j2 +public class Prog1 { + + public static void main(String[] args) throws NoSuchAlgorithmException, IOException { + KeyGenerator kg = KeyGenerator.getInstance("AES"); + kg.init(256); + SecretKey sk = kg.generateKey(); + byte[] data = sk.getEncoded(); + + Files.write(Paths.get(args[0]), data); + + + } + +} diff --git a/src/main/java/java2/koz01/lab12/Prog2.java b/src/main/java/java2/koz01/lab12/Prog2.java new file mode 100644 index 0000000000000000000000000000000000000000..1e860fbcd37445b8529ea43038762bbfdf6838db --- /dev/null +++ b/src/main/java/java2/koz01/lab12/Prog2.java @@ -0,0 +1,26 @@ +package java2.koz01.lab12; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import javax.crypto.Cipher; +import javax.crypto.CipherOutputStream; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.spec.SecretKeySpec; + +public class Prog2 { + public static void main(String[] args) throws IOException, Throwable, NoSuchPaddingException { + byte[] keydata = Files.readAllBytes(Paths.get(args[0])); + SecretKeySpec sks = new SecretKeySpec(keydata, "AES"); + + Cipher cipher = Cipher.getInstance("AES"); + + cipher.init(Cipher.ENCRYPT_MODE, sks); + + try (var os = new CipherOutputStream(Files.newOutputStream(Paths.get(args[2])), cipher)) { + Files.copy(Paths.get(args[1]), os); + } + + } +} diff --git a/src/main/java/java2/koz01/lab12/Prog3.java b/src/main/java/java2/koz01/lab12/Prog3.java new file mode 100644 index 0000000000000000000000000000000000000000..4a0be92bcfb6a709a865aa8d217dadf81153bab1 --- /dev/null +++ b/src/main/java/java2/koz01/lab12/Prog3.java @@ -0,0 +1,26 @@ +package java2.koz01.lab12; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import javax.crypto.Cipher; +import javax.crypto.CipherInputStream; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.spec.SecretKeySpec; + +public class Prog3 { + public static void main(String[] args) throws IOException, Throwable, NoSuchPaddingException { + byte[] keydata = Files.readAllBytes(Paths.get(args[0])); + SecretKeySpec sks = new SecretKeySpec(keydata, "AES"); + + Cipher cipher = Cipher.getInstance("AES"); + + cipher.init(Cipher.DECRYPT_MODE, sks); + + try (var is = new CipherInputStream(Files.newInputStream(Paths.get(args[1])), cipher)) { + Files.copy(is, Paths.get(args[2])); + } + + } +} diff --git a/src/main/java/java2/koz01/lab12/Prog4.java b/src/main/java/java2/koz01/lab12/Prog4.java new file mode 100644 index 0000000000000000000000000000000000000000..a708e72095dabe4c521a2cfa1f3ff06ea34dcc17 --- /dev/null +++ b/src/main/java/java2/koz01/lab12/Prog4.java @@ -0,0 +1,44 @@ +package java2.koz01.lab12; + +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.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 Prog4 { + public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException { + + KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); + kpg.initialize(4096); + KeyPair kp = kpg.generateKeyPair(); + log.info("public: {}", kp.getPublic()); + log.info("private: {}", kp.getPrivate()); + + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + + RSAPublicKeySpec publicKeySpec = keyFactory.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); + RSAPrivateKeySpec privateKeySpec = keyFactory.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class); + + writeKey(args[0] + ".pub", publicKeySpec.getModulus(), publicKeySpec.getPublicExponent()); + writeKey(args[0] + ".priv", privateKeySpec.getModulus(), privateKeySpec.getPrivateExponent()); + + } + + private static void writeKey(String string, BigInteger modulus, BigInteger exponent) throws IOException { + StringBuffer sb = new StringBuffer(); + sb.append(modulus.toString()); + sb.append("\n"); + sb.append(exponent.toString()); + Files.writeString(Paths.get(string), sb.toString()); + } +} diff --git a/src/main/java/java2/koz01/lab12/Prog5.java b/src/main/java/java2/koz01/lab12/Prog5.java new file mode 100644 index 0000000000000000000000000000000000000000..06b9c47b403ac9dd3468b4eb0efc5f50a41719e2 --- /dev/null +++ b/src/main/java/java2/koz01/lab12/Prog5.java @@ -0,0 +1,60 @@ +package java2.koz01.lab12; + +import java.io.IOException; +import java.math.BigInteger; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.security.InvalidKeyException; +import java.security.KeyFactory; +import java.security.NoSuchAlgorithmException; +import java.security.PublicKey; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.RSAPublicKeySpec; +import java.util.List; +import java.util.stream.Collectors; + +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 Prog5 { + public static void main(String[] args) + throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, + InvalidKeyException, IllegalBlockSizeException, BadPaddingException { + List<BigInteger> valsPub = readBigInts(args[0] + ".pub"); + + + KeyFactory kf = KeyFactory.getInstance("RSA"); + PublicKey pk = kf.generatePublic(new RSAPublicKeySpec(valsPub.get(0), valsPub.get(1))); + + log.info("pub: {}", pk); + + KeyGenerator kg = KeyGenerator.getInstance("AES"); + kg.init(256); + SecretKey sk = kg.generateKey(); + byte[] keyData = sk.getEncoded(); + Cipher cipherRSA = Cipher.getInstance("RSA"); + cipherRSA.init(Cipher.ENCRYPT_MODE, pk); + byte[] encKey = cipherRSA.doFinal(keyData); + Files.write(Paths.get(args[1]), encKey); + + Cipher cipherAES = Cipher.getInstance("AES"); + cipherAES.init(Cipher.ENCRYPT_MODE, sk); + try(var os = new CipherOutputStream(Files.newOutputStream(Paths.get(args[3]) ), cipherAES)) { + Files.copy(Paths.get(args[2]), os); + } + } + + private static List<BigInteger> readBigInts(String string) throws IOException { + return Files.readAllLines(Paths.get(string)).stream().map(str -> new BigInteger(str)) + .collect(Collectors.toList()); + + } +} diff --git a/src/main/java/java2/koz01/lab12/Prog6.java b/src/main/java/java2/koz01/lab12/Prog6.java new file mode 100644 index 0000000000000000000000000000000000000000..5c48e0770e72a40c5364acdf5e2a1da9f6b3a0f9 --- /dev/null +++ b/src/main/java/java2/koz01/lab12/Prog6.java @@ -0,0 +1,58 @@ +package java2.koz01.lab12; + +import java.io.IOException; +import java.math.BigInteger; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.security.InvalidKeyException; +import java.security.KeyFactory; +import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.RSAPrivateKeySpec; +import java.util.List; +import java.util.stream.Collectors; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.CipherOutputStream; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; + +import lombok.extern.log4j.Log4j2; + +@Log4j2 +public class Prog6 { + public static void main(String[] args) + throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, + InvalidKeyException, IllegalBlockSizeException, BadPaddingException { + List<BigInteger> valsPub = readBigInts(args[0] + ".priv"); + + + KeyFactory kf = KeyFactory.getInstance("RSA"); + PrivateKey pk = kf.generatePrivate(new RSAPrivateKeySpec(valsPub.get(0), valsPub.get(1))); + + log.info("priv: {}", pk); + + byte[] encKey = Files.readAllBytes(Paths.get(args[1])); + + Cipher cipherRSA = Cipher.getInstance("RSA"); + cipherRSA.init(Cipher.DECRYPT_MODE, pk); + byte[] keyData = cipherRSA.doFinal(encKey); + SecretKey sk = new SecretKeySpec(keyData, "AES"); + + Cipher cipherAES = Cipher.getInstance("AES"); + cipherAES.init(Cipher.DECRYPT_MODE, sk); + try(var os = new CipherOutputStream(Files.newOutputStream(Paths.get(args[3]) ), cipherAES)) { + Files.copy(Paths.get(args[2]), os); + } + } + + private static List<BigInteger> readBigInts(String string) throws IOException { + return Files.readAllLines(Paths.get(string)).stream().map(str -> new BigInteger(str)) + .collect(Collectors.toList()); + + } +}