Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
amitnath
Posts: 16
Registered: ‎09-12-2008
My Device: Not Specified

NoSuchAlgorithmException - when storing a symmetric key into the DeviceKeyStore

How can we store a symmetric key into the DeviceKeyStore ?

I have attempted to do (see the sample src code below), but recieve the following exception

 

NoSuchAlgorithmException

 

This same occurs regardless of the Algorithm that I use.

- TripleDES

- DES

- AES

 

What I am doing wrong ?

 

-------------------------------------------------------------------------------------------------------------------------------------

KeyStore ks = DeviceKeyStore.getInstance();
   
if (ks == null) {
 return;
}
 
// CREATE KEY
String label = "Secure KEY";
TripleDESKey key = new TripleDESKey();
String symmetricKeyEncodingAlgorithm = key.getAlgorithm();
   
// LOAD KEY
try {
 ks.set(null,  label,  key,  symmetricKeyEncodingAlgorithm,  KeyStore.SECURITY_LEVEL_HIGH,
 ks.getTicket());

} catch (Exception e) {

}

 

-------------------------------------------------------------------------------------------------------------------------------------

 

The error occurs on the following environments

 

- simulator (JDE 4.5)

 

- blackberry Pearl (8100)

  OS 4.5.0.102 (platform 2.7.0.85)

  crypto kernel 3.8.5.32a

Please use plain text.
Developer
dmazgalin
Posts: 92
Registered: ‎01-15-2009
My Device: Blackberry 9550

Re: NoSuchAlgorithmException - when storing a symmetric key into the DeviceKeyStore

Hi. According to RIM Crypto API try to use this code

 

        try
        {
            KeyStore keyStore = DeviceKeyStore.getInstance();
            byte[] keyA = new byte[8];
            TripleDESKey ca = new TripleDESKey(keyA);
            AssociatedData associatedData = new AssociatedData(AssociatedData.EMAIL, "myemail@mycompany.com".getBytes());
            keyStore.set(new AssociatedData[]{ associatedData }, "Key", ca, ca.getAlgorithm(), KeyStore.SECURITY_LEVEL_HIGH, null);
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }

 

Regards, Dmitry.

Please use plain text.
New Developer
amitnath
Posts: 16
Registered: ‎09-12-2008
My Device: Not Specified

Re: NoSuchAlgorithmException - when storing a symmetric key into the DeviceKeyStore

 

Thanks Dmitry for the updated code suggestions. I have given this a try, and recieved a new exception

IllegalArgumentException

 

then I made a very slight change to your suggested code....

byte[] keyA = new byte[8];

was changed to

byte[] keyA = new byte[16];

 

According to the API the lengths need to be 16 & 24 bytes long.

After that change, the IlegalArugmentException error is now gone, but  I still get a NuchSuchAlgorithm Exception.

 

Any additional ideas

 

Amit

 

PS:  below is the revised code

 

try {
               
KeyStore keyStore = DeviceKeyStore.getInstance();
byte[] keyA = new byte[16];
TripleDESKey ca = new TripleDESKey(keyA);
AssociatedData associatedData = new AssociatedData(AssociatedData.EMAIL, "myemail@mycompany.com".getBytes());        
keyStore.set(new AssociatedData[]{ associatedData }, "Key", ca, ca.getAlgorithm(), KeyStore.SECURITY_LEVEL_HIGH, null);

                       
} catch (Exception e) {

// error handler

}

Please use plain text.