Java Reference
In-Depth Information
• The message-digest algorithms don't throw exceptions to signal errors.
• The Digest interface provides a method that tells you how many bytes long the
digest will be.
In addition to the handy getDigestSize method provided by the Digest interface,
you can also get a human-readable name of a message-digest algorithm by invoking
the Digest method getAlgorithmName , which you could present to the user via your appli-
cation's UI. Most of the message-digest algorithms the Bouncy Castle API provides
actually implement the ExtendedDigest interface, which implements Digest and adds the
getByteLength method. You can invoke getByteLength to learn the size of the internal
buffer the digest applies its algorithm to.
Encrypting and Decrypting Using the Bouncy Castle API
The Bouncy Castle API provides cipher implementations through cryptographic engines
(in org.bouncycastle.crypto. engines) that implement specific interfaces such as
AsymmetricBlockCipher , BlockCipher , or StreamCipher (all of which you can find in the
org.bouncycastle.crypto package). These interfaces all serve a common purpose: they
let you initialize the cipher, provide data in the form of byte arrays to be encrypted or
decrypted (as either blocks or part of a message stream), and then perform the encryp-
tion or decryption operation. As with the JCA and SATSA-CRYPTO API, when you
initialize a cipher, you indicate whether you want the implementation to perform
encryption or decryption, as well as the details of the key for the operation. Using the
Bouncy Castle API to perform an encryption with RC4, you might write the code shown
in Listing 15-6.
Listing 15-6. Using the Bouncy Castle API to Perform RC4 Encryption
byte[] secretKey = { … };
String plainText = "Here there be treasure";
byte[] plainTextBytes = plainText.getBytes();
org.bouncycastle.crypto.StreamCipher cipher =
new org.bouncycastle.crypto.engines.RC4Engine();
cipher.init( true,
new org.bouncycastle.crypto.params.KeyParameter(secretKey));
byte[] cipherTextBytes = new byte[plainTextBytes.length];
try {
cipher.processBytes( plainTextBytes, 0,
plainTextBytes.length,
cipherTextBytes, 0 );
} catch( Exception e ) {…}
 
Search WWH ::




Custom Search