Analysis of des encryption and decryption source code C key value

  • 2020-05-30 20:58:41
  • OfStack

Company protocol security requirements, the transmission content needs to do des, md5 encryption.

I was a little dizzy when I was assigned this task because I was a newcomer. Start searching the web for all kinds of des encrypted content. Because do not understand that the need to understand the principle, and finally missed the time, confused themselves. Of course, logical, interested friends can try to do it.

First paste encryption, decryption of the source code:


/// <summary>       
///  Encrypt the data        
/// </summary>       
/// <param name="Text"></param>       
/// <param name="sKey"></param>       
/// <returns></returns>       
public static string Encrypt(string Text, string sKey)         {           
DESCryptoServiceProvider des = new DESCryptoServiceProvider();           
byte[] inputByteArray;           
inputByteArray = Encoding.Default.GetBytes(Text);           
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));           
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));           
System.IO.MemoryStream ms = new System.IO.MemoryStream();           
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);           
cs.Write(inputByteArray, 0, inputByteArray.Length);           
cs.FlushFinalBlock();           
StringBuilder ret = new StringBuilder();           
foreach (byte b in ms.ToArray())             {               
ret.AppendFormat("{0:X2}", b);           
}           
return ret.ToString();       
}
        #endregion
/// <summary>       
///  Decrypt the data        
/// </summary>       
/// <param name="Text"></param>       
/// <param name="sKey"></param>       
/// <returns></returns>       
public static string Decrypt(string Text, string sKey)         {           
DESCryptoServiceProvider des = new DESCryptoServiceProvider();           
int len;           
len = Text.Length / 2;           
byte[] inputByteArray = new byte[len];           
int x, i;           
for (x = 0; x < len; x++)             {               
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);               
inputByteArray[x] = (byte)i;             }           
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));           
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));           
System.IO.MemoryStream ms = new System.IO.MemoryStream();           
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);           
cs.Write(inputByteArray, 0, inputByteArray.Length);           
cs.FlushFinalBlock();           
return Encoding.Default.GetString(ms.ToArray());         }
#endregion

Because it is the first time to contact des and the requirements of the company's agreement document, I am confused about the following things in this code:

1: two parameters

Text is the content to be encrypted

sKey is used as a key to encrypt content. Of course, when encrypting and decrypting, the value of sKey should be kept at 1.

2: key value of des object

The key value and the IV value are fixed 8-bit lengths, so remember the 1. Since our parameter sKey is of indefinite length, we adopted a method to encrypt it with MD5 and then intercept its first 8 bits. This is to ensure that key1 is delivered at the time of decryption. Otherwise it will be decrypted incorrectly.

Finally, I would like to say 1 as a newcomer, I feel to remember a few places, perhaps in the eyes of most people write des necessary points ~~ don't spray me, ah,

A few essential objects:

What do you think of des without it

MemoryStream stream objects stored in memory

CryptoStream defines linking a data stream to an encrypted transformation stream. It writes to the MemoryStream object

And finally, String.


Related articles: