C # implementation BASE64 encryption decryption

Source: Internet
Author: User
Tags base64 decrypt
Method One
Class Base64helper {///<summary>///Base64 encryption, UTF8 encoded encryption///</summary>///<param Name = ' source ' > plaintext to be encrypted </param>///<returns> encrypted string </returns> public static string Base64Encode (str
    ing source) {return Base64Encode (Encoding.UTF8, source); }///<summary>///Base64 encryption///</summary>///<param name= "Encodetype" > Encryption using the Encoding method </ param>///<param name= "source" > Encrypted plaintext </param>///<returns></returns> public Stati C String Base64Encode (Encoding Encodetype, String source) {string encode = string.
        Empty;
        byte[] bytes = encodetype.getbytes (source);
        try {encode = convert.tobase64string (bytes);
        catch {encode = source;
    return encode; ///<summary>///Base64 decryption, using UTF8 encoding method to decrypt///</summary>///<param name= "ResULt > Ciphertext to decrypt </param>///<returns> decrypted string </returns> public static string Base64decode (String re
    Sult) {return Base64decode (Encoding.UTF8, result); }///<summary>///Base64 decryption///</summary>///<param name= "Encodetype" > Decryption using the Encoding method, note and add Dense time in the same way </param>///<param name= "Result" > to decrypt the ciphertext </param>///<returns> decrypted string </returns > public static string Base64decode (Encoding encodetype, string result) {string decode = string.
        Empty;
        byte[] bytes = convert.frombase64string (result);
        try {decode = encodetype.getstring (bytes);
        catch {decode = result;
    return decode; }
}
Method Two

There is an encryption method: plaintext encrypted after the generation length is 24-bit ciphertext, and ciphertext to = end. After the query found that everyone said this encryption method is BASE64 encryption, but this encryption method in one of the BASE64 encryption, the specific code is as follows.

Class program
{
    static void Main (string[] args)
    {
        string original = "123456";

        var md5 = bitconverter.tostring (new System.Security.Cryptography.MD5CryptoServiceProvider (). ComputeHash (Encoding.Default.GetBytes (original))). Replace ("-", ""). ToLower ();
        Console.WriteLine ("md5=" + MD5);

        B64 is a cipher with a length of 24 bits at the end of =
        b64 = convert.tobase64string (new System.Security.Cryptography.MD5CryptoServiceProvider (). ComputeHash (Encoding.Default.GetBytes (original)));
        Console.WriteLine ("b64=" + b64);

        var dec = bitconverter.tostring (New System.Security.Cryptography.FromBase64Transform (). TransformFinalBlock (Encoding.Default.GetBytes (b64), 0, Encoding.Default.GetBytes (b64). Length)). Replace ("-", ""). ToLower ();
        Console.WriteLine ("dec=" + dec);
        Console.ReadLine ();            
    }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.