Skip to content
Snippets Groups Projects
Commit 24502039 authored by koz01's avatar koz01
Browse files

Laboratory work

parent 0462491b
Branches 2022-10_45
No related merge requests found
Pipeline #90 failed with stages
in 0 seconds
package java2.koz01.lab12;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class Prog1 {
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(256);
SecretKey sk = kg.generateKey();
try (OutputStream os = Files.newOutputStream(Paths.get(args[0]))) {
byte[] data = sk.getEncoded();
os.write(data);
}
}
}
package java2.koz01.lab12;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Prog2 {
public static void main(String[] args)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {
byte[] data = Files.readAllBytes(Paths.get(args[1])); // nacist ze souboru
SecretKey sk = new SecretKeySpec(data, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sk);
try (OutputStream os = new CipherOutputStream(Files.newOutputStream(Paths.get(args[2])), cipher);
InputStream is = Files.newInputStream(Paths.get(args[0]))) {
byte[] buffer = new byte[1024];
int len;
while (-1 != (len = is.read(buffer))) {
os.write(buffer, 0, len);
}
}
}
}
package java2.koz01.lab12;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Prog3 {
public static void main(String[] args)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {
byte[] data = Files.readAllBytes(Paths.get(args[1])); // nacist ze souboru
SecretKey sk = new SecretKeySpec(data, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sk);
try (OutputStream os = Files.newOutputStream(Paths.get(args[2]));
InputStream is = new CipherInputStream(Files.newInputStream(Paths.get(args[0])), cipher)) {
byte[] buffer = new byte[1024];
int len;
while (-1 != (len = is.read(buffer))) {
os.write(buffer, 0, len);
}
}
}
}
package java2.koz01.lab12;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.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 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 Prog4 {
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
KeyGenerator kgaes = KeyGenerator.getInstance("AES");
kgaes.init(256);
SecretKey sk = kgaes.generateKey();
KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA");
kg.initialize(4096);
KeyPair kp = kg.generateKeyPair();
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pub = kf.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class);
RSAPrivateKeySpec priv = kf.getKeySpec(kp.getPrivate(), RSAPrivateKeySpec.class);
writeToFile(args[1] + ".pub", pub.getModulus(), pub.getPublicExponent());
writeToFile(args[1] + ".priv", priv.getModulus(), priv.getPrivateExponent());
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, kp.getPublic());
byte[] encrypted_key = cipher.doFinal(sk.getEncoded());
Cipher cipher2 = Cipher.getInstance("AES");
cipher2.init(Cipher.ENCRYPT_MODE, sk);
try (OutputStream os = new CipherOutputStream(Files.newOutputStream(Paths.get(args[2])), cipher2);
InputStream is = Files.newInputStream(Paths.get(args[0]))) {
byte[] buffer = new byte[1024];
int len;
while (-1 != (len = is.read(buffer))) {
os.write(buffer, 0, len);
}
}
try (OutputStream os = Files.newOutputStream(Paths.get(args[3]))) {
os.write(encrypted_key);
}
}
private static void writeToFile(String string, BigInteger modulus, BigInteger exponent) throws IOException {
try (BufferedWriter bw = Files.newBufferedWriter(Paths.get(string))) {
bw.write(modulus.toString());
bw.newLine();
bw.write(exponent.toString());
}
}
}
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