parent
f7085b6520
commit
c9716b81c5
|
@ -116,6 +116,45 @@ public class CryptoHelper {
|
|||
return cipher.doFinal(ciphertext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Aes 256 encryption byte [ ].
|
||||
*
|
||||
* @param plaintext the plaintext
|
||||
* @param aesKey the aes key
|
||||
* @return the byte [ ]
|
||||
* @throws NoSuchAlgorithmException the no such algorithm exception
|
||||
* @throws NoSuchPaddingException the no such padding exception
|
||||
* @throws InvalidKeyException the invalid key exception
|
||||
* @throws BadPaddingException the bad padding exception
|
||||
* @throws IllegalBlockSizeException the illegal block size exception
|
||||
*/
|
||||
public static byte[] aes256Encryption(byte[] plaintext, String aesKey) throws NoSuchAlgorithmException,
|
||||
NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
|
||||
Cipher cipher = Cipher.getInstance(AES_ALGORITHM_STR);
|
||||
SecretKeySpec key = new SecretKeySpec(sha256Encryption(aesKey), "AES");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, key);
|
||||
return cipher.doFinal(plaintext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Aes 256 decryption byte [ ].
|
||||
*
|
||||
* @param ciphertext the ciphertext
|
||||
* @param aesKey the aes key
|
||||
* @return the byte [ ]
|
||||
* @throws NoSuchAlgorithmException the no such algorithm exception
|
||||
* @throws NoSuchPaddingException the no such padding exception
|
||||
* @throws InvalidKeyException the invalid key exception
|
||||
* @throws BadPaddingException the bad padding exception
|
||||
* @throws IllegalBlockSizeException the illegal block size exception
|
||||
*/
|
||||
public static byte[] aes256Decryption(byte[] ciphertext, String aesKey) throws NoSuchAlgorithmException,
|
||||
NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
|
||||
Cipher cipher = Cipher.getInstance(AES_ALGORITHM_STR);
|
||||
SecretKeySpec key = new SecretKeySpec(sha256Encryption(aesKey), "AES");
|
||||
cipher.init(Cipher.DECRYPT_MODE, key);
|
||||
return cipher.doFinal(ciphertext);
|
||||
}
|
||||
|
||||
/**
|
||||
* Des decryption byte [ ].
|
||||
|
|
|
@ -65,7 +65,7 @@ public class CryptoHelperTest {
|
|||
* @throws InvalidKeyException the invalid key exception
|
||||
*/
|
||||
@Test
|
||||
public void t2_desTest() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException,
|
||||
public void t3_desTest() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException,
|
||||
BadPaddingException, InvalidKeyException {
|
||||
String srcTest = "hello word";
|
||||
String key = "hkoUV5ZWh0q1jSxMnpjovVn19Qg99HY6DD40";
|
||||
|
@ -77,4 +77,19 @@ public class CryptoHelperTest {
|
|||
byte[] aesDecode = CryptoHelper.desDecryption(aesCode, key);
|
||||
Assert.assertEquals(new String(aesDecode), srcTest);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void t4_aes256Test() throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException,
|
||||
NoSuchAlgorithmException, NoSuchPaddingException {
|
||||
String srcTest = "hello word";
|
||||
String key = "hkoUV5ZWh0q1jSxMnpjovVn19Qg99HY6DD40";
|
||||
byte[] aesCode = CryptoHelper.aes256Encryption(srcTest.getBytes(StandardCharsets.UTF_8), key);
|
||||
String showText = CryptoHelper.base64Encryption(aesCode);
|
||||
|
||||
|
||||
Assert.assertEquals(showText, "3sTXo4P2/pGQEfL9UJ/wRQ==");
|
||||
|
||||
byte[] aesDecode = CryptoHelper.aes256Decryption(aesCode, key);
|
||||
Assert.assertEquals(new String(aesDecode), srcTest);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue