REM:
1. 增加AES256加密算法支持
This commit is contained in:
HuangXin 2020-09-16 15:28:46 +08:00
parent f7085b6520
commit c9716b81c5
2 changed files with 55 additions and 1 deletions

View File

@ -116,6 +116,45 @@ public class CryptoHelper {
return cipher.doFinal(ciphertext); 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 [ ]. * Des decryption byte [ ].

View File

@ -65,7 +65,7 @@ public class CryptoHelperTest {
* @throws InvalidKeyException the invalid key exception * @throws InvalidKeyException the invalid key exception
*/ */
@Test @Test
public void t2_desTest() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, public void t3_desTest() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException,
BadPaddingException, InvalidKeyException { BadPaddingException, InvalidKeyException {
String srcTest = "hello word"; String srcTest = "hello word";
String key = "hkoUV5ZWh0q1jSxMnpjovVn19Qg99HY6DD40"; String key = "hkoUV5ZWh0q1jSxMnpjovVn19Qg99HY6DD40";
@ -77,4 +77,19 @@ public class CryptoHelperTest {
byte[] aesDecode = CryptoHelper.desDecryption(aesCode, key); byte[] aesDecode = CryptoHelper.desDecryption(aesCode, key);
Assert.assertEquals(new String(aesDecode), srcTest); 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);
}
} }