Convert private key in PEM format

后端 未结 3 1318
后悔当初
后悔当初 2021-01-04 22:52

I have created a self-signed certificate with Java code and added into KeyStore. Now I want to export Private key and Certificate created, into a file in PEM format. Is it

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-04 23:52

    On Android, you can use the following Kotlin extension function:

    import android.util.Base64
    import java.security.PublicKey
    
    fun PublicKey.toPemString(): String {
        val publicKeyBase64: String = Base64.encodeToString(this.encoded, Base64.NO_WRAP)
        return publicKeyBase64.chunked(64).joinToString(
            separator = "\n",
            prefix = "-----BEGIN PUBLIC KEY-----\n",
            postfix = "\n-----END PUBLIC KEY-----\n"
        )
    }
    

提交回复
热议问题