/**
* 암호화 처리
* @param publicKey
* @param plainText
* @return
* @throws InvalidKeyException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws UnsupportedEncodingException
*/
public static String encryptRSA(PublicKey publicKey, String plainText)
throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] ecryptedBytes = cipher.doFinal(plainText.getBytes("utf-8"));
String ecryptedString = Base64.getEncoder().encodeToString(ecryptedBytes);
logger.info("##### ecryptedString {}", ecryptedString);
return ecryptedString;
}
.
2. 복호화 처리
/**
* 복호화 처리
* @param privateKey
* @param securedText
* @return
* @throws Exception
*/
public static String decryptRSA(PrivateKey privateKey, String securedText) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] securedBytes = Base64.getDecoder().decode(securedText.getBytes());
String securedString = new String(securedBytes, "utf-8");
byte[] decryptedBytes = cipher.doFinal(securedBytes);
String decryptedString = new String(decryptedBytes, "utf-8");
logger.debug("##### decryptedString {}", decryptedString);
return decryptedString;
}
.
3. 테스트 케이스 작성
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {RSAKeyGenerator.class})
public class RSAKeyGenerator {
@Test
public void testRSAKeyGenerator() throws Exception {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(512); // key size 설정
KeyPair keyPair = generator.generateKeyPair();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
RSAPublicKeySpec publicKeySpec = (RSAPublicKeySpec) keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);
// publicKey / UI에서 사용가능 - 4번 참고
String publicKeyModulus = publicKeySpec.getModulus().toString(16);
String publicKeyExponent = publicKeySpec.getPublicExponent().toString(16);
String e_username = SecurityUtils.encryptRSA(publicKey, "admin1");
String e_password = SecurityUtils.encryptRSA(publicKey, "1");
String de_username = SecurityUtils.decryptRSA(privateKey, e_username);
String de_password = SecurityUtils.decryptRSA(privateKey, e_password);
}
}
<!--순서에 주의-->
<script src="/assets/js/rsa/rsa.js"></script>
<script src="/assets/js/rsa/jsbn.js"></script>
<script src="/assets/js/rsa/prng4.js"></script>
<script src="/assets/js/rsa/rng.js"></script>
...
const rsaPublicKeyModulus = document.getElementById("publicKeyModulus").value;
const rsaPublicKeyExponent = document.getElementById("publicKeyExponent").value;
const rsa = new RSAKey();
rsa.setPublic(rsaPublicKeyModulus, rsaPublicKeyExponent);
// 사용자ID와 비밀번호를 RSA로 암호화한다.
var securedUsername = rsa.encrypt(username);
var securedPassword = rsa.encrypt(password);
끝~~