Notice
Recent Posts
Recent Comments
Link
투케이2K
468. (Android/Java) X.509 Certificate 인증서 정보를 base64 로 변경 및 base64 를 X509Certificate 인증서로 변경 실시 본문
Android
468. (Android/Java) X.509 Certificate 인증서 정보를 base64 로 변경 및 base64 를 X509Certificate 인증서로 변경 실시
투케이2K 2023. 1. 27. 20:45[개발 환경 설정]
개발 툴 : AndroidStudio
[소스 코드]
// -------------------------------------
// [로직 처리 실시]
// -------------------------------------
try {
// [생성할 인증서 정보 및 유효 일자 정의 실시]
String KEY_ALIAS = "PrivateKeyTwok";
String AndroidKeyStore = "AndroidKeyStore";
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.add(Calendar.YEAR, 20);
// [AlgorithmParameterSpec 사용해 인증서 정보 정의]
AlgorithmParameterSpec spec = null;
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){
spec = new KeyGenParameterSpec.Builder(KEY_ALIAS, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setCertificateSubject(new X500Principal("CN=" + KEY_ALIAS + " CA Certificate"))
.setCertificateSerialNumber(BigInteger.ONE)
.setCertificateNotBefore(start.getTime())
.setCertificateNotAfter(end.getTime())
.setBlockModes(KeyProperties.BLOCK_MODE_ECB)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
.build();
}
else {
spec = new KeyPairGeneratorSpec.Builder(getApplicationContext())
.setAlias(KEY_ALIAS)
.setSubject(new X500Principal("CN=" + KEY_ALIAS + " CA Certificate"))
.setSerialNumber(BigInteger.ONE)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
}
// [비대칭 암호화 새 개인 키 생성 및 KeyStore 에 저장 실시]
KeyPairGenerator kpg = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, AndroidKeyStore);
kpg.initialize(spec);
KeyPair kp = kpg.generateKeyPair();
// [Keystore 로드]
KeyStore keyStore = KeyStore.getInstance(AndroidKeyStore);
keyStore.load(null);
// [인증서 로드 : X.509]
Certificate originCertificate = keyStore.getCertificate(KEY_ALIAS);
// -------------------------------------
// [인증서 정보를 base64 로 변경 실시]
StringBuffer base64Encode = new StringBuffer();
base64Encode.append("-----BEGIN CERTIFICATE-----\n");
base64Encode.append(Base64.encodeToString(originCertificate.getEncoded(), Base64.DEFAULT).trim());
base64Encode.append("\n-----END CERTIFICATE-----");
// -------------------------------------
// [base64 데이터를 다시 인증서로 변경]
String certData = base64Encode.toString();
certData = certData.replaceAll("-----BEGIN CERTIFICATE-----", "");
certData = certData.replaceAll("-----END CERTIFICATE-----", "");
certData = certData.replaceAll(" ", "");
byte encodedCert[] = Base64.decode(certData, Base64.DEFAULT);
ByteArrayInputStream inputStream = new ByteArrayInputStream(encodedCert);
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
X509Certificate convertCertificate = (X509Certificate)certFactory.generateCertificate(inputStream);
// -------------------------------------
// [로그 출력 실시]
S_Log.ltd("================================================");
S_Log.cnt("["+ACTIVITY_NAME+" >> "+C_Util.getNowMethod(1)+" :: 로그 출력 실시]");
S_Log.cnt("----------------------------------------");
S_Log.cnt("[keyStore isKeyEntry :: "+String.valueOf(keyStore.isKeyEntry(KEY_ALIAS))+"]");
S_Log.cnt("----------------------------------------");
S_Log.cnt("[originCertificate getType :: "+String.valueOf(originCertificate.getType())+"]");
S_Log.cnt("----------------------------------------");
S_Log.cnt("[originCertificate toString :: "+String.valueOf(originCertificate.toString())+"]");
S_Log.cnt("----------------------------------------");
S_Log.cnt("[base64Encode :: "+String.valueOf(base64Encode.toString())+"]");
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.lbd("================================================");
}
catch (Exception e){
e.printStackTrace();
}
[결과 출력]
D///===========//: ================================================
I/: [A_Intro >> onCreate :: 로그 출력 실시]
I/: ----------------------------------------
I/: [keyStore isKeyEntry :: true]
I/: ----------------------------------------
I/: [originCertificate getType :: X.509]
I/: ----------------------------------------
I/: [originCertificate 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:
....
25:7d
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Key Usage: critical
Key Encipherment, Data Encipherment
Signature Algorithm: sha256WithRSAEncryption
]
I/: ----------------------------------------
I/: [base64Encode :: -----BEGIN CERTIFICATE-----
MIIB2zCCAcWgAwIBAgIBATANBgkqhkiG9w0BAQsFADAoMSYwJAYDVQQDEx1Qcml2YXRlS2V5VHdv
ayBDQSBDZXJ0aWZpY2F0ZTAeFw0yMzAxMjYyMzEzNDVaFw00MzAxMjYyMzEzNDVaMCgxJjAkBgNV
BAMTHVByaXZhdGVLZXlUd29rIENBIENlcnRpZmljYXRlMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
BAMCBDAwDQYJKoZIhvcNAQELBQADAQA=
-----END CERTIFICATE-----]
I/: ----------------------------------------
I/: [certData ::
MIIB2zCCAcWgAwIBAgIBATANBgkqhkiG9w0BAQsFADAoMSYwJAYDVQQDEx1Qcml2YXRlS2V5VHdv
ayBDQSBDZXJ0aWZpY2F0ZTAeFw0yMzAxMjYyMzEzNDVaFw00MzAxMjYyMzEzNDVaMCgxJjAkBgNV
BAMTHVByaXZhdGVLZXlUd29rIENBIENlcnRpZmljYXRlMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
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, ....... , 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:
....
25:7d
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Key Usage: critical
Key Encipherment, Data Encipherment
Signature Algorithm: sha256WithRSAEncryption
]
D///===========//: ================================================
반응형
'Android' 카테고리의 다른 글
Comments