为了避免对加密数据的攻击,而传输 CryptoDataFormat 也可以根据可配置的 MAC 算法计算加密交换内容的 Message Authentication Code。在加密后,计算的 HMAC 会附加到流中。它与解密阶段的流分开。根据传输的版本重新计算并验证的 MAC 以确保在传输中未被篡改。如需有关消息身份验证代码的更多信息,请参阅 http://en.wikipedia.org/wiki/HMAC
KeyGenerator generator = KeyGenerator.getInstance("DES");
CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES", generator.generateKey());
cryptoFormat.setShouldAppendHMAC(true);
from("direct:hmac")
.marshal(cryptoFormat)
.to("mock:encrypted")
.unmarshal(cryptoFormat)
.to("mock:unencrypted");
KeyGenerator generator = KeyGenerator.getInstance("DES");
CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES", generator.generateKey());
cryptoFormat.setShouldAppendHMAC(true);
from("direct:hmac")
.marshal(cryptoFormat)
.to("mock:encrypted")
.unmarshal(cryptoFormat)
.to("mock:unencrypted");
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
<crypto id="hmac" algorithm="DES" keyRef="desKey" shouldAppendHMAC="true" />
<crypto id="hmac" algorithm="DES" keyRef="desKey" shouldAppendHMAC="true" />
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
默认情况下,HMAC 使用 HmacSHA1 mac 算法计算,但可以通过提供不同的算法名称来轻松地更改。请参阅此处了解如何通过配置的安全供应商检查哪些算法可用
KeyGenerator generator = KeyGenerator.getInstance("DES");
CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES", generator.generateKey());
cryptoFormat.setShouldAppendHMAC(true);
cryptoFormat.setMacAlgorithm("HmacMD5");
from("direct:hmac-algorithm")
.marshal(cryptoFormat)
.to("mock:encrypted")
.unmarshal(cryptoFormat)
.to("mock:unencrypted");
KeyGenerator generator = KeyGenerator.getInstance("DES");
CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES", generator.generateKey());
cryptoFormat.setShouldAppendHMAC(true);
cryptoFormat.setMacAlgorithm("HmacMD5");
from("direct:hmac-algorithm")
.marshal(cryptoFormat)
.to("mock:encrypted")
.unmarshal(cryptoFormat)
.to("mock:unencrypted");
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
<crypto id="hmac-algorithm" algorithm="DES" keyRef="desKey" macAlgorithm="HmacMD5" shouldAppendHMAC="true" />
<crypto id="hmac-algorithm" algorithm="DES" keyRef="desKey" macAlgorithm="HmacMD5" shouldAppendHMAC="true" />
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow