Skip to content
Snippets Groups Projects
Commit 2d20df2c authored by Jan Kožusznik's avatar Jan Kožusznik
Browse files

Use CipherInput(Output)Stream for RSA Cipher

parent a1901f53
No related merge requests found
Pipeline #100 failed with stages
in 0 seconds
......@@ -16,3 +16,4 @@
derby.log
/db/
/output/
/keys.rsa
pom.enc 0 → 100644
File added
pom.txt 0 → 100644
<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>
......@@ -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>
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");
......
......@@ -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);
......
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 {
}
}
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();
}
}
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);
}
}
}
}
......@@ -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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment