Cryptography Reference
In-Depth Information
for (int i=0;i<ba.length;i++) ba[i]=getBytes(new
BigInteger(1,ba[i]).modPow(e,p));
//Return to a 1D array.
//The ciphertext block size is one byte greater than plaintext block size.
return unBlock(ba,blockSize+1);
}
public static byte[] pohligHellmanDecipher(byte[] msg,BigInteger d,BigInteger p)
{
//Compute the ciphertext block size
int blockSize=(p.bitLength()-1)/8+1;
//Check the deciphering exponent
if (!(p.subtract(BigIntegerMath.ONE).gcd(d).equals(BigIntegerMath.ONE)))
throw new IllegalArgumentException
(“Deciphering key is not relatively prime to (modulus minus one).”);
byte[][] ba=block(msg,blockSize);
//Begin the deciphering
for (int i=0;i<ba.length;i++) ba[i]=getBytes(new
BigInteger(1,ba[i]).modPow(d,p));
//Go from blocks to a 1D array, and remove padding; return this
return unPad(unBlock(ba,blockSize-1),blockSize-1);
}
//…Other methods
}
An applet (called TestPohligHellmanCipherApplet) to view the behavior of this cipher
can be run from the topic's website. The applet generates a safe prime to use as the modu-
lus. The applet actually uses a salted version of Pohlig-Hellman. You will see that if you enci-
pher the same plaintext multiple times, you will receive a different ciphertext each time. The
methods to encipher/decipher this way are in the Ciphers class.
public static byte[] pohligHellmanEncipherWSalt
(byte[] msg,BigInteger e,BigInteger p,SecureRandom sr) {
//Compute the plaintext block size
int blockSize=(p.bitLength()-1)/8;
if (blockSize<5) throw new IllegalArgumentException
(“Block size must be >= 5 bytes”);
//Check the enciphering exponent
if (!(p.subtract(BigIntegerMath.ONE).gcd(e).equals(BigIntegerMath.ONE)))
throw new IllegalArgumentException
(“Enciphering key is not relatively prime to (modulus minus one).”);
byte[][] ba=block(pad(msg,blockSize-4),blockSize-4);
//Begin the enciphering
for (int i=0;i<ba.length;i++) {
ba[i]=addSalt(ba[i],sr);
ba[i]=getBytes(new BigInteger(1,ba[i]).modPow(e,p));
}
Search WWH ::




Custom Search