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

solution

parent 76ebba8f
No related merge requests found
Pipeline #108 failed with stages
in 0 seconds
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 KeyPairLoader {
private KeyFactory kf;
public KeyPairLoader() throws NoSuchAlgorithmException {
kf = KeyFactory.getInstance("RSA");
}
public KeyPair load(String file) throws IOException, InvalidKeySpecException {
try (BufferedReader br = Files.newBufferedReader(Paths.get(file))) {
BigInteger modulus = new BigInteger(br.readLine());
BigInteger pubExp = new BigInteger(br.readLine());
BigInteger privExp = new BigInteger(br.readLine());
RSAPrivateKeySpec priv = new RSAPrivateKeySpec(modulus, privExp);
RSAPublicKeySpec pub = new RSAPublicKeySpec(modulus, pubExp);
return new KeyPair(kf.generatePublic(pub), kf.generatePrivate(priv));
}
}
}
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.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 Lab13Decrypt {
public static void main(String[] args) throws InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException, IOException,
InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException
{
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]))
{
byte [] buffer = new byte[1024];
int len;
while (-1 != (len = is.read(buffer))) {
os.write(buffer, 0, len);
}
}
log.info("decrypted");
}
}
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.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 lombok.extern.log4j.Log4j2;
@Log4j2
public class Lab13Encrypt {
public static void main(String[] args) throws NoSuchAlgorithmException,
IOException, NoSuchPaddingException, InvalidKeyException,
InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException
{
KeyPairLoader keyPairLoader = new KeyPairLoader();
KeyPair kp = keyPairLoader.load("keys.rsa");
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(256);
SecretKey sk = kg.generateKey();
/*try (OutputStream os = Files.newOutputStream(Paths.get("secretkey.aes"))) {
os.write(sk.getEncoded());
}*/
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sk);
// 1. argument - input
// 2. argument - output
byte[] buffer = new byte[1024];
OutputStream nos;
try (InputStream is = new FileInputStream(args[0]);
OutputStream os = new CipherOutputStream(nos = new FileOutputStream(
args[1]),
cipher))
{
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();
int len;
while (-1 != (len = is.read(buffer))) {
os.write(buffer, 0, len);
}
}
log.info("encryption done!");
}
}
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,
IOException, InvalidKeySpecException
{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair kp = kpg.generateKeyPair();
log.info("private: {}", kp.getPrivate());
log.info("public: {}", kp.getPublic());
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec pri = kf.getKeySpec(kp.getPrivate(),
RSAPrivateKeySpec.class);
RSAPublicKeySpec pub = kf.getKeySpec(kp.getPublic(),
RSAPublicKeySpec.class);
try (BufferedWriter bw = Files.newBufferedWriter(Paths.get("keys.rsa"))) {
bw.write(pub.getModulus().toString());
bw.newLine();
bw.write(pub.getPublicExponent().toString());
bw.newLine();
bw.write(pri.getPrivateExponent().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