C # DES encryption class, 16-bit encryption .,
This Encryption Class is self-written because it is not the same as the DES encryption written in java. It is finally the same as Java encryption, and solves the different problems after encryption.
You can directly call the encryption and decryption methods in it.
Using System; using System. collections. generic; using System. linq; using System. web; using System. text; using System. security. cryptography; using System. IO; namespace EallNum. helper {public class FI_DesTools {private FI_DesTools () {} private static string key = "×××× "; /// <summary> /// Key for symmetric encryption and decryption /// </summary> public static string key {get {return key;} set {Key = value ;}} /// <summary> // DES encryption /// </summary> /// <param name = "encryptString"> </param> /// <returns> </returns> public static string DesEncrypt (string strEncryptString) {StringBuilder strRetValue = new StringBuilder (); try {byte [] keyBytes = Encoding. UTF8.GetBytes (key. substring (0, 8); byte [] keyIV = keyBytes; byte [] inputByteArray = Encoding. UTF8.GetBytes (strEncryptString); DESCryptoServiceProvider provider = new DESCryptoServiceProvider (); provider. mode = CipherMode. ECB; // compatible with Des encryption algorithm provider in other languages. padding = PaddingMode. zeros; // auto-fill 0 MemoryStream mStream = new MemoryStream (); CryptoStream cStream = new CryptoStream (mStream, provider. createEncryptor (keyBytes, keyIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); // base64 encoding is not used // return Convert. toBase64String (mStream. toArray (); // It is organized into a hexadecimal string foreach (byte B in mStream. toArray () {strRetValue. appendFormat ("{0: X2}", B) ;}} catch (Exception e) {Console. writeLine (e);} return strRetValue. toString ();} /// <summary> /// DES decryption /// </summary> /// <param name = "decryptString"> </param> /// <returns> </returns> public static string DesDecrypt (string strDecryptString) {string strRetValue = ""; try {byte [] keyBytes = Encoding. UTF8.GetBytes (key. substring (0, 8); byte [] keyIV = keyBytes; // base64 decoding is not used. // byte [] inputByteArray = Convert. fromBase64String (decryptString); // hexadecimal conversion to byte [] inputByteArray = new byte [strDecryptString. length/2]; for (int x = 0; x <strDecryptString. length/2; x ++) {int I = (Convert. toInt32 (strDecryptString. substring (x * 2, 2), 16); inputByteArray [x] = (byte) I;} DESCryptoServiceProvider provider = new DESCryptoServiceProvider (); provider. mode = CipherMode. ECB; // compatible with Des encryption algorithm provider in other languages. padding = PaddingMode. zeros; // auto-fill 0 MemoryStream mStream = new MemoryStream (); CryptoStream cStream = new CryptoStream (mStream, provider. createDecryptor (keyBytes, keyIV), CryptoStreamMode. write); cStream. write (inputByteArray, 0, inputByteArray. length); cStream. flushFinalBlock (); // remove the ending null character // strRetValue = Encoding. UTF8.GetString (mStream. toArray (); strRetValue = Encoding. UTF8.GetString (mStream. toArray ()). trimEnd ('\ 0');} catch (Exception e) {Console. writeLine (e) ;}return strRetValue ;}}}