ÀÚ¹Ù RSA ¾Ï/º¹È£È »ç¿ë¹ý
ÀÚ¹Ù ¾ð¾î¿¡¼ ºñ´ëĪŰ ¹æ½ÄÀÇ RSA ¾Ï/º¹È£È ¹æ¹ýÀ» ¾Ë¾Æº¾´Ï´Ù. Å°»ý¼º°ú ¾ÏÈ£È º¹È£È¸¦ ¸ðµÎ ÀÚ¹Ù ¾ð¾î·Î ¼öÇàÇÕ´Ï´Ù.
CipherUtil.java ÆÄÀÏÀ» 1024bit Å°¸¦ »ý¼ºÇÏ°í, ¾ÏÈ£È, º¹È£È¸¦ Áö¿øÇÏ´Â À¯Æ¿¸®Æ¼ Ŭ·¡½º·Î ÀÛ¼ºµÇ¾ú½À´Ï´Ù. Å°´Â SecureRandom Ŭ·¡½º¸¦ »ç¿ëÇؼ ÀÓÀÇÀÇ Å°¸¦ »ý¼ºÇÕ´Ï´Ù.
package com.tistory.offbyone.rsa;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class CipherUtil {
/**
* 1024ºñÆ® RSA Å°½ÖÀ» »ý¼ºÇÕ´Ï´Ù.
*/
public static KeyPair genRSAKeyPair() throws NoSuchAlgorithmException {
SecureRandom secureRandom = new SecureRandom();
KeyPairGenerator gen;
gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(1024, secureRandom);
KeyPair keyPair = gen.genKeyPair();
return keyPair;
}
/**
* Public Key·Î RSA ¾Ïȣȸ¦ ¼öÇàÇÕ´Ï´Ù.
* @param plainText ¾ÏÈ£ÈÇÒ Æò¹®ÀÔ´Ï´Ù.
* @param publicKey °ø°³Å° ÀÔ´Ï´Ù.
* @return
*/
public static String encryptRSA(String plainText, PublicKey publicKey)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bytePlain = cipher.doFinal(plainText.getBytes());
String encrypted = Base64.getEncoder().encodeToString(bytePlain);
return encrypted;
}
/**
* Private Key·Î RAS º¹È£È¸¦ ¼öÇàÇÕ´Ï´Ù.
*
* @param encrypted ¾ÏÈ£ÈµÈ ÀÌÁøµ¥ÀÌÅ͸¦ base64 ÀÎÄÚµùÇÑ ¹®ÀÚ¿ ÀÔ´Ï´Ù.
* @param privateKey º¹È£È¸¦ À§ÇÑ °³ÀÎÅ° ÀÔ´Ï´Ù.
* @return
* @throws Exception
*/
public static String decryptRSA(String encrypted, PrivateKey privateKey)
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
Cipher cipher = Cipher.getInstance("RSA");
byte[] byteEncrypted = Base64.getDecoder().decode(encrypted.getBytes());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytePlain = cipher.doFinal(byteEncrypted);
String decrypted = new String(bytePlain, "utf-8");
return decrypted;
}
}
À§¿¡¼ ÀÛ¼ºÇÑ À¯Æ¿¸®Æ¼ Ŭ·¡½º¸¦ »ç¿ëÇؼ Æò¹®À» °³ÀÎÅ°·Î ¾ÏÈ£È ÇÏ°í, °ø°³Å°·Î º¹È£ÈÇÏ´Â ¿¹Á¦ÀÔ´Ï´Ù.
package com.tistory.offbyone.rsa;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
public class RSATest {
public static void main(String[] args) throws Exception {
// RSA Å°½ÖÀ» »ý¼ºÇÕ´Ï´Ù.
KeyPair keyPair = CipherUtil.genRSAKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
String plainText = "¾ÏÈ£È ÇÒ ¹®ÀÚ¿ ÀÔ´Ï´Ù.";
// Base64 ÀÎÄÚµùµÈ ¾ÏÈ£È ¹®ÀÚ¿ ÀÔ´Ï´Ù.
String encrypted = CipherUtil.encryptRSA(plainText, publicKey);
System.out.println("encrypted : " + encrypted);
// º¹È£È ÇÕ´Ï´Ù.
String decrypted = CipherUtil.decryptRSA(encrypted, privateKey);
System.out.println("decrypted : " + decrypted);
// °ø°³Å°¸¦ Base64 ÀÎÄÚµùÇÑ ¹®ÀÚÀÏÀ» ¸¸µì´Ï´Ù.
byte[] bytePublicKey = publicKey.getEncoded();
String base64PublicKey = Base64.getEncoder().encodeToString(bytePublicKey);
System.out.println("Base64 Public Key : " + base64PublicKey);
// °³ÀÎÅ°¸¦ Base64 ÀÎÄÚµùÇÑ ¹®ÀÚ¿À» ¸¸µì´Ï´Ù.
byte[] bytePrivateKey = privateKey.getEncoded();
String base64PrivateKey = Base64.getEncoder().encodeToString(bytePrivateKey);
System.out.println("Base64 Private Key : " + base64PrivateKey);
}
}
½ÇÇàÇÑ °á°ú ÀÔ´Ï´Ù.
°³ÀÎÅ°¿Í °ø°³Å°¸¦ Base64·Î ÀÎÄÚµùµÈ ¹®ÀÚ¿·Î ¸¸µå´Â ¹æ¹ýµµ Ãß°¡ µÇ¾îÀÖ½À´Ï´Ù.
Ãâó: https://offbyone.tistory.com/346 [½¬°í ½ÍÀº °³¹ßÀÚ]