Cryptography Reference
In-Depth Information
int blockSize=(n.bitLength()-1)/8+1;
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,n));
//Go from blocks to a 1D array, and remove padding; return this
return unPad(unBlock(ba,blockSize-1),blockSize-1);
}
The methods to encipher/decipher with salt are also in the Ciphers class.
public static byte[] RSAEncipherWSalt
(byte[] msg,BigInteger e,BigInteger n,SecureRandom sr) {
//Compute the plaintext block size
int blockSize=(n.bitLength()-1)/8;
if (blockSize<5) throw new IllegalArgumentException
(“Block size must be >= 5 bytes”);
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,n));
}
//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[] RSADecipherWSalt(byte[] msg,BigInteger d,BigInteger n) {
//Compute the ciphertext block size
int blockSize=(n.bitLength()-1)/8+1;
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,n));
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);
}
TestRSACipherApplet is on the topic's website to test the RSA methods. The applet actu-
ally uses a salted version of RSA. You will see that if you encipher the same plaintext mul-
tiple times, you will receive a different ciphertext each time. (See Figure 14.3.)
Search WWH ::




Custom Search