Merge remote-tracking branch 'base-version/main' into dev
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,50 @@
|
||||
package com.zt.plat.framework.common.util.security;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
class CryptoSignatureUtilsTest {
|
||||
|
||||
@Test
|
||||
void decryptShouldIgnoreWhitespaceInCiphertext() {
|
||||
String key = "test-key";
|
||||
String plaintext = "{\"sample\":123}";
|
||||
|
||||
String encrypted = CryptoSignatureUtils.encrypt(plaintext, key, CryptoSignatureUtils.ENCRYPT_TYPE_AES);
|
||||
int splitIndex = Math.max(1, encrypted.length() / 2);
|
||||
String cipherWithWhitespace = " " + encrypted.substring(0, splitIndex)
|
||||
+ " \n\t "
|
||||
+ encrypted.substring(splitIndex);
|
||||
|
||||
String decrypted = CryptoSignatureUtils.decrypt(cipherWithWhitespace, key, CryptoSignatureUtils.ENCRYPT_TYPE_AES);
|
||||
|
||||
assertEquals(plaintext, decrypted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void decryptShouldRestorePlusCharactersConvertedToSpaces() {
|
||||
String key = "test-key";
|
||||
String basePlaintext = "payload-";
|
||||
|
||||
String encryptedWithPlus = null;
|
||||
String chosenPlaintext = null;
|
||||
for (int i = 0; i < 100; i++) {
|
||||
String candidatePlaintext = basePlaintext + i;
|
||||
String candidateEncrypted = CryptoSignatureUtils.encrypt(candidatePlaintext, key, CryptoSignatureUtils.ENCRYPT_TYPE_AES);
|
||||
if (candidateEncrypted.indexOf('+') >= 0) {
|
||||
encryptedWithPlus = candidateEncrypted;
|
||||
chosenPlaintext = candidatePlaintext;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assertNotNull(encryptedWithPlus, "Expected to generate ciphertext containing '+' character");
|
||||
|
||||
String mutatedCipher = encryptedWithPlus.replace('+', ' ');
|
||||
String decrypted = CryptoSignatureUtils.decrypt(mutatedCipher, key, CryptoSignatureUtils.ENCRYPT_TYPE_AES);
|
||||
|
||||
assertEquals(chosenPlaintext, decrypted);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user