Bael 4485 (#10612)
* BAEL-4485 RSA in Java * BAEL-4485 RSA in Java * BAEL-4485 RSA in Java Co-authored-by: majewsk6 <krzysztof.majewski.km1@contractors.roche.com>
This commit is contained in:
parent
34d4a4032d
commit
9706d7b6fd
@ -0,0 +1,92 @@
|
|||||||
|
package com.baeldung.cipher;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.KeyPairGenerator;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
|
||||||
|
public class RsaUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRsaKeyPair_whenEncryptAndDecryptString_thenCompareResults() throws Exception {
|
||||||
|
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||||
|
generator.initialize(2048);
|
||||||
|
KeyPair pair = generator.generateKeyPair();
|
||||||
|
PrivateKey privateKey = pair.getPrivate();
|
||||||
|
PublicKey publicKey = pair.getPublic();
|
||||||
|
|
||||||
|
String secretMessage = "Baeldung secret message";
|
||||||
|
Cipher encryptCipher = Cipher.getInstance("RSA");
|
||||||
|
encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||||
|
byte[] secretMessageBytes = secretMessage.getBytes(StandardCharsets.UTF_8);
|
||||||
|
byte[] encryptedMessageBytes = encryptCipher.doFinal(secretMessageBytes);
|
||||||
|
|
||||||
|
Cipher decryptCipher = Cipher.getInstance("RSA");
|
||||||
|
decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||||
|
byte[] decryptedMessageBytes = decryptCipher.doFinal(encryptedMessageBytes);
|
||||||
|
String decryptedMessage = new String(decryptedMessageBytes, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
Assertions.assertEquals(secretMessage, decryptedMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRsaKeyPair_whenEncryptAndDecryptFile_thenCompareResults() throws Exception {
|
||||||
|
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||||
|
generator.initialize(2048);
|
||||||
|
KeyPair pair = generator.generateKeyPair();
|
||||||
|
PrivateKey privateKey = pair.getPrivate();
|
||||||
|
PublicKey publicKey = pair.getPublic();
|
||||||
|
|
||||||
|
String originalContent = "some secret message";
|
||||||
|
Path tempFile = Files.createTempFile("temp", "txt");
|
||||||
|
writeString(tempFile, originalContent);
|
||||||
|
|
||||||
|
byte[] fileBytes = Files.readAllBytes(tempFile);
|
||||||
|
Cipher encryptCipher = Cipher.getInstance("RSA");
|
||||||
|
encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||||
|
byte[] encryptedFileBytes = encryptCipher.doFinal(fileBytes);
|
||||||
|
try (FileOutputStream stream = new FileOutputStream(tempFile.toFile())) {
|
||||||
|
stream.write(encryptedFileBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
encryptedFileBytes = Files.readAllBytes(tempFile);
|
||||||
|
Cipher decryptCipher = Cipher.getInstance("RSA");
|
||||||
|
decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||||
|
byte[] decryptedFileBytes = decryptCipher.doFinal(encryptedFileBytes);
|
||||||
|
try (FileOutputStream stream = new FileOutputStream(tempFile.toFile())) {
|
||||||
|
stream.write(decryptedFileBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
String fileContent = readString(tempFile);
|
||||||
|
|
||||||
|
Assertions.assertEquals(originalContent, fileContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeString(Path path, String content) throws Exception {
|
||||||
|
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
|
||||||
|
writer.write(content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String readString(Path path) throws Exception {
|
||||||
|
StringBuilder resultStringBuilder = new StringBuilder();
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(path.toFile()))) {
|
||||||
|
String line;
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
resultStringBuilder.append(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resultStringBuilder.toString();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user