我正在編寫一個加密程序,並希望將多個密碼塊和流模式與哈希機制一起使用。我沒有任何使用OFB等流模式對消息進行加密,解密和驗證的問題,但是當他們使用填充時,我在使用blockcipher moder解密和驗證消息時遇到了問題。在填充和塊密碼模式下使用哈希
例如我使用ECB(我知道它不是很好)與PKCS7Padding和SHA-256。在我解密消息後,它最後有一些字符。除此之外,我收到消息,散列摘要不等於原始摘要。
這個問題不會發生,當我不使用填充。
這裏是我的代碼:
@Override
public byte[] encrypt(byte[] input) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/" + getPadding(), "BC");
cipher.init(Cipher.ENCRYPT_MODE, getKey());
byte[] output = getBytesForCipher(cipher, input);
int ctLength = cipher.update(input, 0, input.length, output, 0);
updateHash(input);
cipher.doFinal(getDigest(), 0, getDigest().length, output, ctLength);
return output;
}
protected byte[] getBytesForCipher(Cipher cipher, byte[] input) {
return new byte[cipher.getOutputSize(input.length + hash.getDigestLength())];
}
protected void updateHash(byte[] input) {
hash.update(input);
}
public byte[] decrypt(byte[] input) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/" + getPadding(), "BC");
cipher.init(Cipher.DECRYPT_MODE, getKey());
byte[] output = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, output, 0);
cipher.doFinal(output, ctLength);
return removeHash(output);
}
protected byte[] removeHash(byte[] output) {
int messageLength = output.length - hash.getDigestLength();
hash.update(output, 0, output.length - hash.getDigestLength());;
byte[] realOutput = new byte[messageLength];
System.arraycopy(output, 0, realOutput, 0, messageLength);
messageValid = isValid(output);
return realOutput;
}
private boolean isValid(byte[] output) {
int messageLength = output.length - hash.getDigestLength();
byte[] messageHash = new byte[hash.getDigestLength()];
System.arraycopy(output, messageLength, messageHash, 0, messageHash.length);
return MessageDigest.isEqual(hash.digest(), messageHash);
}
我使用BouncyCastle的供應商。
*爲什麼*您是否在使用BC提供程序來實現這種功能?你不喜歡硬件加速?這是應該在默認提供程序上執行的基本功能。 –