공개키 암호화 [RSA구현]

Posted by 백창
2015. 1. 16. 15:33 컴퓨터보안/Programming
반응형


 개념


 공개키 알고리즘은 암호화하는 키와 해독하는 키가 동일하지 않도록(혹은 하나로 다른 하나를 유추할 수 없도록) 고안된 방식이다. 공개키 알고리즘을 이용하면, 암호문을 작성할 때 사용하는 암호키가 외부에 공개되어도 비밀키를 모르면 암호문을 해독할 수 없다.


 공개키 알고리즘에서는 사용자가 두 개의 암호키(공개키와 비밀키)를 사용하는데, 공개키는 원문서를 암호화하는 데 사용하므로 원칙적으로 누구에게나 공개된다. 따라서 송신 호스트는 공개키로 원문서를 암호화하여 전송한다. 수신 호스트에서는 암호문을 해독하기 위해 비밀키를 사용한다.


 공개키 알고리즘의 대표 예는 RSA 알고리즘이 있다.



그림출처 : 네이버 지식백과



 RSA 구현


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
 
import javax.crypto.Cipher;
 
public class RSA {
    //RSA
    private PublicKey publicKey;
    private PrivateKey privateKey;
    
    /**
     * 키 쌍 생성
     * */
    public KeyPair keyGen(){
        KeyPair keyPair = null;
        
        try {
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
            keyPair = keyPairGen.generateKeyPair();
            
            publicKey = keyPair.getPublic();
            privateKey= keyPair.getPrivate();
            
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        
        return keyPair;
    }
    
    public PublicKey getPublic(){
        return publicKey;
    }
    public PrivateKey getPrivate(){
        return privateKey;
    }
    
    /**
     * 암호화
     * */
    public byte[] encryptRSA(byte[] input, PublicKey publicKey)throws Exception{
        byte[] encrypted = null;
        
        try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            encrypted = cipher.doFinal(input);
            
        } catch(Exception e){
            e.printStackTrace();
        }
        
        return encrypted;
    }
    /**
     * 암호화
     * */
    public byte[] encryptRSA(byte[] input, PrivateKey privateKey)throws Exception{
        byte[] encrypted = null;
        
        try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
            encrypted = cipher.doFinal(input);
            
        } catch(Exception e){
            e.printStackTrace();
        }
        
        return encrypted;
    }
    
    /**
     * 복호화
     * */
    public byte[] decryptRSA(byte[] input, PrivateKey privateKey) throws Exception{
        byte[] decrypted = null;
        try{
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            decrypted = cipher.doFinal(input);
        }catch(Exception e){
            e.printStackTrace();
        }
        
        return decrypted;
    }
    
    /**
     * 복호화
     * */
    public byte[] decryptRSA(byte[] input, PublicKey publicKey) throws Exception{
        byte[] decrypted = null;
        try{
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
            decrypted = cipher.doFinal(input);
        }catch(Exception e){
            e.printStackTrace();
        }
        
        return decrypted;
    }
    
}
 
 
cs


반응형

'컴퓨터보안 > Programming' 카테고리의 다른 글

대칭키 암호화  (0) 2014.12.27
DES 암호화 과정(java)  (0) 2014.10.20