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

init

parents
No related merge requests found
**/target/
/target/
*/target/*
.classpath
.project
.settings
# Package Files #
*.jar
*.war
*.ear
*.iml
*.idea
derby.log
/db/
/output/
pom.xml 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>
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 KeyPairReader {
private KeyFactory factory;
public KeyPairReader() throws NoSuchAlgorithmException {
factory = KeyFactory.getInstance("rsa");
}
public KeyPair readKeys(String fileWithKeys) throws IOException,
InvalidKeySpecException
{
//open file
//Read first line-> modulus
//...
//create RSAPublicKeySpec
try (BufferedReader br = Files.newBufferedReader(Paths.get(fileWithKeys))) {
BigInteger modulus = new BigInteger(br.readLine());
BigInteger publicExponent = new BigInteger(br.readLine());
BigInteger privateExponent = new BigInteger(br.readLine());
RSAPublicKeySpec pub = new RSAPublicKeySpec(modulus, publicExponent);
RSAPrivateKeySpec pri = new RSAPrivateKeySpec(modulus, privateExponent);
return new KeyPair(factory.generatePublic(pub), factory.generatePrivate(
pri));
}
}
}
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.nio.ByteBuffer;
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 Lab13DecryptData {
public static void main(String[] args) throws InvalidKeySpecException,
IOException, NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
if (args.length < 3) {
log.error("Provide key_file input output");
return;
}
KeyPairReader reader = new KeyPairReader();
KeyPair kp = reader.readKeys(args[0]);
try (InputStream is2 = new FileInputStream(args[1]);
OutputStream os = new FileOutputStream(args[2]))
{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, kp.getPrivate());
byte[] data = new byte[256];
is2.read(data);
byte[] decoded = cipher.doFinal(data);
ByteBuffer bb = ByteBuffer.wrap(decoded, 0, 4);
int lenOfKey = bb.getInt();
log.info("len: {}", lenOfKey);
is2.read(data);
decoded = cipher.doFinal(data);
SecretKeySpec skc = new SecretKeySpec(decoded, 0, lenOfKey, "AES");
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skc);
try (CipherInputStream is = new CipherInputStream(is2, cipher)) {
byte[] buffer = new byte[1024];
int len;
while (-1 != (len = is.read(buffer))) {
os.write(buffer, 0, len);
log.info("writed data: {}", len);
}
}
}
}
}
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.nio.ByteBuffer;
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 javax.crypto.spec.SecretKeySpec;
import lombok.extern.log4j.Log4j2;
@Log4j2
public class Lab13EncryptData {
public static void main(String[] args) throws InvalidKeySpecException,
IOException, NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
if (args.length < 3) {
log.error("Provide key_file input output");
return;
}
KeyPairReader reader = new KeyPairReader();
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(256);
KeyPair kp = reader.readKeys(args[0]);
SecretKey sk = kg.generateKey();
SecretKeySpec skc = new SecretKeySpec(sk.getEncoded(), "AES");
try(InputStream is = new FileInputStream(args[1]);
OutputStream os2 = new FileOutputStream(args[2]))
{
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, kp.getPublic());
os2.write(cipher.doFinal(ByteBuffer.allocate(4).putInt(skc
.getEncoded().length).array()));
os2.write(cipher.doFinal(skc.getEncoded()));
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sk);
try (CipherOutputStream os = new CipherOutputStream(os2, cipher)) {
byte[] buffer = new byte[1024];
int len;
while (-1 != (len = is.read(buffer))) {
os.write(buffer, 0, len);
}
}
}
}
}
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,
InvalidKeySpecException, IOException
{
if (args.length <= 0) {
log.error("Provide name of file for output");
return;
}
KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("rsa");
keyGenerator.initialize(2048);
KeyPair kp = keyGenerator.generateKeyPair();
KeyFactory kf = KeyFactory.getInstance("rsa");
RSAPublicKeySpec publicKeySpec = kf.getKeySpec(kp.getPublic(),
RSAPublicKeySpec.class);
RSAPrivateKeySpec privateKeySpec = kf.getKeySpec(kp.getPrivate(),
RSAPrivateKeySpec.class);
log.info("public modulus:{}, public exponent: {}\n {", publicKeySpec
.getModulus(), publicKeySpec.getPublicExponent());
log.info("private modulus:{}, private exponent: {}\n {", privateKeySpec
.getModulus(), privateKeySpec.getPrivateExponent());
try (BufferedWriter bw = Files.newBufferedWriter(Paths.get(args[0]))) {
bw.write(publicKeySpec.getModulus().toString());
bw.newLine();
bw.write(publicKeySpec.getPublicExponent().toString());
bw.newLine();
bw.write(privateKeySpec.getPrivateExponent().toString());
bw.newLine();
}
}
}
module java2.koz01.lab13 {
exports java2.koz01.lab13;
requires lombok;
requires org.apache.logging.log4j;
}
\ No newline at end of file
/* JavaFX CSS - Leave this comment until you have at least create one rule which uses -fx-Property */
\ No newline at end of file
<Configuration>
<Appenders>
<Console name="Console">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"></AppenderRef>
</Root>
</Loggers>
</Configuration>
\ 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