Android Question RSA PrivateKeyFromBytes does not load the new private key values

Darsiar

Member
RSA PrivateKeyFromBytes does not load the new private key values, whereas PublicKeyFromBytes works fine

B4X:
Public MyPublicRSAKeyAsBytes() As Byte, MyPrivateRSAKeyAsBytes() As Byte

Private ForeignKPG, OwnKPG As KeyPairGenerator

Sub GenerateOWNRSAKeys
    ' C.Initialize("RSA/ECB/PKCS1Padding")
    OwnKPG.Initialize("RSA", 2048)
    OwnKPG.GenerateKey
     MyPrivateRSAKeyAsBytes=OwnKPG.PrivateKeyToBytes
     MyPublicRSAKeyAsBytes=OwnKPG.PublicKeyToBytes  
   
    'Key load TEST
     OwnKPG.Initialize("RSA", 2048)
    OwnKPG.PrivateKeyFromBytes(MyPrivateRSAKeyAsBytes)
    MyPrivateRSAKeyAsBytes = OwnKPG.PrivateKey ' <--- Error (Key = NULL)'
End Sub
 

Darsiar

Member
But in this test we immediately generate the same object, how can it be wrong? In another example, we generate and record it, and we give it public through a network to another computer. There everything is correctly encrypted with a public key and the result is sent back. But we cannot decrypt, because the stored private key is no longer working. We took the code from your RSA examples. What can be wrong? Where do we look?
 
Upvote 0

Darsiar

Member
Error clarification:
1. If you create a key pair, write them to the database through a Base64 encoder, encrypt the data, write them to the database, and without shutting down the program, immediately read the keys and data from the database, the decoder works fine.

2. If you turn off the program and turn it on again, read all the parameters from the database (from the same records that work) into the same decoder, then the decoder cannot load the private key.
3. The same situation occurs if data was transferred between computers after turning off one of the programs.

Visually, all keys are recoverable from the database and are identically fresh.

Presumably, something remains in the encryption-decryption library.

Help me please.
We include a test example:
B4X:
    ' Encoding "TEST TEXT"
    Dim cc As String = GetDataKeyValue("mePublicKey")
    Dim s As String =  EncodeAsePassword("TEST TEXT", cc)    ' зашифровали
 
    'Test Save to BD Key="ppp1"
    SaveDataKeyValue("ppp1",s)
 
 
    ' Test load in BD (key ="ppp1")'
    Dim s1 As String = GetDataKeyValue("ppp1")
    ' decode'
    Dim s= DecodePassword(s1)
 
' SUB  Decoder'
    Sub DecodePassword(code As String)As String
    Dim ret As String=""
    Try
        Dim b() As Byte = su.DecodeBase64(code)
     
     
        Dim ss As String =ckvs.GetDataKeyValue("mePryvateKey")
        Dim bbb() As Byte =su.DecodeBase64(ss)
     
     
        Dim oKPG As KeyPairGenerator
        oKPG.Initialize("RSA", 2048)
        oKPG.PrivateKeyFromBytes(bbb)
     
        Dim ss As String =ckvs.GetDataKeyValue("mePublicKey")
         bbb  =su.DecodeBase64(ss)
         oKPG.PublicKeyFromBytes(bbb)    
        Dim b1() As Byte=CI.Decrypt(b ,oKPG.PrivateKey,False)
        ret = BC.StringFromBytes(b1,"UTF8")
     
    Catch
        Log("RSA decode error for ok-meet operation.")
    End Try
    Return ret
End Sub

  Sub EncodeAsePassword(asePassword As String, publicKey As String) As String
    ForeignPublicRSAKeyAsBytes=su.DecodeBase64(publicKey)
    ForeignKPG.Initialize("RSA", 2048)
    ForeignKPG.PublicKeyFromBytes(ForeignPublicRSAKeyAsBytes)
    CI.Initialize("RSA/ECB/PKCS1Padding")
    Log("Sending Password")        
    Return su.EncodeBase64(CI.Encrypt(asePassword.GetBytes("UTF8"),ForeignKPG.PublicKey,False))  'JG.ToString
End Sub
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
RSA PrivateKeyFromBytes does not load the new private key values, whereas PublicKeyFromBytes works fine
Well, that seems heck crazy. What about if you swap the order that you access them in? Is it possible that they use the same internal buffer?

Also, maybe try adding these Logs:
B4X:
    'Key load TEST
Log("before " & MyPrivateRSAKeyAsBytes.Length)
     OwnKPG.Initialize("RSA", 2048)
Log("after " & MyPrivateRSAKeyAsBytes.Length)
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
MyPrivateRSAKeyAsBytes = OwnKPG.PrivateKey '

This is a typo. PrivateKey is an object, you want to use PrivateKeyToBytes.

Maybe your Base64 string gets corrupted on the way (url encoding?). How do you store it to the db? PHP?

Try to store the key(s) to a file. Does that work?
 
Upvote 0

Darsiar

Member
Yes thank you. This is probably good advice. When we use a local encoder for each operation, and its local data, the error disappears. Perhaps the reason was that we used the example given here, in which the encoder was global for all operations. However, all the same, it seems strange to us. Perhaps developers need to add a method for cleaning internal data, for the encoder?
Save to file also helps the local encoder.
For some reason, a database with converting bytes to a string and vice versa distorts the private key.
 
Upvote 0
Top