runtime: TripleDESCryptoServiceProvider.Key - specified Key is not valid size
Running the following code:
[TestMethod]
public void TripleDesKeyTest()
{
byte[] key = new byte[16] { 3, 3, 3, 5, 222, 13, 155, 55, 122, 123, 165, 187, 188, 1, 11, 133 };
byte[] data = Encoding.UTF8.GetBytes("superSeekritSauerkraut");
var des = new TripleDESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Key = key;
var Transform = des.CreateEncryptor();
byte[] encryptedResult = Transform.TransformFinalBlock(data, 0, data.Length);
Assert.IsNotNull(encryptedResult);
Assert.IsTrue(encryptedResult.Length > 0);
}
Works against full framework, but when running against .NET Core I get:
System.Security.Cryptography.CryptographicException occurred
HResult=0x80131501
Message=Specified key is not a valid size for this algorithm.
Source=<Cannot evaluate the exception source>
StackTrace:
at System.Security.Cryptography.SymmetricAlgorithm.set_Key(Byte[] value)
at System.Security.Cryptography.TripleDES.set_Key(Byte[] value)
at System.Security.Cryptography.TripleDESCryptoServiceProvider.set_Key(Byte[] value)
at Westwind.Utilities.Test.EncryptionTests.TripleDesKeyTest() in C:\projects2010\Westwind.Utilities.Core\Westwind.Utilities.Test\EncryptionTests.cs:line 20
After digging around a bit it seems the key should be 16 or 24 bytes, but even if the key is that size it doesn’t work.
Expected: Same behavior as full framework which accepts a key of any byte size.
Updated So it looks like a 24 byte key works.
Either way this doesn’t seem right. If using CipherMode.ECB shouldn’t the key be adjusted to fit?
Here’s the test output:
PS C:\projects2010\Westwind.Utilities.Core\Westwind.Utilities.Test> dotnet test --filter Westwind.Utilities.Test.EncryptionTests.TripleDesKeyTest
Build started, please wait...
Build completed.
Test run for C:\projects2010\Westwind.Utilities.Core\Westwind.Utilities.Test\bin\Debug\netcoreapp2.0\Westwind.Utilities.Test.dll(.NETCoreApp,Version=v2.0)
Microsoft (R) Test Execution Command Line Tool Version 15.3.0-preview-20170502-03
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
Failed Westwind.Utilities.Test.EncryptionTests.TripleDesKeyTest
Error Message:
Test method Westwind.Utilities.Test.EncryptionTests.TripleDesKeyTest threw exception:
System.Security.Cryptography.CryptographicException: Specified key is not a valid size for this algorithm.
Stack Trace:
at System.Security.Cryptography.SymmetricAlgorithm.set_Key(Byte[] value)
at System.Security.Cryptography.TripleDES.set_Key(Byte[] value)
at System.Security.Cryptography.TripleDESCryptoServiceProvider.set_Key(Byte[] value)
at Westwind.Utilities.Test.EncryptionTests.TripleDesKeyTest() in C:\projects2010\Westwind.Utilities.Core\Westwind.Utilities.Test\EncryptionTests.cs:line 24
Total tests: 1. Passed: 0. Failed: 1. Skipped: 0.
Test Run Failed.
Test execution time: 0.9436 Seconds
Test run for C:\projects2010\Westwind.Utilities.Core\Westwind.Utilities.Test\bin\Debug\net452\Westwind.Utilities.Test.dll(.NETFramework,Version=v4.5.2)
Microsoft (R) Test Execution Command Line Tool Version 15.3.0-preview-20170502-03
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
Total tests: 1. Passed: 1. Failed: 0. Skipped: 0.
Test Run Successful.
Test execution time: 0.8669 Seconds
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (12 by maintainers)
Understood and totally agree.
Just thinking through some of the scenarios that can cause problems.