KJBank 암호화 관련 수정
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -23,7 +23,8 @@ public class EAIServerVO implements Serializable {
|
||||
* 리모트 호출 url ==> rmi://{ip}:{rmi RegistryPort}/xxxxx
|
||||
* 사이트마다 다를수 있음.
|
||||
*/
|
||||
private int plusRmiPort = 1;
|
||||
//private int plusRmiPort = 1;
|
||||
private int plusRmiPort = 11; //jwhong for weblogic
|
||||
|
||||
/**
|
||||
* sessionManager에서 쓰기위해
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
package com.kjbank.encrypt.exchange.crypto;
|
||||
|
||||
//import org.apache.commons.lang.StringUtils;
|
||||
//import org.apache.log4j.Logger;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.Key;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Base64;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import com.eactive.eai.common.util.Logger;
|
||||
|
||||
public class AES256Cipher {
|
||||
|
||||
//private static final Logger logger = Logger.getLogger(AES256Cipher.class);
|
||||
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
|
||||
private static final String IV_STRING = "A-16-Byte-String";
|
||||
|
||||
public static String encrypt(String inPlainText, String inKey)
|
||||
throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException {
|
||||
|
||||
String resultData = "";
|
||||
|
||||
try {
|
||||
|
||||
if(StringUtils.isEmpty(inPlainText)){
|
||||
throw new IllegalArgumentException("plainText is Empty");
|
||||
}
|
||||
|
||||
if(StringUtils.isEmpty(inKey) || inKey.length() < 32){
|
||||
throw new IllegalArgumentException("invalid key");
|
||||
}
|
||||
|
||||
if (inKey.length() > 32) {
|
||||
inKey = inKey.substring(0, 32);
|
||||
}
|
||||
logger.debug("[AES256Cipher] Encrypt Key : " + inKey);
|
||||
|
||||
Key key = new SecretKeySpec(inKey.getBytes(StandardCharsets.UTF_8), "AES");
|
||||
IvParameterSpec ivParameterSpec = new IvParameterSpec(IV_STRING.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
|
||||
|
||||
byte[] encryptByte = cipher.doFinal(inPlainText.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
resultData = Base64.getEncoder().encodeToString(encryptByte);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("AES256Cipher Encrypt() : " + e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
return resultData;
|
||||
}
|
||||
|
||||
public static String decrypt(String inEncryptedText, String inKey)
|
||||
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
|
||||
{
|
||||
String resultData = "";
|
||||
|
||||
try {
|
||||
|
||||
if(StringUtils.isEmpty(inEncryptedText)){
|
||||
throw new IllegalArgumentException("encryptedText is Empty");
|
||||
}
|
||||
|
||||
if(StringUtils.isEmpty(inKey) || inKey.length() < 32){
|
||||
throw new IllegalArgumentException("invalid key");
|
||||
}
|
||||
|
||||
if (inKey.length() > 32) {
|
||||
inKey = inKey.substring(0, 32);
|
||||
}
|
||||
|
||||
logger.debug("[AES256Cipher] Decrypt Key : " + inKey);
|
||||
|
||||
Key key = new SecretKeySpec(inKey.getBytes(StandardCharsets.UTF_8), "AES");
|
||||
IvParameterSpec ivParameterSpec = new IvParameterSpec(IV_STRING.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
|
||||
|
||||
byte[] encryptByte = Base64.getDecoder().decode(inEncryptedText);
|
||||
|
||||
byte[] decryptByte = cipher.doFinal(encryptByte);
|
||||
|
||||
resultData = new String(decryptByte, StandardCharsets.UTF_8);
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("AES256Cipher Decrypt() : " + e);
|
||||
throw e;
|
||||
}
|
||||
|
||||
return resultData;
|
||||
}
|
||||
|
||||
public static String encrypt(String plainText, String iv, String key)
|
||||
throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException {
|
||||
|
||||
String resultData = "";
|
||||
|
||||
byte[] ivBytes;
|
||||
byte[] keyBytes;
|
||||
|
||||
if (StringUtils.isEmpty(plainText)) {
|
||||
throw new IllegalArgumentException("plainText is Empty");
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(iv)) {
|
||||
throw new IllegalArgumentException("invalid iv");
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
throw new IllegalArgumentException("invalid key");
|
||||
}
|
||||
|
||||
ivBytes = Base64.getDecoder().decode(iv);
|
||||
keyBytes = Base64.getDecoder().decode(key);
|
||||
|
||||
Key keySpec = new SecretKeySpec(keyBytes, "AES");
|
||||
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParameterSpec);
|
||||
|
||||
byte[] encryptByte = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
resultData = Base64.getEncoder().encodeToString(encryptByte);
|
||||
|
||||
logger.debug("[AES256] Plain Text : " + plainText);
|
||||
logger.debug("[AES256] Encrypt Text : " + resultData);
|
||||
|
||||
return resultData;
|
||||
}
|
||||
|
||||
public static String decrypt(String encryptText, String iv, String key)
|
||||
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
|
||||
|
||||
String resultData = "";
|
||||
|
||||
byte[] ivBytes;
|
||||
byte[] keyBytes;
|
||||
|
||||
if (StringUtils.isEmpty(encryptText)) {
|
||||
throw new IllegalArgumentException("encryptText is Empty");
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(iv)) {
|
||||
throw new IllegalArgumentException("invalid iv");
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(key)) {
|
||||
throw new IllegalArgumentException("invalid key");
|
||||
}
|
||||
|
||||
ivBytes = Base64.getDecoder().decode(iv);
|
||||
keyBytes = Base64.getDecoder().decode(key);
|
||||
|
||||
Key keySpec = new SecretKeySpec(keyBytes, "AES");
|
||||
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
|
||||
|
||||
byte[] encryptByte = (Base64.getDecoder().decode(encryptText));
|
||||
|
||||
byte[] decryptByte = cipher.doFinal(encryptByte);
|
||||
|
||||
resultData = new String(decryptByte, StandardCharsets.UTF_8);
|
||||
|
||||
logger.debug("[AES256] Encrypt Text : " + encryptText);
|
||||
logger.debug("[AES256] Decrypt Text : " + resultData);
|
||||
|
||||
return resultData;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.kjbank.encrypt.exchange.crypto;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.KeyGenerator;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.GCMParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Base64;
|
||||
|
||||
public class AES256GCMCipher {
|
||||
private static final int AES_KEY_SIZE = 256;
|
||||
private static final int GCM_TAG_LENGTH = 128;
|
||||
private static final int GCM_IV_LENGTH = 12;
|
||||
|
||||
private final SecretKey key;
|
||||
|
||||
public AES256GCMCipher(byte[] keyBytes) {
|
||||
if (keyBytes.length != AES_KEY_SIZE / 8) {
|
||||
throw new IllegalArgumentException("Key must be 256 bits (32 bytes)");
|
||||
}
|
||||
this.key = new SecretKeySpec(keyBytes, "AES");
|
||||
}
|
||||
|
||||
public static byte[] generateKey() throws Exception {
|
||||
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
|
||||
keyGen.init(AES_KEY_SIZE);
|
||||
return keyGen.generateKey().getEncoded();
|
||||
}
|
||||
|
||||
public String encrypt(String plaintext, byte[] aad) throws Exception {
|
||||
byte[] iv = new byte[GCM_IV_LENGTH];
|
||||
new SecureRandom().nextBytes(iv);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH, iv);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
|
||||
if (aad != null) {
|
||||
cipher.updateAAD(aad);
|
||||
}
|
||||
|
||||
byte[] ciphertext = cipher.doFinal(plaintext.getBytes("UTF-8"));
|
||||
|
||||
byte[] encrypted = new byte[iv.length + ciphertext.length];
|
||||
System.arraycopy(iv, 0, encrypted, 0, iv.length);
|
||||
System.arraycopy(ciphertext, 0, encrypted, iv.length, ciphertext.length);
|
||||
|
||||
return Base64.getEncoder().encodeToString(encrypted);
|
||||
}
|
||||
|
||||
public String decrypt(String encryptedBase64, byte[] aad) throws Exception {
|
||||
byte[] encrypted = Base64.getDecoder().decode(encryptedBase64);
|
||||
|
||||
byte[] iv = new byte[GCM_IV_LENGTH];
|
||||
byte[] ciphertext = new byte[encrypted.length - GCM_IV_LENGTH];
|
||||
System.arraycopy(encrypted, 0, iv, 0, GCM_IV_LENGTH);
|
||||
System.arraycopy(encrypted, GCM_IV_LENGTH, ciphertext, 0, ciphertext.length);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH, iv);
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, spec);
|
||||
if (aad != null) {
|
||||
cipher.updateAAD(aad);
|
||||
}
|
||||
|
||||
byte[] plaintext = cipher.doFinal(ciphertext);
|
||||
return new String(plaintext, "UTF-8");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package com.kjbank.encrypt.exchange.crypto;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class AESCipher
|
||||
{
|
||||
private static final String IV_STRING = "A-16-Byte-String";
|
||||
private static final String charset = "UTF-8";
|
||||
|
||||
private static String aesEncryptString(String content, String key)
|
||||
throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
|
||||
InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
|
||||
{
|
||||
byte[] contentBytes = content.getBytes(charset);
|
||||
byte[] keyBytes = key.getBytes(charset);
|
||||
byte[] encryptedBytes = aesEncryptBytes(contentBytes, keyBytes);
|
||||
|
||||
return byteArrayToHexString(encryptedBytes);
|
||||
}
|
||||
|
||||
private static String aesDecryptString(String content, String key)
|
||||
throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException,
|
||||
IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
|
||||
{
|
||||
byte[] encryptedBytes = hexStringToByteArray(content);
|
||||
byte[] keyBytes = key.getBytes(charset);
|
||||
byte[] decryptedBytes = aesDecryptBytes(encryptedBytes, keyBytes);
|
||||
return new String(decryptedBytes, charset);
|
||||
}
|
||||
|
||||
private static byte[] aesEncryptBytes(byte[] contentBytes, byte[] keyBytes)
|
||||
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
|
||||
IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
|
||||
{
|
||||
return cipherOperation(contentBytes, keyBytes, Cipher.ENCRYPT_MODE);
|
||||
}
|
||||
|
||||
private static byte[] aesDecryptBytes(byte[] contentBytes, byte[] keyBytes)
|
||||
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
|
||||
IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException
|
||||
{
|
||||
return cipherOperation(contentBytes, keyBytes, Cipher.DECRYPT_MODE);
|
||||
}
|
||||
|
||||
private static byte[] cipherOperation(byte[] contentBytes, byte[] keyBytes, int mode)
|
||||
throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
|
||||
InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
|
||||
{
|
||||
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");
|
||||
|
||||
byte[] initParam = IV_STRING.getBytes(charset);
|
||||
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
cipher.init(mode, secretKey, ivParameterSpec);
|
||||
|
||||
return cipher.doFinal(contentBytes);
|
||||
}
|
||||
|
||||
public static String encrypt(String inText, String inKey)
|
||||
{
|
||||
if (inKey.length() > 16)
|
||||
{
|
||||
inKey = inKey.substring(0, 16);
|
||||
}
|
||||
|
||||
String resultData = "";
|
||||
String inNewKey = String.format("%16s", inKey);
|
||||
|
||||
try
|
||||
{
|
||||
resultData = AESCipher.aesEncryptString(inText, inNewKey);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
|
||||
return resultData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 복호화
|
||||
*
|
||||
* @param inEncryptedText
|
||||
* @param inKey
|
||||
* @return
|
||||
*/
|
||||
public static String decrypt(String inEncryptedText, String inKey)
|
||||
{
|
||||
if (inKey.length() > 16)
|
||||
{
|
||||
inKey = inKey.substring(0, 16);
|
||||
}
|
||||
|
||||
String inNewKey = String.format("%16s", inKey);
|
||||
|
||||
String resultData = "";
|
||||
try
|
||||
{
|
||||
resultData = AESCipher.aesDecryptString(inEncryptedText, inNewKey);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println(e.toString());
|
||||
}
|
||||
|
||||
return resultData;
|
||||
}
|
||||
|
||||
public static String decryptExistException(String inEncryptedText, String inKey) throws Exception
|
||||
{
|
||||
if (inKey.length() > 16)
|
||||
{
|
||||
inKey = inKey.substring(0, 16);
|
||||
}
|
||||
|
||||
String inNewKey = String.format("%16s", inKey);
|
||||
|
||||
String resultData = "";
|
||||
|
||||
resultData = AESCipher.aesDecryptString(inEncryptedText, inNewKey);
|
||||
|
||||
|
||||
return resultData;
|
||||
}
|
||||
|
||||
/**
|
||||
* byte 배열을 HEX 문자열로 변환
|
||||
*
|
||||
* @param bytes
|
||||
* @return
|
||||
*/
|
||||
public static String byteArrayToHexString(byte[] bytes)
|
||||
{
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
for (int i = 0; i < bytes.length; i++)
|
||||
{
|
||||
if (((int) bytes[i] & 0xff) < 0x10)
|
||||
buffer.append("0");
|
||||
buffer.append(Long.toString((int) bytes[i] & 0xff, 16));
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* HEX 문자열을 byte 배열로 변환
|
||||
* (ex: "0D0A" => {0x0D, 0x0A,})
|
||||
*
|
||||
* @param inHexString
|
||||
* @return
|
||||
*/
|
||||
public static final byte[] hexStringToByteArray(String inHexString)
|
||||
{
|
||||
int i = 0;
|
||||
byte[] results = new byte[inHexString.length() / 2];
|
||||
for (int k = 0; k < inHexString.length(); )
|
||||
{
|
||||
results[i] = (byte) (Character.digit(inHexString.charAt(k++), 16) << 4);
|
||||
results[i] += (byte) (Character.digit(inHexString.charAt(k++), 16));
|
||||
i++;
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user