简体   繁体   中英

Encode decoded string with Base64String

I am learning how to encode and decode string. This is a method to decode chiper text to plain text I found around the web.

public static string Decode(string chiperText)
{
    byte[] numArray = Convert.FromBase64String(chiperText);
    byte[] numArray1 = new byte[(int)numArray.Length - 1];
    byte num = (byte)(numArray[0] ^ 188);
    for (int i = 1; i < (int)numArray.Length; i++)
    {
        numArray1[i - 1] = (byte)(numArray[i] ^ 188 ^ num);
    }
    return Encoding.ASCII.GetString(numArray1);
}

My problem is I have no idea how to encode to original state. I try this method and it doesn't work.

public static string Encode(string plainText)
{
    byte[] bytes = Encoding.ASCII.GetBytes(plainText);

    byte[] results = new byte[(int)bytes.Length - 1];

    byte num = (byte)(bytes[0] ^ 188);
    for (int i = 1; i < bytes.Length; i++)
    {
        results[i - 1] = (byte)(bytes[i] ^ 188 ^ num);
    }

    return Convert.ToBase64String(results);
}

Although I agree entirely with SLaks comment that the above does not constitute any kind of crypto that you should use, the following procedure will produce the "encrypted" data that you are looking to decrypt:

public static string Encode(string plainText)
{
    byte[] numArray = System.Text.Encoding.Default.GetBytes(plainText);
    byte[] numArray1 = new byte[(int)numArray.Length + 1];
    // Generate a random byte as the seed used
    (new Random()).NextBytes(numArray1);
    byte num = (byte)(numArray1[0] ^ 188);
    numArray1[0] = numArray1[0];
    for (int i = 0; i < (int)numArray.Length; i++)
    {
        numArray1[i + 1] = (byte)(num ^ 188 ^ numArray[i]);
    }
    return Convert.ToBase64String(numArray1);
}

Please do not, for a single second, consider using this as a method for 'encrypting' sensitive data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM