Skip to content

Commit 52edd46

Browse files
IkoroVictorKevinGilmore
authored andcommitted
BAEL-1371 - Guide to Cipher class (eugenp#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
1 parent 0d22a64 commit 52edd46

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.cipher;
2+
3+
import javax.crypto.*;
4+
import javax.crypto.spec.SecretKeySpec;
5+
import java.security.InvalidKeyException;
6+
import java.security.NoSuchAlgorithmException;
7+
import java.security.cert.Certificate;
8+
9+
public class Encryptor {
10+
11+
public byte[] encryptMessage(byte[] message, byte[] keyBytes) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException {
12+
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
13+
SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
14+
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
15+
byte[] encryptedMessage = cipher.doFinal(message);
16+
return encryptedMessage;
17+
}
18+
19+
public byte[] encryptMessage(byte[] message, Certificate publicKeyCertificate) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException {
20+
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
21+
cipher.init(Cipher.ENCRYPT_MODE, publicKeyCertificate);
22+
byte[] encryptedMessage = cipher.doFinal(message);
23+
return encryptedMessage;
24+
}
25+
26+
public byte[] decryptMessage(byte[] encryptedMessage, byte[] keyBytes) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
27+
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
28+
SecretKey secretKey = new SecretKeySpec(keyBytes, "AES");
29+
cipher.init(Cipher.DECRYPT_MODE, secretKey);
30+
byte[] clearMessage = cipher.doFinal(encryptedMessage);
31+
return clearMessage;
32+
}
33+
34+
35+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)