org.bouncycastle.crypto.engines
Class NaccacheSternEngine

java.lang.Object
  extended by org.bouncycastle.crypto.engines.NaccacheSternEngine
All Implemented Interfaces:
AsymmetricBlockCipher

public class NaccacheSternEngine
extends java.lang.Object
implements AsymmetricBlockCipher

NaccacheStern Engine. For details on this cipher, please see http://www.gemplus.com/smart/rd/publications/pdf/NS98pkcs.pdf


Field Summary
private  boolean debug
           
private  boolean forEncryption
           
private  NaccacheSternKeyParameters key
           
private  java.util.Vector[] lookup
           
private static java.math.BigInteger ONE
           
private static java.math.BigInteger ZERO
           
 
Constructor Summary
NaccacheSternEngine()
           
 
Method Summary
 byte[] addCryptedBlocks(byte[] block1, byte[] block2)
          Adds the contents of two encrypted blocks mod sigma
private static java.math.BigInteger chineseRemainder(java.util.Vector congruences, java.util.Vector primes)
          Computes the integer x that is expressed through the given primes and the congruences with the chinese remainder theorem (CRT).
 byte[] encrypt(java.math.BigInteger plain)
          Encrypts a BigInteger aka Plaintext with the public key.
 int getInputBlockSize()
          Returns the input block size of this algorithm.
 int getOutputBlockSize()
          Returns the output block size of this algorithm.
 void init(boolean forEncryption, CipherParameters param)
          Initializes this algorithm.
 byte[] processBlock(byte[] in, int inOff, int len)
          Process a single Block using the Naccache-Stern algorithm.
 byte[] processData(byte[] data)
          Convenience Method for data exchange with the cipher.
 void setDebug(boolean debug)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

forEncryption

private boolean forEncryption

key

private NaccacheSternKeyParameters key

lookup

private java.util.Vector[] lookup

debug

private boolean debug

ZERO

private static java.math.BigInteger ZERO

ONE

private static java.math.BigInteger ONE
Constructor Detail

NaccacheSternEngine

public NaccacheSternEngine()
Method Detail

init

public void init(boolean forEncryption,
                 CipherParameters param)
Initializes this algorithm. Must be called before all other Functions.

Specified by:
init in interface AsymmetricBlockCipher
Parameters:
forEncryption - if true the cipher is initialised for encryption, if false for decryption.
param - the key and other data required by the cipher.
See Also:
AsymmetricBlockCipher.init(boolean, org.bouncycastle.crypto.CipherParameters)

setDebug

public void setDebug(boolean debug)

getInputBlockSize

public int getInputBlockSize()
Returns the input block size of this algorithm.

Specified by:
getInputBlockSize in interface AsymmetricBlockCipher
Returns:
maximum size for an input block.
See Also:
AsymmetricBlockCipher.getInputBlockSize()

getOutputBlockSize

public int getOutputBlockSize()
Returns the output block size of this algorithm.

Specified by:
getOutputBlockSize in interface AsymmetricBlockCipher
Returns:
maximum size of the output block produced by the cipher.
See Also:
AsymmetricBlockCipher.getOutputBlockSize()

processBlock

public byte[] processBlock(byte[] in,
                           int inOff,
                           int len)
                    throws InvalidCipherTextException
Process a single Block using the Naccache-Stern algorithm.

Specified by:
processBlock in interface AsymmetricBlockCipher
Parameters:
in - the input data
inOff - offset into the in array where the data starts
len - the length of the block to be processed.
Returns:
the resulting byte array of the encryption/decryption process.
Throws:
InvalidCipherTextException - data decrypts improperly.
See Also:
AsymmetricBlockCipher.processBlock(byte[], int, int)

encrypt

public byte[] encrypt(java.math.BigInteger plain)
Encrypts a BigInteger aka Plaintext with the public key.

Parameters:
plain - The BigInteger to encrypt
Returns:
The byte[] representation of the encrypted BigInteger (i.e. crypted.toByteArray())

addCryptedBlocks

public byte[] addCryptedBlocks(byte[] block1,
                               byte[] block2)
                        throws InvalidCipherTextException
Adds the contents of two encrypted blocks mod sigma

Parameters:
block1 - the first encrypted block
block2 - the second encrypted block
Returns:
encrypt((block1 + block2) mod sigma)
Throws:
InvalidCipherTextException

processData

public byte[] processData(byte[] data)
                   throws InvalidCipherTextException
Convenience Method for data exchange with the cipher. Determines blocksize and splits data to blocksize.

Parameters:
data - the data to be processed
Returns:
the data after it went through the NaccacheSternEngine.
Throws:
InvalidCipherTextException

chineseRemainder

private static java.math.BigInteger chineseRemainder(java.util.Vector congruences,
                                                     java.util.Vector primes)
Computes the integer x that is expressed through the given primes and the congruences with the chinese remainder theorem (CRT).

Parameters:
congruences - the congruences c_i
primes - the primes p_i
Returns:
an integer x for that x % p_i == c_i