大佬教程收集整理的这篇文章主要介绍了使用 AES 加密数据测试 REST 端点,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
环境:Java 8 / SpringBoot v2.0.2
我试图通过将 Cipher 实例从“AES”更改为“AES/GCM/Nopadding”来解决代码中的安全问题。但是,对于具有使用此加密方法的加密路径参数值的 REST 端点的现有测试失败。
这是我的加密方法,
public String encrypt(final Long transactionID) {
Assert.notNull(transactionID,"transaction ID Should Not Be null");
String encryptedText = "";
try
{ final byte[] encodedSecretKey = Base64.decodeBase64(encryptKey);
final SecretKeySpec secretKey = new SecretKeySpec(encodedSecretKey,"AES");
Cipher cipher = Cipher.geTinstance("AES/GCM/Nopadding");
cipher.init(1,secretKey);
final byte[] contentAsBytes = transactionID.toString().getBytes();
byte[] contentAsByteCypherText = cipher.doFinal(contentAsBytes);
byte[] iv = cipher.getIV();
byte[] message = new byte[numbER_OF_Iv_bYTES + contentAsByteCypherText.length];
System.arraycopy(iv,SRC_position,message,DEST_position,numbER_OF_Iv_bYTES);
System.arraycopy(contentAsByteCypherText,numbER_OF_Iv_bYTES,contentAsByteCypherText.length);
encryptedText = Base64.encodeBase64URLSafeString(messagE);
} catch (NoSuchAlgorithmException | NoSuchpaddingException | InvalIDKeyException | IllegalBlockSizeException | BadpaddingException E) {
LOGGER.error("Failed to encrypt transaction ID ",E);
throw new BlahException("Failed to encrypt transaction ID",E);
}
return encryptedText;
}
我的测试
public voID testjourney(final Long transactionID) throws Exception {
final String request = loadExpectedContent(transactionID);
mockRestserviceServer.reset();
mockRestserviceServer.expect(requestTo("/spring/rest/transaction/" + webClIEnt.encrypt(transactionID)))
.andExpect(method(httpR_487_11845@ethod.GET))
.andRespond(withsuccess().ContentType(MediaType.APPliCATION_JsON).body(request));
final CompletableFuture<String> completable = webClIEnt.getWebjourney(transactionID);
mockRestserviceServer.verify();
final String response = completable.get();
Assert.assertNotNull(responsE);
LOGGER.info(LoggingUtils.format(LoggingUtils.keyvalue("request",transactionID),LoggingUtils.keyvalue("Response",LoggingUtils.parse(responsE))));
}
在这个测试中,加密方法被命中两次。
不同之处在于,当 Cipher.geTinstance("AES/GCM/Nopadding");
被使用两次时,它返回两个不同的值,但未通过测试。但是当使用 Cipher.geTinstance("AES");
时(没有 IV)它两次返回相同的值。
我的问题是如何使用“AES/GCM/Nopadding”测试这个 REST 端点?
为了测试它你不想得到相同的密文,你想在解密后得到相同的明文。使用 GCM,您需要对某些内容进行两次加密但得到不同结果的测试。
您不应该重复使用 IV 值(对于 GCM,相同的 IV 和明文导致相同的密文)。
所以:
byte[] iv = cipher.getIV();
将返回一个安全的随机生成的 12 字节数组,因此每次的密文都不同。
在没有 IV 的情况下调用 Cipher.geTinstance("AES")
绝对不是您想要做的:
我会更改测试以执行加密,然后解密(在测试中)并确保密文相同。
以上是大佬教程为你收集整理的使用 AES 加密数据测试 REST 端点全部内容,希望文章能够帮你解决使用 AES 加密数据测试 REST 端点所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。