Cryptography Reference
In-Depth Information
//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[] pohligHellmanDecipherWSalt(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));
ba[i]=removeSalt(ba[i]);
}
//Go from blocks to a 1D array, and remove padding; return this
return unPad(unBlock(ba,blockSize-5),blockSize-5);
}
You can see that these methods call a couple of helper methods, addSalt() and
removeSalt(), also in the Ciphers class.
//Method to add salt to blocks
private static byte[] addSalt(byte[] b,SecureRandom random) {
byte[] answer=new byte[b.length+4];
byte[] salt=new byte[4];
random.nextBytes(salt);
//Put salt in front
System.arraycopy(salt,0,answer,0,4);
//Copy the message over
System.arraycopy(b,0,answer,4,b.length);
return answer;
}
//Method to remove salt
private static byte[] removeSalt(byte[] b) {
byte[] answer=new byte[b.length-4];
//Copy the message over
System.arraycopy(b,4,answer,0,answer.length);
return answer;
}
Search WWH ::




Custom Search