[New] Handling illegal key size in AesCryptoHelperTest
This commit is contained in:
parent
46c3db2913
commit
1de56d0043
|
@ -9,11 +9,16 @@ import java.io.File;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class AesCryptoHelperTest {
|
public class AesCryptoHelperTest {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(AesCryptoHelperTest.class);
|
||||||
|
|
||||||
private static final char[] password = "A2589309-17AE-4819-B9E4-E577CFA7778F".toCharArray();
|
private static final char[] password = "A2589309-17AE-4819-B9E4-E577CFA7778F".toCharArray();
|
||||||
private static final byte[] salt;
|
private static final byte[] salt;
|
||||||
|
|
||||||
|
@ -28,92 +33,146 @@ public class AesCryptoHelperTest {
|
||||||
@Test
|
@Test
|
||||||
public void shouldWrapStreams() throws Exception {
|
public void shouldWrapStreams() throws Exception {
|
||||||
|
|
||||||
byte[] clearTextBytes = "Some text".getBytes();
|
try {
|
||||||
|
byte[] clearTextBytes = "Some text".getBytes();
|
||||||
|
|
||||||
// encrypt data
|
// encrypt data
|
||||||
ByteArrayOutputStream encryptedOut = new ByteArrayOutputStream();
|
ByteArrayOutputStream encryptedOut = new ByteArrayOutputStream();
|
||||||
OutputStream outputStream = AesCryptoHelper.wrapEncrypt(password, salt, encryptedOut);
|
OutputStream outputStream = AesCryptoHelper.wrapEncrypt(password, salt, encryptedOut);
|
||||||
outputStream.write(clearTextBytes);
|
outputStream.write(clearTextBytes);
|
||||||
outputStream.flush();
|
outputStream.flush();
|
||||||
outputStream.close();
|
outputStream.close();
|
||||||
|
|
||||||
// decrypt data
|
// decrypt data
|
||||||
byte[] encryptedBytes = encryptedOut.toByteArray();
|
byte[] encryptedBytes = encryptedOut.toByteArray();
|
||||||
ByteArrayInputStream encryptedIn = new ByteArrayInputStream(encryptedBytes);
|
ByteArrayInputStream encryptedIn = new ByteArrayInputStream(encryptedBytes);
|
||||||
InputStream inputStream = AesCryptoHelper.wrapDecrypt(password, salt, encryptedIn);
|
InputStream inputStream = AesCryptoHelper.wrapDecrypt(password, salt, encryptedIn);
|
||||||
|
|
||||||
ByteArrayOutputStream decryptedOut = new ByteArrayOutputStream();
|
ByteArrayOutputStream decryptedOut = new ByteArrayOutputStream();
|
||||||
byte[] readBuffer = new byte[64];
|
byte[] readBuffer = new byte[64];
|
||||||
int read = 0;
|
int read = 0;
|
||||||
while ((read = inputStream.read(readBuffer)) != -1) {
|
while ((read = inputStream.read(readBuffer)) != -1) {
|
||||||
decryptedOut.write(readBuffer, 0, read);
|
decryptedOut.write(readBuffer, 0, read);
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] decryptedBytes = decryptedOut.toByteArray();
|
||||||
|
|
||||||
|
assertArrayEquals(clearTextBytes, decryptedBytes);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
if (e.getCause() instanceof InvalidKeyException
|
||||||
|
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
|
||||||
|
logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!");
|
||||||
|
else
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] decryptedBytes = decryptedOut.toByteArray();
|
|
||||||
|
|
||||||
assertArrayEquals(clearTextBytes, decryptedBytes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldEncryptBytes() {
|
public void shouldEncryptBytes() {
|
||||||
|
try {
|
||||||
|
|
||||||
byte[] clearTextBytes = "Some text".getBytes();
|
byte[] clearTextBytes = "Some text".getBytes();
|
||||||
|
|
||||||
byte[] encryptedBytes = AesCryptoHelper.encrypt(password, salt, clearTextBytes);
|
byte[] encryptedBytes = AesCryptoHelper.encrypt(password, salt, clearTextBytes);
|
||||||
byte[] decryptedBytes = AesCryptoHelper.decrypt(password, salt, encryptedBytes);
|
byte[] decryptedBytes = AesCryptoHelper.decrypt(password, salt, encryptedBytes);
|
||||||
|
|
||||||
assertArrayEquals(clearTextBytes, decryptedBytes);
|
assertArrayEquals(clearTextBytes, decryptedBytes);
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
if (e.getCause() instanceof InvalidKeyException
|
||||||
|
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
|
||||||
|
logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!");
|
||||||
|
else
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldEncryptShortFile() {
|
public void shouldEncryptShortFile() {
|
||||||
// file to be encrypted
|
try {
|
||||||
String clearTextFileS = "src/test/resources/crypto_test_short.txt";
|
|
||||||
// encrypted file
|
|
||||||
String encryptedFileS = "target/encrypted_short.aes";
|
|
||||||
// encrypted file
|
|
||||||
String decryptedFileS = "target/decrypted_short.txt";
|
|
||||||
|
|
||||||
testCrypto(clearTextFileS, encryptedFileS, decryptedFileS);
|
// file to be encrypted
|
||||||
|
String clearTextFileS = "src/test/resources/crypto_test_short.txt";
|
||||||
|
// encrypted file
|
||||||
|
String encryptedFileS = "target/encrypted_short.aes";
|
||||||
|
// encrypted file
|
||||||
|
String decryptedFileS = "target/decrypted_short.txt";
|
||||||
|
|
||||||
|
testCrypto(clearTextFileS, encryptedFileS, decryptedFileS);
|
||||||
|
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
if (e.getCause() instanceof InvalidKeyException
|
||||||
|
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
|
||||||
|
logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!");
|
||||||
|
else
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldEncryptMiddleFile() {
|
public void shouldEncryptMiddleFile() {
|
||||||
|
try {
|
||||||
|
|
||||||
// file to be encrypted
|
// file to be encrypted
|
||||||
String clearTextFileS = "src/test/resources/crypto_test_middle.txt";
|
String clearTextFileS = "src/test/resources/crypto_test_middle.txt";
|
||||||
// encrypted file
|
// encrypted file
|
||||||
String encryptedFileS = "target/encrypted_middle.aes";
|
String encryptedFileS = "target/encrypted_middle.aes";
|
||||||
// encrypted file
|
// encrypted file
|
||||||
String decryptedFileS = "target/decrypted_middle.txt";
|
String decryptedFileS = "target/decrypted_middle.txt";
|
||||||
|
|
||||||
testCrypto(clearTextFileS, encryptedFileS, decryptedFileS);
|
testCrypto(clearTextFileS, encryptedFileS, decryptedFileS);
|
||||||
|
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
if (e.getCause() instanceof InvalidKeyException
|
||||||
|
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
|
||||||
|
logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!");
|
||||||
|
else
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldEncryptLongFile() {
|
public void shouldEncryptLongFile() {
|
||||||
|
try {
|
||||||
|
|
||||||
// file to be encrypted
|
// file to be encrypted
|
||||||
String clearTextFileS = "src/test/resources/crypto_test_long.txt";
|
String clearTextFileS = "src/test/resources/crypto_test_long.txt";
|
||||||
// encrypted file
|
// encrypted file
|
||||||
String encryptedFileS = "target/encrypted_long.aes";
|
String encryptedFileS = "target/encrypted_long.aes";
|
||||||
// encrypted file
|
// encrypted file
|
||||||
String decryptedFileS = "target/decrypted_long.txt";
|
String decryptedFileS = "target/decrypted_long.txt";
|
||||||
|
|
||||||
testCrypto(clearTextFileS, encryptedFileS, decryptedFileS);
|
testCrypto(clearTextFileS, encryptedFileS, decryptedFileS);
|
||||||
|
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
if (e.getCause() instanceof InvalidKeyException
|
||||||
|
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
|
||||||
|
logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!");
|
||||||
|
else
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldEncryptBinaryFile() {
|
public void shouldEncryptBinaryFile() {
|
||||||
|
|
||||||
// file to be encrypted
|
try {
|
||||||
String clearTextFileS = "src/test/resources/crypto_test_image.ico";
|
|
||||||
// encrypted file
|
|
||||||
String encryptedFileS = "target/encrypted_image.aes";
|
|
||||||
// encrypted file
|
|
||||||
String decryptedFileS = "target/decrypted_image.ico";
|
|
||||||
|
|
||||||
testCrypto(clearTextFileS, encryptedFileS, decryptedFileS);
|
// file to be encrypted
|
||||||
|
String clearTextFileS = "src/test/resources/crypto_test_image.ico";
|
||||||
|
// encrypted file
|
||||||
|
String encryptedFileS = "target/encrypted_image.aes";
|
||||||
|
// encrypted file
|
||||||
|
String decryptedFileS = "target/decrypted_image.ico";
|
||||||
|
|
||||||
|
testCrypto(clearTextFileS, encryptedFileS, decryptedFileS);
|
||||||
|
|
||||||
|
} catch (RuntimeException e) {
|
||||||
|
if (e.getCause() instanceof InvalidKeyException
|
||||||
|
&& e.getCause().getMessage().equals("Illegal key size or default parameters"))
|
||||||
|
logger.warn("YOU ARE MISSING THE UNLIMITED JCE POLICIES and can not do AES encryption!");
|
||||||
|
else
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void testCrypto(String clearTextFileS, String encryptedFileS, String decryptedFileS) {
|
private static void testCrypto(String clearTextFileS, String encryptedFileS, String decryptedFileS) {
|
||||||
|
|
Loading…
Reference in New Issue