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

init

parent 066602ac
No related merge requests found
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();
}
}
}
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