if (!forEncryption)
{
// At decryption make sure that we receive padded data blocks
if (len < getInputBlockSize())
{
throw new InvalidCipherTextException("BlockLength does not match modulus for Naccache-Stern cipher.\n");
}
}
byte[] block;
if (inOff != 0 || len != in.length)
{
block = new byte[len];
System.arraycopy(in, inOff, block, 0, len);
}
else
{
block = in;
}
// transform input into BigInteger
BigInteger input = new BigInteger(1, block);
if (debug)
{
System.out.println("input as BigInteger: " + input);
}
byte[] output;
if (forEncryption)
{
output = encrypt(input);
}
else
{
Vector plain = new Vector();
NaccacheSternPrivateKeyParameters priv = (NaccacheSternPrivateKeyParameters)key;
Vector primes = priv.getSmallPrimes();
// Get Chinese Remainders of CipherText
for (int i = 0; i < primes.size(); i++)
{
BigInteger exp = input.modPow(priv.getPhi_n().divide((BigInteger)primes.elementAt(i)), priv.getModulus());
Vector al = lookup[i];
if (lookup[i].size() != ((BigInteger)primes.elementAt(i)).intValue())
{
if (debug)
{
System.out.println("Prime is " + primes.elementAt(i) + ", lookup table has size " + al.size());
}
throw new InvalidCipherTextException("Error in lookup Array for "
+ ((BigInteger)primes.elementAt(i)).intValue()
+ ": Size mismatch. Expected ArrayList with length "
+ ((BigInteger)primes.elementAt(i)).intValue() + " but found ArrayList of length "
+ lookup[i].size());
}
int lookedup = al.indexOf(exp);
if (lookedup == -1)
{
if (debug)
{
System.out.println("Actual prime is " + primes.elementAt(i));
System.out.println("Decrypted value is " + exp);
System.out.println("LookupList for " + primes.elementAt(i) + " with size " + lookup[i].size()
+ " is: ");
for (int j = 0; j < lookup[i].size(); j++)
{
System.out.println(lookup[i].elementAt(j));
}
}
throw new InvalidCipherTextException("Lookup failed");
}
plain.addElement(BigInteger.valueOf(lookedup));
}
BigInteger test = chineseRemainder(plain, primes);