提交 0e6a4ca6 编写于 作者: A akosarev

8157603: TestCipher.java doesn't check one of the decrypted message as expected

Reviewed-by: valeriep
上级 9f0a804e
/* /*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -150,11 +150,6 @@ public abstract class TestCipher { ...@@ -150,11 +150,6 @@ public abstract class TestCipher {
} }
// Encryption // Encryption
int PAD_LEN = 0;
if (pad.equalsIgnoreCase("PKCS5Padding")) {
// Need to consider pad bytes
PAD_LEN = 8;
}
byte[] plainText = INPUT_TEXT.clone(); byte[] plainText = INPUT_TEXT.clone();
...@@ -162,12 +157,13 @@ public abstract class TestCipher { ...@@ -162,12 +157,13 @@ public abstract class TestCipher {
byte[] cipherText = ci.doFinal(INPUT_TEXT, ENC_OFFSET, TEXT_LEN); byte[] cipherText = ci.doFinal(INPUT_TEXT, ENC_OFFSET, TEXT_LEN);
// Generate cipher and save to same buffer // Generate cipher and save to same buffer
int offset = ci.update( int enc_bytes = ci.update(
INPUT_TEXT, ENC_OFFSET, TEXT_LEN, INPUT_TEXT, STORAGE_OFFSET); INPUT_TEXT, ENC_OFFSET, TEXT_LEN, INPUT_TEXT, STORAGE_OFFSET);
ci.doFinal(INPUT_TEXT, offset + STORAGE_OFFSET); enc_bytes += ci.doFinal(INPUT_TEXT, enc_bytes + STORAGE_OFFSET);
if (!equalsBlock( if (!equalsBlock(
INPUT_TEXT, STORAGE_OFFSET, cipherText, 0, cipherText.length)) { INPUT_TEXT, STORAGE_OFFSET, enc_bytes,
cipherText, 0, cipherText.length)) {
throw new RuntimeException( throw new RuntimeException(
"Different ciphers generated with same buffer"); "Different ciphers generated with same buffer");
} }
...@@ -183,8 +179,8 @@ public abstract class TestCipher { ...@@ -183,8 +179,8 @@ public abstract class TestCipher {
byte[] recoveredText = ci.doFinal(cipherText, 0, cipherText.length); byte[] recoveredText = ci.doFinal(cipherText, 0, cipherText.length);
if (!equalsBlock( if (!equalsBlock(
plainText, ENC_OFFSET, recoveredText, 0, plainText, ENC_OFFSET, TEXT_LEN,
recoveredText.length)) { recoveredText, 0, recoveredText.length)) {
throw new RuntimeException( throw new RuntimeException(
"Recovered text not same as plain text"); "Recovered text not same as plain text");
} else { } else {
...@@ -192,13 +188,13 @@ public abstract class TestCipher { ...@@ -192,13 +188,13 @@ public abstract class TestCipher {
} }
// Recover text from cipher and save to same buffer // Recover text from cipher and save to same buffer
ci.update(INPUT_TEXT, STORAGE_OFFSET, TEXT_LEN + PAD_LEN, INPUT_TEXT, int dec_bytes = ci.update(
ENC_OFFSET); INPUT_TEXT, STORAGE_OFFSET, enc_bytes, INPUT_TEXT, ENC_OFFSET);
ci.doFinal(INPUT_TEXT, ENC_OFFSET); dec_bytes += ci.doFinal(INPUT_TEXT, dec_bytes + ENC_OFFSET);
if (!equalsBlock( if (!equalsBlock(
plainText, ENC_OFFSET, recoveredText, 0, plainText, ENC_OFFSET, TEXT_LEN,
recoveredText.length)) { INPUT_TEXT, ENC_OFFSET, dec_bytes)) {
throw new RuntimeException( throw new RuntimeException(
"Recovered text not same as plain text with same buffer"); "Recovered text not same as plain text with same buffer");
} else { } else {
...@@ -208,9 +204,12 @@ public abstract class TestCipher { ...@@ -208,9 +204,12 @@ public abstract class TestCipher {
out.println("Test Passed."); out.println("Test Passed.");
} }
private static boolean equalsBlock(byte[] b1, int off1, byte[] b2, int off2, private static boolean equalsBlock(byte[] b1, int off1, int len1,
int len) { byte[] b2, int off2, int len2) {
for (int i = off1, j = off2, k = 0; k < len; i++, j++, k++) { if (len1 != len2) {
return false;
}
for (int i = off1, j = off2, k = 0; k < len1; i++, j++, k++) {
if (b1[i] != b2[j]) { if (b1[i] != b2[j]) {
return false; return false;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册