hcrypto.cipher
Class Cipher

java.lang.Object
  |
  +--hcrypto.cipher.Cipher

public class Cipher
extends java.lang.Object

This class provides the functionality for a wide range of historical ciphers that can be used for encryption and decryption.

To create a Cipher object, the application must call Cipher.getInstance() passing it a String that gives the name of the cipher algorithm. For example, here is how one would create a Caesar cipher.

      Cipher cipher = Cipher.getInstance("Caesar");
      HistoricalKey key = HistoricalKey.getInstance("Caesar", cipher.getProvider());
      key.init("55/printable");
      cipher.init(key);
      String c1encrypt = cipher.encrypt(secret);
 
The Cipher.getInstance() method searches a list of providers for an implementation of Caesar cipher and will throw a "No such algorithm" exception if none is found. Details on creating an algorithm-dependent key for the cipher are described in the documentation of HistoricalKey class.

See also:


CipherEngine
HistoricalKey
Provider


Field Summary
protected  java.lang.String algorithm
          the name of this algorithm
protected  CipherEngine engine
          provides a reference to the class that implements the cipher algorithm
protected  java.lang.String provider
          the name of the provider of this algorithm
 
Constructor Summary
protected Cipher(CipherEngine engine)
          the constructor is only used internally to create a Cipher object.
 
Method Summary
 java.lang.String decrypt(java.lang.String s)
          decrypts its String parameter returning the plaintext string.
 void decryptFile(java.lang.String inFileName, java.lang.String outFileName)
          decrypts a ciphertext input file into an output file by simply calling the engineDecryptFile() method implemented in the ENGINE.
 void decryptFile(java.lang.String inFileName, java.lang.String outFileName, java.lang.String encoding)
          decrypts a ciphertext input file into an output file by simply calling the engineDecryptFile() method implemented in the ENGINE.
 java.lang.String encrypt(java.lang.String s)
          encrypts its String parameter returning the encrypted string.
 void encryptFile(java.lang.String inFileName, java.lang.String outFileName)
          encrypts a plaintext input file into an output file by simply calling the engineEncryptFile() method implemented in the ENGINE.
 void encryptFile(java.lang.String inFileName, java.lang.String outFileName, java.lang.String encoding)
          encrypts a plaintext input file into an output file by simply calling the engineEncryptFile() method implemented in the ENGINE.
 java.lang.String getAlgorithm()
          returns a string giving the name of the cipher algorithm.
 java.lang.String getAlphabetRangeOptions()
          returns a string giving the alphabet range options for a particular cipher.
static Cipher getInstance(java.lang.String algorithm)
          searches for a provider that provides an implementation of the algorithm named in its parameter, throwing a "no such algorithm" exception if none is found.
static Cipher getInstance(java.lang.String algorithm, java.lang.String provider)
          searches for the provider named in the second parameter for an implementation of the algorithm named in its first parameter.
 java.lang.String getProvider()
          returns a string giving the name of the provider that implements the cipher algorithm.
 void init(HistoricalKey key)
          initializes the particular encrypting engine by calling the encapsulated CipherEngine and passing it a key of the appropriate type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

engine

protected CipherEngine engine
provides a reference to the class that implements the cipher algorithm

provider

protected java.lang.String provider
the name of the provider of this algorithm

algorithm

protected java.lang.String algorithm
the name of this algorithm
Constructor Detail

Cipher

protected Cipher(CipherEngine engine)
the constructor is only used internally to create a Cipher object. It is passed an instance of the engine that provides an implementation of the desired cipher algorithm. Applications should use one of the getInstance() factory methods to create a Cipher.
Method Detail

getInstance

public static final Cipher getInstance(java.lang.String algorithm,
                                       java.lang.String provider)
searches for the provider named in the second parameter for an implementation of the algorithm named in its first parameter.
Parameters:
algorithm, - a String giving the algorithm name
provider, - a String giving the provider name

getInstance

public static final Cipher getInstance(java.lang.String algorithm)
searches for a provider that provides an implementation of the algorithm named in its parameter, throwing a "no such algorithm" exception if none is found.
Parameters:
algorithm, - a String giving the algorithm name

init

public final void init(HistoricalKey key)
                throws java.lang.Exception
initializes the particular encrypting engine by calling the encapsulated CipherEngine and passing it a key of the appropriate type.
Parameters:
key, - an instance of a HistoricalKey

encryptFile

public final void encryptFile(java.lang.String inFileName,
                              java.lang.String outFileName)
                       throws java.lang.Exception
encrypts a plaintext input file into an output file by simply calling the engineEncryptFile() method implemented in the ENGINE.
Parameters:
inFile, - the name of the input plaintext file.
outFile, - the name of the output encrypted file.

encryptFile

public final void encryptFile(java.lang.String inFileName,
                              java.lang.String outFileName,
                              java.lang.String encoding)
                       throws java.lang.Exception
encrypts a plaintext input file into an output file by simply calling the engineEncryptFile() method implemented in the ENGINE. Use this version if you want to encrypt a file that is NOT encoded with locale-dependent character encoding scheme.
Parameters:
inFile, - the name of the input plaintext file.
outFile, - the name of the output encrypted file.
encoding, - the name of the encoding scheme -- "ISO-2022-JP"

decryptFile

public final void decryptFile(java.lang.String inFileName,
                              java.lang.String outFileName)
                       throws java.lang.Exception
decrypts a ciphertext input file into an output file by simply calling the engineDecryptFile() method implemented in the ENGINE.
Parameters:
inFile, - the name of the input ciphertext file.
outFile, - the name of the output plaintext file.

decryptFile

public final void decryptFile(java.lang.String inFileName,
                              java.lang.String outFileName,
                              java.lang.String encoding)
                       throws java.lang.Exception
decrypts a ciphertext input file into an output file by simply calling the engineDecryptFile() method implemented in the ENGINE. Use this version if you want to decrypt a file that is NOT encoded with locale-dependent character encoding scheme.
Parameters:
inFile, - the name of the input ciphertext file.
outFile, - the name of the output plaintext file.
encoding, - the name of the encoding scheme -- e.g., "ISO-2022-JP"

encrypt

public java.lang.String encrypt(java.lang.String s)
                         throws java.lang.Exception
encrypts its String parameter returning the encrypted string. It simply calls the engineEncrypt() method implemented in the ENGINE.
Parameters:
s, - a String to be encrypted

decrypt

public final java.lang.String decrypt(java.lang.String s)
                               throws java.lang.Exception
decrypts its String parameter returning the plaintext string. It simply calls the engineDecrypt() method implemented in the ENGINE.
Parameters:
s, - a String to be decrypted

getAlgorithm

public final java.lang.String getAlgorithm()
returns a string giving the name of the cipher algorithm.

getProvider

public final java.lang.String getProvider()
returns a string giving the name of the provider that implements the cipher algorithm.

getAlphabetRangeOptions

public final java.lang.String getAlphabetRangeOptions()
returns a string giving the alphabet range options for a particular cipher. The alphabet rangeOptions string should be set in the CipherEngine constructor.