Encryption Library question??

giga

Well-Known Member
Licensed User
Longtime User
You can generate the keys with this code:
B4X:
Dim kpg As KeyPairGenerator
kpg.Initialize("RSA", 128)
kpg.GenerateKey
See the documentation: Basic4android - Encryption

Thanks Erel,

I was taking the more difficult approach to the KeyPairGenerator. I should have tried the simpler way. THANKS Erel!!:sign0162:
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
Well I made some headway, But cannot get the keys to write to a file(s).

B4X:
Dim MyKey() As String
Dim kpg As KeyPairGenerator
   
kpg.Initialize("RSA", 128)
kpg.GenerateKey(MyKey)


Write.Initialize(File.OpenOutput(DirName, FileName3, False))
File.WriteString(DirName, FileName3, MyKey)
Write.Close
:confused:
I have the Filename specified in Sub Process_global but when compiling
I get this error: Object reference not set to an instance of an object. (MyKey)

Thanks for any advice or help on this.
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
GenerateKey doesn't expect any parameter. You should use kpg.PrivateKeyToBytes to get an array of bytes with the key. You can then save it to a file with OutputStream.

Thanks Erel,

I set the PrivateKeyToBytes and it is working to produce the key however they are ALL numbers and dashes. (i.e 48-127-5721048-127-876742-)

Is there a parameter for letters and numbers? :sign0104:
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
I am taking a shot in the dark with this code.

working on generating keypairs Private/Public. With Erels help I was able to get the Key in numeric with dashes but not letters. Therefore not able to get the keys in a true PKCS#8 format.

The server I am connecting to will not accept the key in its current state with dashes.

With the code below I keep getting and error "java.lang.ArrayIndexOutOfBoundsException: length=1; index=1"
No matter what number I put in.

I could not find any further examples of the code.
B4X:
Dim Formats(1,0) As String 
Dim kpg As KeyPairGenerator
Dim MyKeyB(0) As Byte
Dim MyKeyC(0) As List
Dim MyKeyD(1) As Byte
Dim MyKeyE(1) As List
  
kpg.Initialize("RSA",128)
kpg.GenerateKey
MyKeyB = kpg.PrivateKeyToBytes
kpg.PrivateKeyFromBytes(MyKeyB)
MyKeyC(0) = kpg.PrivateKey

MyKeyD = kpg.PublicKeyToBytes
kpg.PublicKeyFromBytes(MyKeyD)
MyKeyE(1) = kpg.PublicKey

Is this correct or am I going about this all wrong?
Thanks for any Help.
 
Upvote 0
Top