Ciphers
Classes that implement the
Cipher
interface
(jxta.security.cipher.Cipher
) are used to encrypt
or decrypt data. Cipher objects are obtained via the
getJxtaCipher( )
method of the
JxtaCrypto
interface.
Before it can be used, a cipher must be
initialized with an appropriate key and a mode (either
Cipher.MODE_DECRYPT
or
Cipher.MODE_ENCRYPT
). Bytes to be encrypted or
decrypted are then fed to the cipher’s
update( )
method; the last block of data to be fed
to the cipher is passed to the cipher’s
doFinal( )
method.
An RC4 Cipher Example
Here’s an example that encrypts and decrypts a simple string using the RC4 cipher:
import jxta.security.cipher.Cipher; import jxta.security.crypto.JxtaCrypto; import jxta.security.impl.cipher.RC4Cipher; import jxta.security.impl.cipher.KeyBuilder; import jxta.security.impl.cipher.SecretKey; import jxta.security.impl.random.JRandom; import jxta.security.impl.crypto.JxtaCryptoSuite; public class TestRC4Cipher { public static void main(String[] args) throws Exception { // Step 1: Generate the JxtaCryptoSuite that does only // RC4 encryption (so all other arguments are not used) JxtaCrypto jc = new JxtaCryptoSuite(JxtaCrypto.MEMBER_RC4, null, (byte) 0, (byte) 0); // Step 2: Generate the necessary RC4 key SecretKey secretKey = (SecretKey) KeyBuilder.buildKey( KeyBuilder.TYPE_RC4, KeyBuilder.LENGTH_RC4, false); JRandom random = new JRandom( ); byte[] keydata = new byte[KeyBuilder.LENGTH_RC4 >>> 3]; random.nextBytes(keydata); secretKey.setKey(keydata, ...
Get JXTA in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.