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:
committed by
KevinGilmore
parent
0d22a64cbf
commit
52edd46c0a
35
core-java/src/main/java/com/baeldung/cipher/Encryptor.java
Normal file
35
core-java/src/main/java/com/baeldung/cipher/Encryptor.java
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user