diff --git a/.gitignore b/.gitignore index 1ccc1d34fd459c051a762e9864ec9f66b583d661..f1bf236b7fd9e586789ca2670816c81c7fcbdfaa 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ derby.log /db/ /output/ +/keys.rsa diff --git a/pom.enc b/pom.enc new file mode 100644 index 0000000000000000000000000000000000000000..af311df8b979876ecbaf39c812da821adc49625d Binary files /dev/null and b/pom.enc differ diff --git a/pom.txt b/pom.txt new file mode 100644 index 0000000000000000000000000000000000000000..394826340bc44f23b8dc15fb98af2c7c7fe28e9b --- /dev/null +++ b/pom.txt @@ -0,0 +1,89 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>java2.koz01</groupId> + <artifactId>lab13</artifactId> + <version>0.0.1-SNAPSHOT</version> + <packaging>jar</packaging> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <maven.compiler.source>15</maven.compiler.source> + <maven.compiler.target>15</maven.compiler.target> + <maven.compiler.version>3.8.1</maven.compiler.version> + <javafx.version>15.0.1</javafx.version> + <junit.version>5.7.1</junit.version> + <log4j.version>2.14.1</log4j.version> + <lombok.version>1.18.20</lombok.version> + </properties> + <dependencies> + <dependency> + <groupId>org.openjfx</groupId> + <artifactId>javafx-controls</artifactId> + <version>${javafx.version}</version> + </dependency> + <dependency> + <groupId>org.openjfx</groupId> + <artifactId>javafx-fxml</artifactId> + <version>${javafx.version}</version> + </dependency> + <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-api</artifactId> + <version>${junit.version}</version> + <scope>test</scope> + </dependency> + <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine --> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <version>${junit.version}</version> + <scope>test</scope> + </dependency> + <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params --> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-params</artifactId> + <version>${junit.version}</version> + <scope>test</scope> + </dependency> + <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <version>${lombok.version}</version> + <scope>provided</scope> + </dependency> + <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <version>${log4j.version}</version> + </dependency> + <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <version>${log4j.version}</version> + </dependency> + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>${maven.compiler.version}</version> + <configuration> + <annotationProcessorPaths> + <path> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <version>${lombok.version}</version> + </path> + </annotationProcessorPaths> + </configuration> + </plugin> + </plugins> + </build> +</project> diff --git a/pom.xml b/pom.xml index 394826340bc44f23b8dc15fb98af2c7c7fe28e9b..764e23e753e6f1edf4e0106156c455c1462840ac 100644 --- a/pom.xml +++ b/pom.xml @@ -50,40 +50,47 @@ </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> - <groupId>org.projectlombok</groupId> - <artifactId>lombok</artifactId> - <version>${lombok.version}</version> - <scope>provided</scope> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <version>${lombok.version}</version> + <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-api</artifactId> - <version>${log4j.version}</version> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <version>${log4j.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-core</artifactId> - <version>${log4j.version}</version> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <version>${log4j.version}</version> </dependency> + <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> + <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>2.8.0</version> + </dependency> + </dependencies> <build> <plugins> <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <version>${maven.compiler.version}</version> - <configuration> - <annotationProcessorPaths> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>${maven.compiler.version}</version> + <configuration> + <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </path> </annotationProcessorPaths> - </configuration> - </plugin> + </configuration> + </plugin> </plugins> </build> </project> diff --git a/src/main/java/java2/koz01/lab13/Lab13Decrypt.java b/src/main/java/java2/koz01/lab13/Lab13Decrypt.java index 1af91c75c92ce91e8648a3380e0671a231ba143a..c7ab5e6c8f9a7189ef69ae17d0cce2cf43c1ffad 100644 --- a/src/main/java/java2/koz01/lab13/Lab13Decrypt.java +++ b/src/main/java/java2/koz01/lab13/Lab13Decrypt.java @@ -1,3 +1,4 @@ + package java2.koz01.lab13; import java.io.FileInputStream; @@ -10,13 +11,13 @@ 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 org.apache.commons.io.input.BoundedInputStream; + import lombok.extern.log4j.Log4j2; @Log4j2 @@ -24,34 +25,35 @@ public class Lab13Decrypt { public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IOException, - InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException + InvalidKeySpecException { 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])) + try (InputStream nis = new FileInputStream(args[0]); + OutputStream os = new FileOutputStream(args[1])) { - byte [] buffer = new byte[1024]; - int len; - while (-1 != (len = is.read(buffer))) { - os.write(buffer, 0, len); + Cipher cipher = Cipher.getInstance("AES"); + Cipher aCipher = Cipher.getInstance("RSA"); + aCipher.init(Cipher.DECRYPT_MODE, kp.getPrivate()); + try (InputStream rsaIs = new CipherInputStream( + new NoClosingFilterInputStream(new BoundedInputStream(nis, 256)), + aCipher)) + { + byte[] keyData = new byte[32]; + rsaIs.read(keyData); + SecretKeySpec skp = new SecretKeySpec(keyData, "AES"); + cipher.init(Cipher.DECRYPT_MODE, skp); + } + try (InputStream is = new CipherInputStream( + new NoClosingFilterInputStream(nis), cipher);) + { + 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 index 6acb7679c8ee6d6a7d7ff18ed37fa1cebad4b245..46ae590728edf3483c9359d27e211a703d8545a2 100644 --- a/src/main/java/java2/koz01/lab13/Lab13Encrypt.java +++ b/src/main/java/java2/koz01/lab13/Lab13Encrypt.java @@ -10,10 +10,8 @@ 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; @@ -25,7 +23,7 @@ public class Lab13Encrypt { public static void main(String[] args) throws NoSuchAlgorithmException, IOException, NoSuchPaddingException, InvalidKeyException, - InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException + InvalidKeySpecException { KeyPairLoader keyPairLoader = new KeyPairLoader(); @@ -54,10 +52,12 @@ public class Lab13Encrypt { 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(); + + try (OutputStream rsaOs = new CipherOutputStream( + new NoClosingFilterOutputStream(nos), aCipher)) + { + rsaOs.write(sk.getEncoded()); + } int len; while (-1 != (len = is.read(buffer))) { os.write(buffer, 0, len); diff --git a/src/main/java/java2/koz01/lab13/NoClosingFilterInputStream.java b/src/main/java/java2/koz01/lab13/NoClosingFilterInputStream.java new file mode 100644 index 0000000000000000000000000000000000000000..99662430664e3f3d4a68421e0d33486ebca358a7 --- /dev/null +++ b/src/main/java/java2/koz01/lab13/NoClosingFilterInputStream.java @@ -0,0 +1,18 @@ +package java2.koz01.lab13; + +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; + +class NoClosingFilterInputStream extends FilterInputStream { + + public NoClosingFilterInputStream(InputStream in) { + super(in); + } + + @Override + public void close() throws IOException { + + } + +} diff --git a/src/main/java/java2/koz01/lab13/NoClosingFilterOutputStream.java b/src/main/java/java2/koz01/lab13/NoClosingFilterOutputStream.java new file mode 100644 index 0000000000000000000000000000000000000000..eeef692361b9bb2629289acae8329f802ea76466 --- /dev/null +++ b/src/main/java/java2/koz01/lab13/NoClosingFilterOutputStream.java @@ -0,0 +1,18 @@ +package java2.koz01.lab13; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +class NoClosingFilterOutputStream extends FilterOutputStream { + + public NoClosingFilterOutputStream(OutputStream out) { + super(out); + } + + @Override + public void close() throws IOException { + flush(); + } + +} diff --git a/src/main/java/java2/koz01/lab13/OutputStreamApp.java b/src/main/java/java2/koz01/lab13/OutputStreamApp.java new file mode 100644 index 0000000000000000000000000000000000000000..f321e106d58b40d790c12c48be6cf258cb25ab73 --- /dev/null +++ b/src/main/java/java2/koz01/lab13/OutputStreamApp.java @@ -0,0 +1,42 @@ +package java2.koz01.lab13; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.security.InvalidKeyException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; + +import javax.crypto.Cipher; +import javax.crypto.CipherOutputStream; +import javax.crypto.NoSuchPaddingException; + +import lombok.extern.log4j.Log4j2; + +@Log4j2 +public class OutputStreamApp { + + public static void main(String[] args) throws NoSuchAlgorithmException, + NoSuchPaddingException, FileNotFoundException, IOException, + InvalidKeyException + { + KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); + kpg.initialize(2048); + KeyPair kp = kpg.generateKeyPair(); + + log.info("private: {}", kp.getPrivate()); + log.info("public: {}", kp.getPublic()); + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, kp.getPublic()); + for (int i = 200; i < 257; i++) { + try (OutputStream os = new CipherOutputStream(new FileOutputStream("out" + + i + ".enc"), cipher)) + { + byte[] data = new byte[i]; + os.write(data); + } + } + } +} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index b9bed9f0f434701031afa08f9d1b1b8b03df2efd..415a9ea919ec0eac0647d7a81d4790bd7d844074 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -3,4 +3,5 @@ module java2.koz01.lab13 { requires lombok; requires org.apache.logging.log4j; + requires org.apache.commons.io; } \ No newline at end of file