sitelink1  
sitelink2  
sitelink3  
sitelink4  
extra_vars5  
extra_vars6  
1. DES : 대칭 암호화 알고리즘, 암호화/복호화 키가 같음

2. RSA : 공개키 암호화방식 , 공개키(암호화키), 복소화키는 개인이 소장(비밀키)

3. 공개키 암호화 방식을 이용한 인증과 보안

1) A와 B는 각자 공개키/비밀키 쌍을 생성한다
2) A와 B는 각자의 공개키를 서로 주고받는다.
3) A는 B에게 전달할 문서를 B에게서 받은 공개키로 암호화한다.
4) B는 A에게서 암호화된 문서를 받고 저장해 두었던 비밀키로 복호화 한다.
5) B는 공개키를 A에게만 주었으므로, 복호화에 성공한다면 A가 문서를 보낸것이 확인된다.(인증)
6) B는 문서를 받앗으므로 A에게서 받은 공개키로 받았다는 답신을 암호화 해서 A에게 전송한다
7) A는 리턴받은 응답을 저장해 두엇던 B의 비밀키로 복호화한다.
8) 복호화가 되므로 B가 문서를 제대로 받았다는 것이 확인되었다. (부인방지)
4. 자바소스 예제 
·미리보기 | 소스복사·
  1. import java.io.*;   
  2. import java.math.*;   
  3. import java.util.*;   
  4. import java.security.*;   
  5. import javax.crypto.*;   
  6. import au.net.aba.crypto.provider.*;   
  7. public class RSACrypt   
  8. {   
  9.    public  static final int KEY_LENGTH         = 512;   
  10.    private static final int MSG_BLOCK_LENGTH    = 53;   
  11.    private static final int ENCRYPTED_MSG_LENGTH = 64;   
  12.    // byte 배열로 전달받은 키를 문자열로 만든다.   
  13.    private static String makeStrKey(byte bytes[], int arraySize){   
  14.       StringBuffer bf = new StringBuffer();   
  15.       if (arraySize > 0){   
  16.          int noOfZero = arraySize - bytes.length;   
  17.          for (int i = 0; i < noOfZero; i++)   
  18.             bf.append("00");   
  19.       }   
  20.       for(int i=0; i<bytes.length; i++)   
  21.       {   
  22.          if((bytes[i] > 15) || (bytes[i] < 0))   
  23.             bf.append(java.lang.Integer.toHexString(bytes[i] & 0xff));   
  24.          else  
  25.             bf.append("0" + java.lang.Integer.toHexString(bytes[i] & 0xff));   
  26.       }   
  27.       return bf.toString();   
  28.    }   
  29.    private static byte[] int2byte(int[] src){   
  30.       byte[] dst = new byte[src.length];   
  31.       for (int i = 0; i < src.length; i++)   
  32.          dst[i] = (byte)src[i];   
  33.       return dst;   
  34.    }   
  35.    private static int [] str2int(String src, int arraySize)   
  36.    {   
  37.       int[] dst;   
  38.       if (arraySize > 0){   
  39.          dst = new int[arraySize];   
  40.          int noOfZero = arraySize - (src.length() / 2);   
  41.          for (int i = 0; i < noOfZero; i++) dst[i] = 0x00;   
  42.          for (int i = noOfZero; i < dst.length; i++){   
  43.             char first = src.charAt(i * 2);   
  44.             char second = src.charAt(i * 2 + 1);   
  45.             dst[i] = hex2int(first) * 16 + hex2int(second);   
  46.          }   
  47.       }   
  48.       else{   
  49.          dst = new int[src.length() / 2];   
  50.          for (int i = 0; i < dst.length; i++){   
  51.             char first = src.charAt(i * 2);   
  52.             char second = src.charAt(i * 2 + 1);   
  53.             dst[i] = hex2int(first) * 16 + hex2int(second);   
  54.          }   
  55.       }   
  56.       return dst;   
  57.    }   
  58.    private static int hex2int(char src)   
  59.    {   
  60.       if(src >= '0' && src <= '9'return ((int)src - (int)'0');   
  61.       else if(src >= 'a' && src <= 'f'return ((int)src - (int)'a' + 10);   
  62.       else if(src >= 'A' && src <= 'F'return ((int)src - (int)'A' + 10);   
  63.       else throw new NumberFormatException();   
  64.    }   
  65.    // Random 한 키쌍을 생성한다   
  66.    // 키생성에는 시간이 오래걸리므로, Key Pool을 향후 Key Pool을 만들어야 한다.   
  67.    public static KeyPair getRandomKeyPair(int keyLength) throws Exception{   
  68.       SecureRandom rand = new SecureRandom();   
  69.       rand.nextInt();   
  70.       KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");   
  71.       kpg.initialize(keyLength, rand);   
  72.       KeyPair kp = kpg.generateKeyPair();   
  73.       return kp;   
  74.    }   
  75.    // 공개키는 원격지에 전송해 주어야 하므로 스트링 형태로 만드는 것이 필요하다.    
  76.    public static String getPubKeyStr(PublicKey pubKey)   
  77.    {   
  78.       StringBuffer bf = new StringBuffer();   
  79.       byte[] tempPubKeyArray = pubKey.getEncoded();   
  80.       RSAPubKey rsaPubKey = new RSAPubKey(tempPubKeyArray);    
  81.       bf.append(makeStrKey(rsaPubKey.getModulus().toByteArray(), 128));   
  82.       bf.append(" ");   
  83.       bf.append(makeStrKey(rsaPubKey.getPublicExponent().toByteArray(), 128));   
  84.       return bf.toString();   
  85.    }   
  86.    // 상대방 에게서 전달받은 스트링 형태의 공개키를 공개키 객체로 만들어 주는 함수   
  87.    public static PublicKey getPubKey(String hexPubKey)   
  88.    {   
  89.       StringTokenizer tk = new StringTokenizer(hexPubKey, " ");   
  90.       System.out.println(tk.countTokens());   
  91.       String strPubModulus = tk.nextToken();   
  92.       String strPubExponent = tk.nextToken();   
  93.       int[] intArrayPubModulus = str2int(strPubModulus, 128);   
  94.       int[] intArrayPubExponent = str2int(strPubExponent, 128);   
  95.       byte[] byteArrayPubModulus = int2byte(intArrayPubModulus);   
  96.       byte[] byteArrayPubExponent = int2byte(intArrayPubExponent);   
  97.       BigInteger bigIntPubModulus = new BigInteger(byteArrayPubModulus);   
  98.       BigInteger bigIntPubExponent = new BigInteger(byteArrayPubExponent);   
  99.       return new RSAPubKey(bigIntPubModulus, bigIntPubExponent);   
  100.    }   
  101.    // 문자열과 공개키를 전달받아 암호화를 수행한다.    
  102.    public static String encode(String msg, PublicKey rsaPubKey) throws Exception   
  103.    {   
  104.       int MSG_BLOCK_LENGTH = 53;   
  105.       byte[] msgArray = msg.getBytes();   
  106.       Cipher cipher = Cipher.getInstance("RSA");   
  107.       cipher.init(Cipher.ENCRYPT_MODE, rsaPubKey);   
  108.       Vector arrayList = new Vector();   
  109.       int totalSize = 0;   
  110.       for (int i = 0; i < msgArray.length; i += MSG_BLOCK_LENGTH){   
  111.          int size = Math.min(MSG_BLOCK_LENGTH, msgArray.length - i);   
  112.          byte[] temp = new byte[size];   
  113.          System.arraycopy(msgArray, i, temp, 0, size);   
  114.          byte[] encedMsg = cipher.doFinal(temp);   
  115.          arrayList.addElement(encedMsg);   
  116.          totalSize += encedMsg.length;   
  117.       }   
  118.       byte[] totalArray = new byte[totalSize];   
  119.       int offset = 0;   
  120.       for (int i = 0; i < arrayList.size(); i++){   
  121.          byte[] tempArray = (byte[])arrayList.elementAt(i);   
  122.          System.arraycopy(tempArray, 0, totalArray, offset, tempArray.length);   
  123.          offset += tempArray.length;   
  124.       }   
  125.       return makeStrKey(totalArray, -1);   
  126.    }   
  127.    // 저장된 비밀키를 사용해서 공개키로 암호화된 문자열을 복호화 한다.    
  128.    public static String decode(String msg, PrivateKey rsaPrvKey) throws Exception   
  129.    {   
  130.       int[] intArray = str2int(msg, -1);   
  131.       byte[] msgArray = int2byte(intArray);   
  132.       Cipher cipher = Cipher.getInstance("RSA");   
  133.       cipher.init(Cipher.DECRYPT_MODE, rsaPrvKey);   
  134.       Vector arrayList = new Vector();   
  135.       int totalSize = 0;   
  136.       for (int i = 0; i < msgArray.length; i += ENCRYPTED_MSG_LENGTH)   
  137.       {   
  138.          byte[] temp = new byte[ENCRYPTED_MSG_LENGTH];   
  139.          System.arraycopy(msgArray, i, temp, 0, ENCRYPTED_MSG_LENGTH);   
  140.          byte[] decedMsg = cipher.doFinal(temp);   
  141.          arrayList.addElement(decedMsg);   
  142.          totalSize += decedMsg.length;   
  143.       }   
  144.       byte[] totalArray = new byte[totalSize];   
  145.       int offset = 0;   
  146.       for (int i = 0; i < arrayList.size(); i++)   
  147.       {   
  148.          byte[] tempArray = (byte[])arrayList.elementAt(i);   
  149.          System.arraycopy(tempArray, 0, totalArray, offset, tempArray.length);   
  150.          offset += tempArray.length;   
  151.       }   
  152.       return new String(totalArray);   
  153.    }   
  154.    // 사용예제   
  155.    public static void main(String [] args)   
  156.    {   
  157.       try{   
  158.          // Privider Setting    
  159.          Security.insertProviderAt(new ABAProvider(), 2);         
  160.          // 키쌍을 생성    
  161.          KeyPair keyPair = RSACrypt.getRandomKeyPair(512);   
  162.             
  163.          // 공개키와 비밀키를 얻어옴    
  164.          PrivateKey privateKey = (PrivateKey) keyPair.getPrivate();   
  165.          PublicKey publicKey = (PublicKey ) keyPair.getPublic();   
  166.             
  167.          // 공개키 출력    
  168.          System.out.println("> 생성된 공개키 String : "+RSACrypt.getPubKeyStr(publicKey));   
  169.             
  170.          String encResult = RSACrypt.encode("암호화 할 문자열", publicKey);   
  171.          System.out.println("> 공개키로 암호화된 문자열 : "+encResult);   
  172.          String decResult = RSACrypt.decode(encResult, privateKey);   
  173.          System.out.println("> 비밀키로 복호화된 문자열 : "+decResult);   
  174.       }   
  175.       catch(Exception e)   
  176.       {   
  177.          e.printStackTrace();   
  178.       }   
  179.    }   
  180. }  
번호 제목 글쓴이 날짜 조회 수
공지 [계속 추가중] SBOM 용어 정의 황제낙엽 2025.04.10 8410
공지 [계속 추가중] Keycloak 용어 및 설정 옵션 정의 황제낙엽 2024.02.02 8704
61 무료 Authorization Server 솔루션 황제낙엽 2024.01.18 2144
60 Keycloak 에 대하여 황제낙엽 2024.01.18 1951
59 OAuth2 구글(Google), Github, 카카오(Kakao), 네이버(Naver) 로그인 API 목록 황제낙엽 2023.12.17 2053
58 Spring Authorization Server 관련 포스트 목록과 인프런 강의 황제낙엽 2023.12.07 2154
57 [POST/2023.05.22] OAuth 2.1 Authorization Server (Spring Security) 구축 후기 file 황제낙엽 2023.12.03 2592
56 OAuth 와 JWT 내용 정리 (개념 정의 및 적용 전략) file 황제낙엽 2023.12.03 2313
55 [Copilot, Bard] oauth claim 의 의미 황제낙엽 2023.12.02 1962
54 [reference links] windows + let's encrypt 황제낙엽 2023.03.23 66886
53 WIN-ACME 황제낙엽 2022.09.17 2035
52 Apache Log4j 2 보안 업데이트 권고 황제낙엽 2021.12.13 2281
51 [Let's Encrypt] certbot 을 이용한 인증서 갱신 실패 기록 황제낙엽 2021.07.03 2029
50 DNS TXT Record 적용 후 조회 명령어 (windows 와 web url) 황제낙엽 2021.07.01 1982
49 SSL 인증서 파일 포맷 종류 - crt, cer, csr, pem, der, pfx, p12, jks, key 황제낙엽 2020.07.20 2231
48 certbot docker 를 이용한 인증서 발급 및 갱신(Let's Encrypt-DNS를 통해 도메인 인증) file 황제낙엽 2020.07.14 2704
47 Certbot이란 황제낙엽 2020.07.14 2049
46 Let's Encrypt + Ubuntu 19.10 + Apache 2.4.41 secret 황제낙엽 2020.06.29 1
45 무료 SSL/TLS 인증서 Let's Encrypt(Linux+Apache) secret 황제낙엽 2020.04.05 17
44 SNI 기반 HTTPS 사이트 차단 file 황제낙엽 2020.03.04 2176
43 사설인증서 공인인증서 구분 방법 file 황제낙엽 2019.07.16 2129
42 Convert Certificate Format SSL 인증서 변환 가이드 황제낙엽 2019.03.29 2110