From 52edd46c0a74d1da9fc81d3f91b0c6507c03b324 Mon Sep 17 00:00:00 2001 From: Victor Ikoro Date: Fri, 22 Dec 2017 04:39:55 +0100 Subject: [PATCH] BAEL-1371 - Guide to Cipher class (#3150) * Bean Injection Types * Bean Injection Types * Bean Injection Types * Bean Injection Types - Added / Fixed tests * BAEL-1371 Encryptor class * BAEL-1371 Modified Encryptor class, added encryptor test * BAEL-1371 Modified Encryptor class, added encryptor test * BAEL-1371 removed spring core files * BAEL-1371 Update test, new line for "when" and "then" logic * BAEL-1371 Fixed tests * BAEL-1371 cleanup and assertj assertions --- .../java/com/baeldung/cipher/Encryptor.java | 35 ++++++++++ .../baeldung/cipher/EncryptorUnitTest.java | 70 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/cipher/Encryptor.java create mode 100644 core-java/src/test/java/com/baeldung/cipher/EncryptorUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/cipher/Encryptor.java b/core-java/src/main/java/com/baeldung/cipher/Encryptor.java new file mode 100644 index 0000000000..901975a5d6 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/cipher/Encryptor.java @@ -0,0 +1,35 @@ +package com.baeldung.cipher; + +import javax.crypto.*; +import javax.crypto.spec.SecretKeySpec; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.Certificate; + +public class Encryptor { + + public byte[] encryptMessage(byte[] message, byte[] keyBytes) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException { + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + SecretKey secretKey = new SecretKeySpec(keyBytes, "AES"); + cipher.init(Cipher.ENCRYPT_MODE, secretKey); + byte[] encryptedMessage = cipher.doFinal(message); + return encryptedMessage; + } + + public byte[] encryptMessage(byte[] message, Certificate publicKeyCertificate) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException { + Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); + cipher.init(Cipher.ENCRYPT_MODE, publicKeyCertificate); + byte[] encryptedMessage = cipher.doFinal(message); + return encryptedMessage; + } + + public byte[] decryptMessage(byte[] encryptedMessage, byte[] keyBytes) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + SecretKey secretKey = new SecretKeySpec(keyBytes, "AES"); + cipher.init(Cipher.DECRYPT_MODE, secretKey); + byte[] clearMessage = cipher.doFinal(encryptedMessage); + return clearMessage; + } + + +} diff --git a/core-java/src/test/java/com/baeldung/cipher/EncryptorUnitTest.java b/core-java/src/test/java/com/baeldung/cipher/EncryptorUnitTest.java new file mode 100644 index 0000000000..ba8d2ed96f --- /dev/null +++ b/core-java/src/test/java/com/baeldung/cipher/EncryptorUnitTest.java @@ -0,0 +1,70 @@ +package com.baeldung.cipher; + + +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; + +import static org.assertj.core.api.Assertions.assertThat; + +public class EncryptorUnitTest { + private String encKeyString; + private String message; + private String certificateString; + private Encryptor encryptor; + + @Before + public void init(){ + encKeyString = "1234567890123456"; + message = "This is a secret message"; + encryptor = new Encryptor(); + certificateString = "-----BEGIN CERTIFICATE-----\n" + + "MIICVjCCAb8CAg37MA0GCSqGSIb3DQEBBQUAMIGbMQswCQYDVQQGEwJKUDEOMAwG\n" + + "A1UECBMFVG9reW8xEDAOBgNVBAcTB0NodW8ta3UxETAPBgNVBAoTCEZyYW5rNERE\n" + + "MRgwFgYDVQQLEw9XZWJDZXJ0IFN1cHBvcnQxGDAWBgNVBAMTD0ZyYW5rNEREIFdl\n" + + "YiBDQTEjMCEGCSqGSIb3DQEJARYUc3VwcG9ydEBmcmFuazRkZC5jb20wHhcNMTIw\n" + + "ODIyMDUyNzIzWhcNMTcwODIxMDUyNzIzWjBKMQswCQYDVQQGEwJKUDEOMAwGA1UE\n" + + "CAwFVG9reW8xETAPBgNVBAoMCEZyYW5rNEREMRgwFgYDVQQDDA93d3cuZXhhbXBs\n" + + "ZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMYBBrx5PlP0WNI/ZdzD\n" + + "+6Pktmurn+F2kQYbtc7XQh8/LTBvCo+P6iZoLEmUA9e7EXLRxgU1CVqeAi7QcAn9\n" + + "MwBlc8ksFJHB0rtf9pmf8Oza9E0Bynlq/4/Kb1x+d+AyhL7oK9tQwB24uHOueHi1\n" + + "C/iVv8CSWKiYe6hzN1txYe8rAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAASPdjigJ\n" + + "kXCqKWpnZ/Oc75EUcMi6HztaW8abUMlYXPIgkV2F7YanHOB7K4f7OOLjiz8DTPFf\n" + + "jC9UeuErhaA/zzWi8ewMTFZW/WshOrm3fNvcMrMLKtH534JKvcdMg6qIdjTFINIr\n" + + "evnAhf0cwULaebn+lMs8Pdl7y37+sfluVok=\n" + + "-----END CERTIFICATE-----"; + } + + @Test + public void givenEncryptionKey_whenMessageIsPassedToEncryptor_thenMessageIsEncrypted() throws Exception { + byte[] encryptedMessage = encryptor.encryptMessage(message.getBytes(),encKeyString.getBytes()); + + assertThat(encryptedMessage).isNotNull(); + assertThat(encryptedMessage.length % 32).isEqualTo(0); + } + + @Test + public void givenCertificateWithPublicKey_whenMessageIsPassedToEncryptor_thenMessageIsEncrypted() throws Exception { + CertificateFactory factory = CertificateFactory.getInstance("X.509"); + InputStream is = new ByteArrayInputStream(certificateString.getBytes()); + X509Certificate certificate = (X509Certificate) factory.generateCertificate(is); + + byte[] encryptedMessage = encryptor.encryptMessage(message.getBytes(),certificate); + + assertThat(encryptedMessage).isNotNull(); + assertThat(encryptedMessage.length % 128).isEqualTo(0); + } + + @Test + public void givenEncryptionKey_whenMessageIsEncrypted_thenDecryptMessage() throws Exception{ + byte[] encryptedMessageBytes = encryptor.encryptMessage(message.getBytes(),encKeyString.getBytes()); + + byte[] clearMessageBytes = encryptor.decryptMessage(encryptedMessageBytes, encKeyString.getBytes()); + + assertThat(message).isEqualTo(new String(clearMessageBytes)); + } +}