Original: C # string Encryption decryption function
using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
//default key Vector
Private Staticbyte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
///<summary>
/// DESEncrypt string
///</summary>
///<param name= "encryptstring" >string to encrypt</param>
///<param name= "Encryptkey" >encryption Key,Requirements for8bit</param>
///<returns>encryption successfully returns the encrypted string, failed to return the source string</returns>
PublicStaticstring encryptdes (string encryptstring, string Encryptkey)
{
Try
{
byte[] rgbKey = Encoding.UTF8.GetBytes (encryptkey.substring (0, 8));
byte[] rgbiv = keys;//byte[] rgbiv = RgbKey;
byte[] inputbytearray = Encoding.UTF8.GetBytes (encryptstring);
descryptoserviceprovider DCSP = NewDESCryptoServiceProvider ();
memorystream Mstream = NewMemoryStream ();
cryptostream Cstream = New CryptoStream (mstream, dcsp.createencryptor (RgbKey, rgbiv), CryptoStreamMode.Write);
cstream.write ( inputbytearray, 0, inputbytearray.length);
cstream.flushfinalblock ();
Returned format
Modified in order to match the next PHP des encryption decryption
StringBuilder ret = Newstringbuilder (); foreach (Byte b in Mstream.toarray ()) {ret. AppendFormat ("{0:x2}", b); } ret. ToString (); Returnret. ToString ();
//returnconvert.tobase64string (Mstream.toarray ());
}
Catch
{
return encryptstring;
}
}
///<summary>
/// DESdecrypting a string
///</summary>
///<param name= "decryptstring" >string to decrypt</param>
///<param name= "Decryptkey" >Decryption Key,Requirements for8bit,same as encryption key</param>
///<returns>decryption succeeded in returning the decrypted string, failed to return the source string</returns>
PublicStaticstring decryptdes (string decryptstring, string Decryptkey)
{
Try
{
byte[] rgbKey = Encoding.UTF8.GetBytes (Decryptkey);
byte[] rgbiv = Keys;
byte[] inputbytearray = convert.frombase64string (decryptstring);
descryptoserviceprovider dcsp = NewDESCryptoServiceProvider ();
memorystream Mstream = New MemoryStream ();
cryptostream cstream c11>= NewCryptoStream (Mstream, dcsp. CreateDecryptor (RgbKey, rgbiv), cryptostreammode.write);
cstream.write ( Inputbytearray, 0, inputbytearray.length);
cstream.flushfinalblock ();
returnEncoding.UTF8.GetString (Mstream.toarray ());
}
Catch
{
return decryptstring;
}
}
===========================================================================================
Small knowledge:
The above key vector can be converted to a string, the 0x12 look can not be counted.
C # Converts a string to byte[], and byte[] to a string
1StringToByte[]
StringStr="ABCD" ;
Byte[]bytes=System.Text.Encoding.UTF8.GetBytes (str);
-------------------------------------------------------------------------------------------------------------
2Byte[]ToString
Byte[]bytes=NewBYTE[255];
StringStr=System.Text.Encoding.UTF8.GetString (bytes,0,bytes. Length);
--------------------------------------------------------------------------------------------
In the attached two methods:
private static Byte[]hexstringtobytearray (string s)
{
s = S.replace ("", "");
byte[] buffer = new BYTE[S.LENGTH/2];
for (int i = 0; i < s.length; i + = 2)
BUFFER[I/2] = (byte) convert.tobyte (S.substring (i, 2), 16);
return buffer;
}
private string bytearraytohexstring (byte[] data)
{
StringBuilder sb = new StringBuilder (data. Length * 3);
foreach (byte b in data)
Sb. Append (convert.tostring (b, 16). PadLeft (2, ' 0 '). PadRight (3, "));
Return SB. ToString (). ToUpper ();
}
C # string Encryption decryption function