투케이2K

469. (Android/Java) X.509 Certificate 인증서에서 공개키 (public key) 추출 실시 본문

Android

469. (Android/Java) X.509 Certificate 인증서에서 공개키 (public key) 추출 실시

투케이2K 2023. 1. 27. 20:48

[개발 환경 설정]

개발 툴 : AndroidStudio

 

[소스 코드]

        // -------------------------------------
        // [로직 처리 실시]
        // -------------------------------------
        try {

            // [테스트 인증서 base64 문자열 선언]
            String certData = "MIIB2zCCAcWgAwIBAgIBATANBgkqhkiG9w0BAQsFADAoMSYwJAYDVQQDEx1Qcml2YXRlS2V5VHdv\n" +
                    "    ayBDQSBDZXJ0aWZpY2F0ZTAeFw0yMzAxMjYyMzEzNDVaFw00MzAxMjYyMzEzNDVaMCgxJjAkBgNV\n" +
                    "    BAMTHVByaXZhdGVLZXlUd29rIENBIENlcnRpZmljYXRlMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A\n" +
                    "    MIIBCgKCAQEAovqCZdnbJ7eTNraWOAdB14Im3f3ippwJ28kQmVwo3pCDZdkznbiN8BT798rO4Aoy\n" +
                    "    QDl1GMDnrPtLZTHLIRKJ3b/VBDv1y1mZIsD4eJvavbbJgjuoNF+nleM9cWhNsMKfMnbOGHJ7xYtQ\n" +
                    "    WO3qj/XpU2iZn+UKbu3x8NfWHN0QIpR6Fz2k4zwa0gyn92m0Q9e2DKeO/423Y8JscASNqMGsPVkT\n" +
                    "    kd61cvSxqa90Vc79jDiJpYZXHaovek2ipDZfh3X+1Y19ZmD2DjJ6ahYHmu1iEzHKc2UxQ7hl610K\n" +
                    "    qbyDyUE4yBaTU/TGlSwK2cqcCV4ASFWyV/mxw6K4TWnorcElfQIDAQABoxIwEDAOBgNVHQ8BAf8E\n" +
                    "    BAMCBDAwDQYJKoZIhvcNAQELBQADAQA=";
            certData = certData.replaceAll(" ", "");

            // -------------------------------------

            // [base64 문자열을 byte 변환 >> 인증서로 변환 실시]
            byte encodedCert[] = Base64.decode(certData, Base64.DEFAULT);

            ByteArrayInputStream inputStream  =  new ByteArrayInputStream(encodedCert);

            CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
            X509Certificate convertCertificate = (X509Certificate)certFactory.generateCertificate(inputStream);

            // -------------------------------------

            // [공개키 추출 실시 >> 암호화 사용]
            RSAPublicKey publicKey = (RSAPublicKey) convertCertificate.getPublicKey();
            String base64PublicKey = Base64.encodeToString(publicKey.getEncoded(), Base64.DEFAULT);

            // -------------------------------------

            // [로그 출력 실시]
            S_Log.ltd("================================================");
            S_Log.cnt("["+ACTIVITY_NAME+" >> "+C_Util.getNowMethod(1)+" :: 로그 출력 실시]");
            S_Log.cnt("----------------------------------------");
            S_Log.cnt("[certData :: "+String.valueOf(certData)+"]");
            S_Log.cnt("----------------------------------------");
            S_Log.cnt("[encodedCert :: "+String.valueOf(Arrays.toString(encodedCert))+"]");
            S_Log.cnt("----------------------------------------");
            S_Log.cnt("[convertCertificate getType :: "+String.valueOf(convertCertificate.getType())+"]");
            S_Log.cnt("----------------------------------------");
            S_Log.cnt("[convertCertificate toString :: "+String.valueOf(convertCertificate.toString())+"]");
            S_Log.cnt("----------------------------------------");
            S_Log.cnt("[base64PublicKey :: "+String.valueOf(base64PublicKey)+"]");
            S_Log.lbd("================================================");

        }
        catch (Exception e){
            e.printStackTrace();
        }
 

[결과 출력]

 

D///===========//: ================================================
I/: [A_Intro >> onCreate :: 로그 출력 실시]
I/: ----------------------------------------
I/: [certData :: MIIB2zCCAcWgAwIBAgIBATANBgkqhkiG9w0BAQsFADAoMSYwJAYDVQQDEx1Qcml2YXRlS2V5VHdv
    ayBDQSBDZXJ0aWZpY2F0ZTAeFw0yMzAxMjYyMzEzNDVaFw00MzAxMjYyMzEzNDVaMCgxJjAkBgNV
    BAMTHVByaXZhdGVLZXlUd29rIENBIENlcnRpZmljYXRlMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
    MIIBCgKCAQEAovqCZdnbJ7eTNraWOAdB14Im3f3ippwJ28kQmVwo3pCDZdkznbiN8BT798rO4Aoy
    QDl1GMDnrPtLZTHLIRKJ3b/VBDv1y1mZIsD4eJvavbbJgjuoNF+nleM9cWhNsMKfMnbOGHJ7xYtQ
    WO3qj/XpU2iZn+UKbu3x8NfWHN0QIpR6Fz2k4zwa0gyn92m0Q9e2DKeO/423Y8JscASNqMGsPVkT
    kd61cvSxqa90Vc79jDiJpYZXHaovek2ipDZfh3X+1Y19ZmD2DjJ6ahYHmu1iEzHKc2UxQ7hl610K
    qbyDyUE4yBaTU/TGlSwK2cqcCV4ASFWyV/mxw6K4TWnorcElfQIDAQABoxIwEDAOBgNVHQ8BAf8E
    BAMCBDAwDQYJKoZIhvcNAQELBQADAQA=]
I/: ----------------------------------------
I/: [encodedCert :: [48, -126, 1, -37, 48, -126, 1, -59, -96, 3, 2, 1, 2, 2, 1, 1, 48, 13, 6, 9, 42, -122, 72, -122, -9, 13, 1, 1, 11, 5, 0, 48, 40, 49, 38, 48, 36, 6, 3, 85, 4, 3, 19, 29, 80, 114, 105, 118, 97, 116, 101, 75, 101, 121, 84, 119, 111, 107, 32, 67, 65, 32, 67, 101, 114, 116, 105, 102, 105, 99, 97, 116, 101, 48, 30, 23, 13, 50, 51, 48, 49, 50, 54, 50, 51, 49, 51, 52, 53, 90, 23, 13, 52, 51, 48, 49, 50, 54, 50, 51, 49, 51, 52, 53, 90, 48, 40, 49, 38, 48, 36, 6, 3, 85, 4, 3, 19, 29, 80, 114, 105, 118, 97, 116, 101, 75, 101, 121, 84, 119, 111, 107, 32, 67, 65, 32, 67, 101, 114, 116, 105, 102, 105, 99, 97, 116, 101, 48, -126, 1, 34, 48, 13, 6, 9, 42, -122, 72, -122, -9, 13, 1, 1, 1, 5, 0, 3, -126, 1, 15, 0, 48, -126, 1, 10, 2, -126, 1, 1, 0, -94, -6, -126, 101, -39, -37, 39, -73, -109, 54, -74, -106, 56, 7, 65, -41, -126, 38, -35, -3, -30, -90, -100, 9, -37, -55, 16, -103, 92, 40, -34, -112, -125, 101, -39, 51, -99, -72, -115, -16, 20, -5, -9, -54, -50, -32, 10, 50, 64, 57, 117, 24, -64, -25, -84, -5, 75, 101, 49, -53, 33, 18, -119, -35, -65, -43, 4, 59, -11, -53, 89, -103, 34, -64, -8, 120, -101, -38, -67, -74, -55, -126, 59, -88, 52, 95, -89, -107, -29, 61, 113, 104, 77, -80, -62, -97, 50, 118, -50, 24, 114, 123, -59, -117, 80, 88, -19, -22, -113, -11, -23, 83, 104, -103, -97, -27, 10, 110, -19, -15, -16, -41, -42, 28, -35, 16, 34, -108, 122, 23, 61, -92, -29, 60, 26, -46, 12, -89, -9, 105, -76, 67, -41, -74, 12, -89, -114, -1, -115, -73, 99, -62, 108, 112, 4, -115, -88, -63, -84, 61, 89, 19, -111, -34, -75, 114, -12, -79, -87, -81, 116, 85, -50, -3, -116, 56, -119, -91, -122, 87, 29, -86, 47, 122, 77, -94, -92, 54, 95, -121, 117, -2, -43, -115, 125, 102, 96, -10, 14, 50, 122, 106, 22, 7, -102, -19, 98, 19, 49, -54, 115, 101, 49, 67, -72, 101, -21, 93, 10, -87, -68, -125, -55, 65, 56, -56, 22, -109, 83, -12, -58, -107, 44, 10, -39, -54, -100, 9, 94, 0, 72, 85, -78, 87, -7, -79, -61, -94, -72, 77, 105, -24, -83, -63, 37, 125, 2, 3, 1, 0, 1, -93, 18, 48, 16, 48, 14, 6, 3, 85, 29, 15, 1, 1, -1, 4, 4, 3, 2, 4, 48, 48, 13, 6, 9, 42, -122, 72, -122, -9, 13, 1, 1, 11, 5, 0, 3, 1, 0]]
I/: ----------------------------------------
I/: [convertCertificate getType :: X.509]
I/: ----------------------------------------
I/: [convertCertificate toString :: Certificate:
        Data:
            Version: 3 (0x2)
            Serial Number: 1 (0x1)
        Signature Algorithm: sha256WithRSAEncryption
            Issuer: CN=PrivateKeyTwok CA Certificate
            Validity
                Not Before: Jan 26 23:13:45 2023 GMT
                Not After : Jan 26 23:13:45 2043 GMT
            Subject: CN=PrivateKeyTwok CA Certificate
            Subject Public Key Info:
                Public Key Algorithm: rsaEncryption
                    Public-Key: (2048 bit)
                    Modulus:
                        00:a2:fa:82:65:d9:db:27:b7:93:36:b6:96:38:07:
                        41:d7:82:26:dd:fd:e2:a6:9c:09:db:c9:10:99:5c:
                        28:de:90:83:65:d9:33:9d:b8:8d:f0:14:fb:f7:ca:
                        ce:e0:0a:32:40:39:75:18:c0:e7:ac:fb:4b:65:31:
                        cb:21:12:89:dd:bf:d5:04:3b:f5:cb:59:99:22:c0:
                        f8:78:9b:da:bd:b6:c9:82:3b:a8:34:5f:a7:95:e3:
                        3d:71:68:4d:b0:c2:9f:32:76:ce:18:72:7b:c5:8b:
                        50:58:ed:ea:8f:f5:e9:53:68:99:9f:e5:0a:6e:ed:
                        f1:f0:d7:d6:1c:dd:10:22:94:7a:17:3d:a4:e3:3c:
                        1a:d2:0c:a7:f7:69:b4:43:d7:b6:0c:a7:8e:ff:8d:
                        b7:63:c2:6c:70:04:8d:a8:c1:ac:3d:59:13:91:de:
                        b5:72:f4:b1:a9:af:74:55:ce:fd:8c:38:89:a5:86:
                        57:1d:aa:2f:7a:4d:a2:a4:36:5f:87:75:fe:d5:8d:
                        7d:66:60:f6:0e:32:7a:6a:16:07:9a:ed:62:13:31:
                        ca:73:65:31:43:b8:65:eb:5d:0a:a9:bc:83:c9:41:
                        38:c8:16:93:53:f4:c6:95:2c:0a:d9:ca:9c:09:5e:
                        00:48:55:b2:57:f9:b1:c3:a2:b8:4d:69:e8:ad:c1:
                        25:7d
                    Exponent: 65537 (0x10001)
            X509v3 extensions:
                X509v3 Key Usage: critical
                    Key Encipherment, Data Encipherment
        Signature Algorithm: sha256WithRSAEncryption
    ]
I/: ----------------------------------------
I/: [base64PublicKey :: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAovqCZdnbJ7eTNraWOAdB14Im3f3ippwJ
    28kQmVwo3pCDZdkznbiN8BT798rO4AoyQDl1GMDnrPtLZTHLIRKJ3b/VBDv1y1mZIsD4eJvavbbJ
    gjuoNF+nleM9cWhNsMKfMnbOGHJ7xYtQWO3qj/XpU2iZn+UKbu3x8NfWHN0QIpR6Fz2k4zwa0gyn
    92m0Q9e2DKeO/423Y8JscASNqMGsPVkTkd61cvSxqa90Vc79jDiJpYZXHaovek2ipDZfh3X+1Y19
    ZmD2DjJ6ahYHmu1iEzHKc2UxQ7hl610KqbyDyUE4yBaTU/TGlSwK2cqcCV4ASFWyV/mxw6K4TWno
    rcElfQIDAQAB
    ]
D///===========//: ================================================

 


반응형
Comments