|
| 1 | +package com.baeldung.cipher; |
| 2 | + |
| 3 | + |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import java.io.ByteArrayInputStream; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.security.cert.CertificateFactory; |
| 10 | +import java.security.cert.X509Certificate; |
| 11 | + |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; |
| 13 | + |
| 14 | +public class EncryptorUnitTest { |
| 15 | + private String encKeyString; |
| 16 | + private String message; |
| 17 | + private String certificateString; |
| 18 | + private Encryptor encryptor; |
| 19 | + |
| 20 | + @Before |
| 21 | + public void init(){ |
| 22 | + encKeyString = "1234567890123456"; |
| 23 | + message = "This is a secret message"; |
| 24 | + encryptor = new Encryptor(); |
| 25 | + certificateString = "-----BEGIN CERTIFICATE-----\n" + |
| 26 | + "MIICVjCCAb8CAg37MA0GCSqGSIb3DQEBBQUAMIGbMQswCQYDVQQGEwJKUDEOMAwG\n" + |
| 27 | + "A1UECBMFVG9reW8xEDAOBgNVBAcTB0NodW8ta3UxETAPBgNVBAoTCEZyYW5rNERE\n" + |
| 28 | + "MRgwFgYDVQQLEw9XZWJDZXJ0IFN1cHBvcnQxGDAWBgNVBAMTD0ZyYW5rNEREIFdl\n" + |
| 29 | + "YiBDQTEjMCEGCSqGSIb3DQEJARYUc3VwcG9ydEBmcmFuazRkZC5jb20wHhcNMTIw\n" + |
| 30 | + "ODIyMDUyNzIzWhcNMTcwODIxMDUyNzIzWjBKMQswCQYDVQQGEwJKUDEOMAwGA1UE\n" + |
| 31 | + "CAwFVG9reW8xETAPBgNVBAoMCEZyYW5rNEREMRgwFgYDVQQDDA93d3cuZXhhbXBs\n" + |
| 32 | + "ZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMYBBrx5PlP0WNI/ZdzD\n" + |
| 33 | + "+6Pktmurn+F2kQYbtc7XQh8/LTBvCo+P6iZoLEmUA9e7EXLRxgU1CVqeAi7QcAn9\n" + |
| 34 | + "MwBlc8ksFJHB0rtf9pmf8Oza9E0Bynlq/4/Kb1x+d+AyhL7oK9tQwB24uHOueHi1\n" + |
| 35 | + "C/iVv8CSWKiYe6hzN1txYe8rAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAASPdjigJ\n" + |
| 36 | + "kXCqKWpnZ/Oc75EUcMi6HztaW8abUMlYXPIgkV2F7YanHOB7K4f7OOLjiz8DTPFf\n" + |
| 37 | + "jC9UeuErhaA/zzWi8ewMTFZW/WshOrm3fNvcMrMLKtH534JKvcdMg6qIdjTFINIr\n" + |
| 38 | + "evnAhf0cwULaebn+lMs8Pdl7y37+sfluVok=\n" + |
| 39 | + "-----END CERTIFICATE-----"; |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void givenEncryptionKey_whenMessageIsPassedToEncryptor_thenMessageIsEncrypted() throws Exception { |
| 44 | + byte[] encryptedMessage = encryptor.encryptMessage(message.getBytes(),encKeyString.getBytes()); |
| 45 | + |
| 46 | + assertThat(encryptedMessage).isNotNull(); |
| 47 | + assertThat(encryptedMessage.length % 32).isEqualTo(0); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void givenCertificateWithPublicKey_whenMessageIsPassedToEncryptor_thenMessageIsEncrypted() throws Exception { |
| 52 | + CertificateFactory factory = CertificateFactory.getInstance("X.509"); |
| 53 | + InputStream is = new ByteArrayInputStream(certificateString.getBytes()); |
| 54 | + X509Certificate certificate = (X509Certificate) factory.generateCertificate(is); |
| 55 | + |
| 56 | + byte[] encryptedMessage = encryptor.encryptMessage(message.getBytes(),certificate); |
| 57 | + |
| 58 | + assertThat(encryptedMessage).isNotNull(); |
| 59 | + assertThat(encryptedMessage.length % 128).isEqualTo(0); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void givenEncryptionKey_whenMessageIsEncrypted_thenDecryptMessage() throws Exception{ |
| 64 | + byte[] encryptedMessageBytes = encryptor.encryptMessage(message.getBytes(),encKeyString.getBytes()); |
| 65 | + |
| 66 | + byte[] clearMessageBytes = encryptor.decryptMessage(encryptedMessageBytes, encKeyString.getBytes()); |
| 67 | + |
| 68 | + assertThat(message).isEqualTo(new String(clearMessageBytes)); |
| 69 | + } |
| 70 | +} |
0 commit comments