Search code examples
codenameonebouncycastle

Store keypair in the device in codename one app


I'm currently trying the codename one framework and I would like to know if there was a way to store keypair generated by the bouncy castle library by any chance ?

I tried to store keys using Storage and Preferences but always resulting the same Exception

java.io.IOException: Object type not supported: org.bouncycastle.asn1.pkcs.RSAPublicKey value: org.bouncycastle.asn1.pkcs.RSAPublicKey@581c
    at com.codename1.io.Util.writeObject(Util.java:481)
    at com.codename1.io.Storage.writeObject(Storage.java:227)

Solution

  • You need to save the byte array related to the key and not the key object. E.g.

    byte[] b = key.toASN1Primitive().getEncoded();
    

    And to restore:

    RSAPublicKey key = RSAPublicKey.getInstance(b);
    

    Haven't tried it but looking at the code I think this should work.