DES encryption weak key issue

Solved1.04K viewsCryptography and Security

DES encryption weak key issue

Hello there,

I am facing an issue with my code (.net) when I try to encrypt data using key with all 0 it says ‘Specified key is a known weak key for ‘TripleDES’ and cannot be used.’ . with actual keys encryption is working fine. Can somebody help with this. Below is the code I use (c#)

TripleDES des = new TripleDESCryptoServiceProvider();
des.Key = key;
des.Mode = CipherMode.CBC;
des.IV = iv;
des.Padding = PaddingMode.None;
byte[] output = des.CreateEncryptor().TransformFinalBlock(data, 0, data.Length);

Thanks,

Aslam

Question is closed for new answers.
mohlam Selected answer as best April 27, 2021
1

Below code will help you with this. But may I know your use case for using weak keys? I hope only for testing purposes/initializing configuration etc. and recommend not to use it in your actual environment if you don’t have a specific use case with weak keys.

[apcode language=”csharp”]

TripleDES tripleDESalg = TripleDES.Create();
            TripleDESCryptoServiceProvider sm = tripleDESalg as TripleDESCryptoServiceProvider;
            sm.Padding = PaddingMode.None;
            sm.Mode = CipherMode.CBC;
            sm.IV = IV;
            MethodInfo mi = sm.GetType().GetMethod("_NewEncryptor", BindingFlags.NonPublic | BindingFlags.Instance);
            object[] Par = { Key, sm.Mode, EmptyIV, sm.FeedbackSize, 0 };
            ICryptoTransform trans = mi.Invoke(sm, Par) as ICryptoTransform;
             byte[] baOutData = SCUtility.ByteArrayToHexString(trans.TransformFinalBlock(data, 0, data.Length));

[/apcode]

You can find more information about weak keys here

admin Edited answer April 30, 2021

Thanks. It worked. It is only for testing and not for the actual product

2
You are viewing 1 out of 1 answers, click here to view all answers.