공개키 암호화 [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

대칭키 암호화

Posted by 백창
2014. 12. 27. 17:52 컴퓨터보안/Programming
반응형


 개념


 대칭키 암호에서는 암호화를 하는 측과 복호화를 하는 측이 같은 비밀키를 사용하여 암복호화를 수행하는 방식을 말한다. 공개키 암호화 방식에 비해 빠른 계산속도를 가지지만 비밀키 분배가 어렵고 중요하다.

 

 대칭키의 종류로는 AES, DES 등이 있다.



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



 AES 구현


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
111
112
113
 
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
 
 
public class AES{
    private String ALGO = "AES/CBC/PKCS5Padding";
    private SecretKeySpec skeySpec;
    private String iv = "abcdabcdabcdabcd";
    
    util u = new util();
    /**
     * Key 생성
     * */
    public SecretKeySpec keyGen(String key){
        
        skeySpec = new SecretKeySpec(key.getBytes(), "AES");
        
        return skeySpec;
    }
    
    /**
     * Key 설정
     * */
    public void setKey(SecretKeySpec skey){
        skeySpec = skey;
    }
    
    /**
     * 암호화 - String Version
     * */
    public String encryptAES(String s) throws Exception{
        String encrypted = null;
        
        try {
//            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
        
            Cipher cipher = Cipher.getInstance(ALGO);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv.getBytes()));
            encrypted = u.byte2Hex(cipher.doFinal(s.getBytes()));
        } catch(Exception e){
            e.printStackTrace();
        }
        
        return encrypted;
        
    }
    
    /**
     * 암호화 - ByteArray Version
     * */
    public byte[] encryptAES(byte[] input) throws Exception{
        byte[] encrypted = null;
        
        try {
//            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
        
            Cipher cipher = Cipher.getInstance(ALGO);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv.getBytes()));
            encrypted = cipher.doFinal(input);
        } catch(Exception e){
            e.printStackTrace();
        }
        
        return encrypted;
        
    }
    
    /**
     * 복호화 - String Version
     * */
    public String decryptAES(String s) throws Exception{
        String decrypted = null;
        
        try{
//            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
            
            Cipher cipher = Cipher.getInstance(ALGO);
            
            cipher.init(Cipher.DECRYPT_MODE, skeySpec,new IvParameterSpec(iv.getBytes()));
            decrypted = new String(cipher.doFinal(u.hex2Byte(s)));
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return decrypted;
    }
    
    /**
     * 복호화 - ByteArray Version
     * */
    public byte[] decryptAES(byte[] input) throws Exception{
        byte[] decrypted = null;
        
        try{
//            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
            
            Cipher cipher = Cipher.getInstance(ALGO);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec,new IvParameterSpec(iv.getBytes()));
            decrypted = cipher.doFinal(input);
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return decrypted;
    }
    
    
    
}
 
 
cs



반응형

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

공개키 암호화 [RSA구현]  (0) 2015.01.16
DES 암호화 과정(java)  (0) 2014.10.20

DES 암호화 과정(java)

Posted by 백창
2014. 10. 20. 10:10 컴퓨터보안/Programming
반응형


 개요

 

 DES 암호화 과정 16R를 손으로 풀어보라는 과제 때문에 작성한 코드 백업용



 소스


급한대로 작성

 

desCrypt.zip



반응형

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

공개키 암호화 [RSA구현]  (0) 2015.01.16
대칭키 암호화  (0) 2014.12.27