SoFunction
Updated on 2025-03-07

Common string encryption and decryption methods for C# encapsulation code


//Method 1
//Add to add a reference
//using ;
/// <summary>
/// SHA1 encryption string
/// </summary>
/// <param name="source">source string</param>
/// <returns> Encrypted string</returns>
public string SHA1(string source)
{
    return (source, "SHA1");
}
/// <summary>
/// MD5 encrypted string
/// </summary>
/// <param name="source">source string</param>
/// <returns> Encrypted string</returns>
public string MD5(string source)
{
    return (source, "MD5");;
}


//Method 2 (reversible encryption and decryption):
//using ;
public string Encode(string data)
{
    byte[] byKey = (KEY_64);
    byte[] byIV = (IV_64);
    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    int i = ;
    MemoryStream ms = new MemoryStream();
    CryptoStream cst = new CryptoStream(ms, (byKey, byIV), );
    StreamWriter sw = new StreamWriter(cst);
    (data);
    ();
    ();
    ();
    return Convert.ToBase64String((), 0, (int));
}
public string Decode(string data)
{
    byte[] byKey = (KEY_64);
    byte[] byIV = (IV_64);
    byte[] byEnc;
    try
    {
        byEnc = Convert.FromBase64String(data);
    }
    catch
    {
        return null;
    }
    DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
    MemoryStream ms = new MemoryStream(byEnc);
    CryptoStream cst = new CryptoStream(ms, (byKey, byIV), );
    StreamReader sr = new StreamReader(cst);


//Method 3 (MD5 irreversible):
//using ;
//MD5 irreversible encryption
//32-bit encryption
public string GetMD5_32(string s, string _input_charset)
{
    MD5 md5 = new MD5CryptoServiceProvider();
    byte[] t = ((_input_charset).GetBytes(s));
    StringBuilder sb = new StringBuilder(32);
    for (int i = 0; i < ; i++)
    {
        (t[i].ToString("x").PadLeft(2, '0'));
    }
    return ();
}
//16-bit encryption
public static string GetMd5_16(string ConvertString)
{
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    string t2 = (((ConvertString)), 4, 8);
    t2 = ("-", "");
    return t2;
}


//Method 4 (symmetric encryption):
//using ;
//using ;
private SymmetricAlgorithm mobjCryptoService;
private string Key;
/// <summary>  
/// Constructor of symmetric encryption class
/// </summary>  
public SymmetricMethod()
{
    mobjCryptoService = new RijndaelManaged();
    Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
}
/// <summary>  
/// Obtain the key
/// </summary>  
/// <returns>Key</returns>
private byte[] GetLegalKey()
{
    string sTemp = Key;
    ();
    byte[] bytTemp = ;
    int KeyLength = ;
    if ( > KeyLength)
        sTemp = (0, KeyLength);
    else if ( < KeyLength)
        sTemp = (KeyLength, ' ');
    return (sTemp);
}
/// <summary>  
/// Obtain the initial vector IV
/// </summary>  
/// <returns>First trial vector IV</returns>
private byte[] GetLegalIV()
{
    string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
    ();
    byte[] bytTemp = ;
    int IVLength = ;
    if ( > IVLength)
        sTemp = (0, IVLength);
    else if ( < IVLength)
        sTemp = (IVLength, ' ');
    return (sTemp);
}
/// <summary>  
/// Encryption method
/// </summary>  
/// <param name="Source">String to be encrypted</param>
/// <returns>Encrypted string</returns>
public string Encrypto(string Source)
{
    byte[] bytIn = UTF8Encoding.(Source);
    MemoryStream ms = new MemoryStream();
    = GetLegalKey();
    = GetLegalIV();
    ICryptoTransform encrypto = ();
    CryptoStream cs = new CryptoStream(ms, encrypto, );
    (bytIn, 0, );
    ();
    ();
    byte[] bytOut = ();
    return Convert.ToBase64String(bytOut);
}
/// <summary>  
/// Decryption method
/// </summary>  
/// <param name="Source">Serial to be decrypted</param>
/// <returns>Decrypted string</returns>
public string Decrypto(string Source)
{
    byte[] bytIn = Convert.FromBase64String(Source);
    MemoryStream ms = new MemoryStream(bytIn, 0, );
    = GetLegalKey();
    = GetLegalIV();
    ICryptoTransform encrypto = ();
    CryptoStream cs = new CryptoStream(ms, encrypto, );
    StreamReader sr = new StreamReader(cs);
    return ();
}

//Method 5:
//using ;
//using ;
//using ;
//Default key vector
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
/**//**//**//// <summary>
/// DES encryption string
/// </summary>
/// <param name="encryptString">Stand to be encrypted</param>
/// <param name="encryptKey">Encrypt key, required to be 8 bits</param>
/// <returns> Encryption successfully returns the encrypted string, and fails to return the source string</returns>
public static string EncryptDES(string encryptString, string encryptKey)
{
    try
    {
        byte[] rgbKey = Encoding.((0, 8));
        byte[] rgbIV = Keys;
        byte[] inputByteArray = Encoding.(encryptString);
        DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
        MemoryStream mStream = new MemoryStream();
        CryptoStream cStream = new CryptoStream(mStream, (rgbKey, rgbIV), );
        (inputByteArray, 0, );
        ();
        return Convert.ToBase64String(());
    }
    catch
    {
        return encryptString;
    }
}
/**//**//**//// <summary>
/// DES decryption string
/// </summary>
/// <param name="decryptString">Stand to be decrypted</param>
/// <param name="decryptKey">Decrypt key, required to be 8 bits, the same as the encryption key</param>
/// <returns>Decryption successfully returns the decrypted string, fails to return to the source string</returns>
public static string DecryptDES(string decryptString, string decryptKey)
{
    try
    {
        byte[] rgbKey = Encoding.(decryptKey);
        byte[] rgbIV = Keys;
        byte[] inputByteArray = Convert.FromBase64String(decryptString);
        DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
        MemoryStream mStream = new MemoryStream();
        CryptoStream cStream = new CryptoStream(mStream, (rgbKey, rgbIV), );
        (inputByteArray, 0, );
        ();
        return Encoding.(());
    }
    catch
    {
        return decryptString;
    }
}


//Method 6 (file encryption):
//using ;
//using ;
//using ;
//Encrypt the file
private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
    //Create the file streams to handle the input and output files.
    FileStream fin = new FileStream(inName, , );
    FileStream fout = new FileStream(outName, , );
    (0);
    //Create variables to help with read and write.
    byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
    long rdlen = 0;              //This is the total number of bytes written.
    long totlen = ;    //This is the total length of the input file.
    int len;                     //This is the number of bytes to be written at a time.
    DES des = new DESCryptoServiceProvider();
    CryptoStream encStream = new CryptoStream(fout, (desKey, desIV), );
    //Read from the input file, then encrypt and write to the output file.
    while (rdlen < totlen)
    {
        len = (bin, 0, 100);
        (bin, 0, len);
        rdlen = rdlen + len;
    }
    ();
    ();
    ();
}
//Decrypt the file
private static void DecryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
    //Create the file streams to handle the input and output files.
    FileStream fin = new FileStream(inName, , );
    FileStream fout = new FileStream(outName, , );
    (0);
    //Create variables to help with read and write.
    byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
    long rdlen = 0;              //This is the total number of bytes written.
    long totlen = ;    //This is the total length of the input file.
    int len;                     //This is the number of bytes to be written at a time.
    DES des = new DESCryptoServiceProvider();
    CryptoStream encStream = new CryptoStream(fout, (desKey, desIV), );
    //Read from the input file, then encrypt and write to the output file.
    while (rdlen < totlen)
    {
        len = (bin, 0, 100);
        (bin, 0, len);
        rdlen = rdlen + len;
    }
    ();
    ();
    ();
    return ();
}