我对.net相当陌生,并对DataProtector有疑问。在.net中缩短DataProtection加密长度?
当没有任何配置使用DataProtector.Protect时,产生的加密对于我需要传递给它的API来说太长了,我在想如果使用配置方法(如看到here)会有帮助吗?我试图在类中的下列地方,我需要保护的数据:
var serviceCollection = new ServiceCollection();
serviceCollection.AddDataProtection()
.UseCustomCryptographicAlgorithms(new ManagedAuthenticatedEncryptionSettings()
{
// a type that subclasses SymmetricAlgorithm
EncryptionAlgorithmType = typeof(Aes),
// specified in bits
EncryptionAlgorithmKeySize = 128,
// a type that subclasses KeyedHashAlgorithm
ValidationAlgorithmType = typeof(HMACSHA256)
});
var services = serviceCollection.BuildServiceProvider();
_protector = services.GetDataProtector("MyClass.v1");
var protect = _protector.Protect(JsonConvert.SerializeObject(myData));
然而,即使从默认256改变EncryptionAlgorithmKeySize到最小128之后,“保护”仍然导致相同长度的加密这使我认为配置不工作或配置不会影响加密长度。
有谁知道这样做是正确的方式还是有更好的方法来减少加密长度?
例如,一个简单的9个字符的字符串会被加密为134个字符。
任何帮助非常感谢,谢谢!
这听起来确实不错。为什么要将加密的数据从机器传输出去,为什么您要发送的API的长度有限? – CodeCaster
@CodeCaster数据包含防伪令牌,但API只允许来回发送/传递500个字符,因此只是想知道是否有可配置的方法来限制加密长度。 –