[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,6 +33,7 @@ public class AesCryptoHelperTest {
|
||||||
@Test
|
@Test
|
||||||
public void shouldWrapStreams() throws Exception {
|
public void shouldWrapStreams() throws Exception {
|
||||||
|
|
||||||
|
try {
|
||||||
byte[] clearTextBytes = "Some text".getBytes();
|
byte[] clearTextBytes = "Some text".getBytes();
|
||||||
|
|
||||||
// encrypt data
|
// encrypt data
|
||||||
|
@ -52,10 +58,18 @@ public class AesCryptoHelperTest {
|
||||||
byte[] decryptedBytes = decryptedOut.toByteArray();
|
byte[] decryptedBytes = decryptedOut.toByteArray();
|
||||||
|
|
||||||
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 shouldEncryptBytes() {
|
public void shouldEncryptBytes() {
|
||||||
|
try {
|
||||||
|
|
||||||
byte[] clearTextBytes = "Some text".getBytes();
|
byte[] clearTextBytes = "Some text".getBytes();
|
||||||
|
|
||||||
|
@ -63,10 +77,19 @@ public class AesCryptoHelperTest {
|
||||||
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() {
|
||||||
|
try {
|
||||||
|
|
||||||
// file to be encrypted
|
// file to be encrypted
|
||||||
String clearTextFileS = "src/test/resources/crypto_test_short.txt";
|
String clearTextFileS = "src/test/resources/crypto_test_short.txt";
|
||||||
// encrypted file
|
// encrypted file
|
||||||
|
@ -75,10 +98,19 @@ public class AesCryptoHelperTest {
|
||||||
String decryptedFileS = "target/decrypted_short.txt";
|
String decryptedFileS = "target/decrypted_short.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 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";
|
||||||
|
@ -88,10 +120,19 @@ public class AesCryptoHelperTest {
|
||||||
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";
|
||||||
|
@ -101,11 +142,21 @@ public class AesCryptoHelperTest {
|
||||||
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() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
// file to be encrypted
|
// file to be encrypted
|
||||||
String clearTextFileS = "src/test/resources/crypto_test_image.ico";
|
String clearTextFileS = "src/test/resources/crypto_test_image.ico";
|
||||||
// encrypted file
|
// encrypted file
|
||||||
|
@ -114,6 +165,14 @@ public class AesCryptoHelperTest {
|
||||||
String decryptedFileS = "target/decrypted_image.ico";
|
String decryptedFileS = "target/decrypted_image.ico";
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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