View Single Post
Old 05-14-2008, 01:28 AM   #1
skynetchris
Knows Where the Search Button Is
 
Join Date: Apr 2008
Model: 7100T
PIN: N/A
Carrier: optus
Posts: 24
Default Any Encryption (DES or RSA x509).

Please Login to Remove!

Hi guys,

I've been asked to encrypt a file before its written to SDCard.

I've tried both DES and RSA with no luck.

I wanted to create a certificate that the BB could use to encrypt with and store the private key on the PC to decrypt with.

So i started with RSA(following RIM knowledge base AES example). Which I can run by itself and it works well.

I followed a google code example of creating the necessary public and private keys along with a certificate. That went fine.

The problem comes when I try to import the certificate and use the public key, i tried:

Code:
 try
{
   fconn = (FileConnection)Connector.open("file:///SDCard/cert.pfx",Connector.READ);
   fileStream = fconn.openInputStream();
   X509Certificate cert = new X509Certificate(fileStream);
   RSAPublicKey publicKey = (RSAPublicKey)cert.getPublicKey();
   publicKey.verify();
}
With all appropriate catch statements

Which seems to work without a hitch, but then I pass it to the encrypt method used in the Knowledge base example and it gives Java.lang exception?

Also if I try and verify the x509 certificate, it complains its not RCC signed?


So then I switched to DES and tried the API example:

Code:
 

   String pword = "pass";
   String plain = "Convert me";
   byte[] text = plain.getBytes();
   byte[] secretKey = pword.getBytes();
   byte[] encrypted = null;
   sampleDESEncryption(secretKey,text,encrypted);

    // sampleDESEncryption
    private static int sampleDESEncryption( byte[] secretKey, byte[] plainText, byte[] cipherText )
        throws CryptoTokenException, CryptoUnsupportedOperationException
    {
        // Create a new DES key based on the 8 bytes in the secretKey array
        DESKey key = new DESKey( secretKey );
                
        // Create a new instance of the DES encryptor engine, passing in the newly 
        // created key
        DESEncryptorEngine engine = new DESEncryptorEngine( key );
        // Encrypt one block (8 bytes) of plainText into cipherText
        engine.encrypt( plainText, 0, cipherText, 0 );
    
        // Return the block size of the engine
        return engine.getBlockLength();
    }
But I dont understand how to get the cipher text back? I tried returning it ({cipherText} return encrypted = sampleDESEncryption(...)) and it complained it was null?

What I would love is a code example of importing and using a java created certificate for encryption. Or a simple des encryption decryption example.

Any ideas?

Cheers,
Chris.
Offline   Reply With Quote