'공개키'에 해당되는 글 1건

  1. 2013.08.05 공개키 암복호화 샘플
2013. 8. 5. 10:48
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
/*
 * 작성일   : 2013. 8. 05.
 * 제목      : TestRSA
 * 설명      : 공개키 암호화 방식(RSA 알고리즘)의 암복호화
 */
 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
 
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.security.cert.CertificateException;
import javax.security.cert.X509Certificate;
 
import org.apache.commons.io.IOUtils;
 
public class TestRSA {
 
    /**
     * @param args
     * @throws IOException
     * @throws NoSuchPaddingException
     * @throws InvalidKeySpecException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     */
    public static void main(String[] args) throws Exception {
 
 
        PublicKey publicKey = getPublicKey();
        PrivateKey privateKey = getPrivateKey();
 
 
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
 
        String text = "아~ 인생은 나그네길~";
        byte[] t0 = text.getBytes();
        System.out.println("P : " + IOUtils.toString(t0));
        byte[] b0 = cipher.doFinal(t0);
        System.out.println("E : " + IOUtils.toString(b0));
          
 
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] b1 = cipher.doFinal(b0);
          
        System.out.println("D : " + IOUtils.toString(b1));
        System.out.println(new String(b1));
 
         
         
    }
 
     
 
    public static PublicKey getPublicKey() throws FileNotFoundException, IOException,
            NoSuchAlgorithmException, InvalidKeySpecException,
            NoSuchPaddingException, InvalidKeyException, CertificateException {
 
        FileInputStream fis = new FileInputStream("d:/sungwon-T410i.crt");
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        X509Certificate x509Cert = X509Certificate.getInstance(buffer);
        PublicKey key = x509Cert.getPublicKey();
 
        return key;
    }
 
    public static PrivateKey getPrivateKey() throws FileNotFoundException,
            IOException, NoSuchAlgorithmException, InvalidKeySpecException,
            NoSuchPaddingException, InvalidKeyException {
        FileInputStream fis = new FileInputStream("d:/sungwon-T410i.der");
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
 
        KeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PrivateKey key = kf.generatePrivate(keySpec);
         
 
        return key;
    }
 
}
Posted by sungwonpekr