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

Laboratory 14:15

parent 0462491b
Branches 2022-14_15
No related merge requests found
Pipeline #94 failed with stages
in 0 seconds
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);
}
}
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);
}
}
}
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]));
}
}
}
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());
}
}
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());
}
}
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());
}
}
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