I am new in Android and I have started to working on existing project which have some encryption algorithm,
Below is existing code
var secureRandom = SecureRandom()
var masterKey = ByteArray(32)
secureRandom.nextBytes(masterKey)
var keyGen = KeyPairGenerator.getInstance("RSA")
keyGen.initialize(2048)
var keyPair = keyGen.generateKeyPair()
var pubKey = keyPair.public
var subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(ASN1Sequence.getInstance(pubKey.encoded))
var pubKeyEncoded = Base64.encodeToString(subjectPublicKeyInfo.parsePublicKey().encoded, Base64.DEFAULT)
var sb = StringBuilder()
sb.append("-----BEGIN RSA PUBLIC KEY-----\n")
sb.append(pubKeyEncoded)
sb.append("\n-----END RSA PUBLIC KEY-----\n")
val publicKey = sb.toString()
val privateKey = keyPair.private as RSAPrivateKey
val string = StringWriter()
var writer = PemWriter(string)
writer.writeObject(privateKey)//<-----Getting an error like Type Mismatch
writer.close()
Can anyone help me how to resolve this issue, I am totally new in this encryption fields, anyone suggest me from where I can get deeper understanding on cryptography,
from above code I have just understand that we are getting two keys like public key and private key for AES
We are encrypting public key by below code
var subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(ASN1Sequence.getInstance(pubKey.encoded))
var pubKeyEncoded = Base64.encodeToString(subjectPublicKeyInfo.parsePublicKey().encoded, Base64.DEFAULT)
But I still did not understand what will be use of SubjectPublicKeyInfo, ASN1Sequence, PemWriter if anyone have knowledge then please explain me.
I am also getting compile time error like Required PemObjectGenerator! found RSAPrivateKey