BlackBerry Java SDK 7.0 API Reference: Cryptography API Overview
|
|||||||||
PREV NEXT | FRAMES NO FRAMES |
This category contains the packages you can use to perform tasks that involve cryptography. You can use this Crypto API to encrypt and decrypt data, work with secure connections, manage cryptographic keys and digitally sign and verify data. Support is included for both assymetric (public key) and symmetric (private key) encryption.
The Crypto API includes the following packages:
The API is comprehensive. It is designed to provide you with high level abstractions to achieve a wide range of cryptographic tasks. However, the API is also designed to be flexible. If the task you need to do is not covered you can extend the API by implementing your own cryptographic algorithms, encoding schemes, certificates and keystores. These tutorials provide walkthroughs describing how to extend the API.
Because the API is large, it is easier to learn and navigate if you view it as a set of smaller component APIs. The next sections describe the categories of tasks covered by components of the API.
TripleDES is a popular symmetric key encryption algorithm. The Crypto API makes it easy to use TripleDES to encrypt and decrypt messages.
Click here for a code sample that demonstrates how to encrypt and decrypt a message.
import java.io.*;
import net.rim.device.api.crypto.*;
import net.rim.device.api.util.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.util.*;
public class EncodeDecodeString extends UiApplication implements FieldChangeListener
{
private ButtonField _encrypt_button;
private ButtonField _decrypt_button;
private TripleDESKey _key;
private TextField _edit_input_string;
public static void main( String[] args )
{
EncodeDecodeString theApp = new EncodeDecodeString();
theApp.enterEventDispatcher();
}
public void fieldChanged(Field field, int context)
{
String strCurrentMessage = _edit_input_string.getText();
if(field == _encrypt_button)
{
_edit_input_string.setText(encrypt(strCurrentMessage));
}
else
{
_edit_input_string.setText(decrypt(strCurrentMessage));
}
}
public EncodeDecodeString()
{
MainScreen screen = new MainScreen();
screen.setTitle(new LabelField("Crypto Demo", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));
_edit_input_string = new BasicEditField("Message:", null, 256, BasicEditField.FILTER_DEFAULT);
screen.add(_edit_input_string);
_encrypt_button = new ButtonField("Encrypt");
_encrypt_button.setChangeListener(this);
screen.add(_encrypt_button);
_decrypt_button = new ButtonField("Decrypt");
_decrypt_button.setChangeListener(this);
screen.add(_decrypt_button);
pushScreen(screen);
}
public String encrypt(String plaintext)
{
try
{
_key = new TripleDESKey();
TripleDESEncryptorEngine encryptionEngine = new TripleDESEncryptorEngine(_key);
// In most cases, the data that we are going to encrypt will
// not fit into the block length of a cipher. When that happens, we must use a padding
// algorithm to pad out the last block. We are going to use PKCS5 to do the padding for us.
PKCS5FormatterEngine formatterEngine = new PKCS5FormatterEngine( encryptionEngine );
// Use the byte array output stream to catch the encrypted information.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// Create a block encryptor which will help us use the triple des engine.
BlockEncryptor encryptor = new BlockEncryptor( formatterEngine, outputStream );
// Encrypt the actual data.
encryptor.write( plaintext.getBytes() );
// Close the stream. This forces the extra bytes to be padded out if there were
// not enough bytes to fill all of the blocks.
encryptor.close();
// Get the actual encrypted data.
byte[] encryptedData = outputStream.toByteArray();
String strEncrypted = new String(encryptedData);
return(strEncrypted);
}
catch( CryptoTokenException e )
{
System.out.println(e.toString());
}
catch (CryptoUnsupportedOperationException e)
{
System.out.println(e.toString());
}
catch( IOException e )
{
System.out.println(e.toString());
}
return "error";
}
public String decrypt(String ciphertext)
{
try
{
// We are now going to perform the decryption. Note that since this is a
// symmetric algorithm we want to use the same key as before.
TripleDESDecryptorEngine decryptorEngine = new TripleDESDecryptorEngine(_key);
// Create the unformatter engine that will remove any of the padding bytes.
PKCS5UnformatterEngine unformatterEngine = new PKCS5UnformatterEngine( decryptorEngine );
// Set up an input stream to hand the encrypted data to the block decryptor.
ByteArrayInputStream inputStream = new ByteArrayInputStream( ciphertext.getBytes() );
// Create the block decryptor passing in the unformatter engine and the
// encrypted data.
BlockDecryptor decryptor = new BlockDecryptor( unformatterEngine, inputStream );
// Now we want to read from the stream. We are going to read the data 10 bytes
// at a time and then add that new data to the decryptedData array. It is
// important to note that for efficiency one would most likely want to use a
// larger value than 10. We use a small value so that we can demonstrate
// several iterations through the loop.
byte[] temp = new byte[10];
DataBuffer db = new DataBuffer();
for( ;; )
{
int bytesRead = decryptor.read( temp );
if( bytesRead <= 0 )
{
// We have run out of information to read, bail out of loop.
break;
}
db.write(temp, 0, bytesRead);
}
// Now we want to ensure that the decrypted data is the same as the data we
// passed into the encryptor.
byte[] decryptedData = db.toArray();
String strDecrypted = new String(decryptedData);
return(strDecrypted);
}
catch( CryptoTokenException e )
{
System.out.println(e.toString());
}
catch (CryptoUnsupportedOperationException e)
{
System.out.println(e.toString());
}
catch( IOException e )
{
System.out.println(e.toString());
}
return "error";
}
}
|
The following table presents the components of the Crypto API. It includes links to more reference information about each of the component APIs.