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
This commit is contained in:
Victor Ikoro 2017-12-22 04:39:55 +01:00 committed by KevinGilmore
parent 0d22a64cbf
commit 52edd46c0a
2 changed files with 105 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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));
}
}