Android Question Calculate the java.security.Key

Francesco Maresca

Member
Licensed User
Longtime User
Take a look at the pem file:

View attachment 50552

1. It looks like a Base64-String
2. There's a header and a footer (which is not Base64)
3. There are LF's at the end of each line (which is not Base64, too)

What we need is a clean Base64 string:

B4X:
    Dim Pem As String
    Dim PemBytes() As Byte

    Pem=File.ReadString(File.DirApp,"pem.pem") 'read the pem file into a string
    Pem=Pem.Replace("-----BEGIN PRIVATE KEY-----","")
    Pem=Pem.Replace("-----END PRIVATE KEY-----","")
    Pem=Pem.Replace(CRLF,"")

After this it is:

B4X:
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDx4a//WIdrQfPjKMlNCXSHuISFd7CkIkqPN14EKbEC9EvsA59Ct6qy4jkG8LaajcX07bOSdJVkNMpjLsNE8sTdS4MNjwkcsmqjnMpnKCcR0UKwnShTZyUjevceFSeiopWEZ2LS91qPL0dg1S5NCgV/ds0E5jcx/KRsdwS35npjzRWnV4gFwCcVOKXm/3L5lRBB+y6PT2P9bRGKctusOlkuImK1BT8o0/ohATnlLB7uEfB+AuFJwYP/KUYVSYGNjuBklh1wCjhuMsGWeoU2uZcrN8F7D/jKZ/nsKefVABw6ELP0NP9EsHZSQ/i77szP1NjxdOAW5qdlsPNtEDE3th9pAgMBAAECggEAaIp3i7ASj0vpAePt5OrSP5qDREAvyzC68t0mODxgsfsuGSqTmb1R/JCDvzkoYvIlfa+CrijPLIkYc/eRE+qtJUvur1yDvrBq...

This is a clean Base64-string :)

1. Convert it into Bytes
2. Load it

Full code:

B4X:
    Dim Pem As String
    Dim PemBytes() As Byte

    Pem=File.ReadString(File.DirApp,"pem.pem") 'load from file into string
    Pem=Pem.Replace("-----BEGIN PRIVATE KEY-----","")
    Pem=Pem.Replace("-----END PRIVATE KEY-----","")
    Pem=Pem.Replace(CRLF,"")
    Log(Pem)
    
    Dim su As StringUtils
    PemBytes=su.DecodeBase64(Pem) 'Base64 -> Bytes

    Dim TestC As Cipher
    Dim TestKPG As KeyPairGenerator
    TestC.Initialize("RSA/ECB/PKCS1Padding")
       TestKPG.Initialize("RSA", 2048)
    TestKPG.PrivateKeyFromBytes(PemBytes) 'Works!


Note: Usually you get the PUBLIC key, not the private one (you encrypt with it and it is DEcrypted with the private key). So if two programs use it, there will be 2 Public- and 2 Private Keys!


I thank you for the help you are giving me.
My problem now is another:
1 - From the text file I read a key
2 - The key is an RSA PRIVATE KEY
3 - Convert the key read from RSA PRIVATE KEY in PRIVATE KEY

How can I perform the third operation?
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
3 - Convert the key read from RSA PRIVATE KEY in PRIVATE KEY

This IS the private key! Maybe you are getting confused with RSA.

RSA uses two keys: Private and Public. In OpenSSL you generate both with:

B4X:
openssl genrsa  -out PrivateKey.pem 2048 'or higher
openssl pkcs8 -topk8 -inform pem -in PrivateKey.pem -outform pem -nocrypt -out C:/xampp/htdocs/rsa/PrivateKey.pem
openssl rsa -pubout -in PrivateKey.pem -out C:/xampp/htdocs/rsa/PublicKey.pem

With this we have

PrivateKey.pem = Private key with headers and "eye friendly"
PublicKey.pem = Public key with headers and "eye friendly"

In B4x we need to adapt it a bit (see my code) and load it as shown in my code. If you and I want to share RSA encrypted data I will give you MY Public Key and you are giving me your Public Key. The Private Keys are top secret!!!! If I send you a message I encrypt it with YOUR Public Key and only you can decrypt it with your Private Key and vice versa.

So you have 2 Ciphers and 2 KeyPairGenerators (yours and mine). You generate the keys in B4x in the first and LOAD my Public key in the second. To load a OpenSSL pem key, the key has to be adapted (see my code).

If you have further questions, please describe the complete process. Example: My app needs to share encrypted data with xxxx, etc. You need to understand what your needs are (not in small steps, the whole thing like "where are the keys from, how do I exchange them, etc.).
 
Upvote 0
Top