Is there a “single line” method for generating an encrypted line?

I want to create a link

http://site/?code=xxxxxxxxxx

Where xxxxxxxxxxis the encrypted string generated from the string user01. And I will need to convert it later.

Is there an easy way to encrypt and decrypt a string like this?

+3
source share
3 answers

you can try this to convert your string. It will be converted to Base64and then hex so that you can put the URL.

var inputString = "xxxxx";
var code = Convert.ToBase64String((new ASCIIEncoding()).GetBytes(inputString)).ToCharArray().Select(x => String.Format("{0:X}", (int)x)).Aggregate(new StringBuilder(), (x, y) => x.Append(y)).ToString();

and this to return a string, from hexadecimal to Base64and from Base64to the original string

var back = (new ASCIIEncoding()).GetString(Convert.FromBase64String(Enumerable.Range(0, code.Length / 2).Select(i => code.Substring(i * 2, 2)).Select(x => (char)Convert.ToInt32(x, 16)).Aggregate(new StringBuilder(), (x, y) => x.Append(y)).ToString()));
+6
source

, System.Security.Cryptography .

Edit:
? OK:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Decrypt(Encrypt("This is a sample", "thisismypassword"), "thisismypassword"));
    }

    public static string Encrypt(string plaintext, string password)
    {
        return Convert.ToBase64String((new AesManaged { Key = Encoding.UTF8.GetBytes(password), Mode = CipherMode.ECB  }).CreateEncryptor().TransformFinalBlock(Encoding.UTF8.GetBytes(plaintext), 0, Encoding.UTF8.GetBytes(plaintext).Length));
    }

    public static string Decrypt(string ciphertext, string password)
    {
        return Encoding.UTF8.GetString((new AesManaged { Key = Encoding.UTF8.GetBytes(password), Mode = CipherMode.ECB }).CreateDecryptor().TransformFinalBlock(Convert.FromBase64String(ciphertext), 0, Convert.FromBase64String(ciphertext).Length));
    }
}
+8
  • ROT13
  • ROT26
  • ---
  • /-WWW--urlencoded
  • encode_hex (aes_256 (secret_key, known_iv, to_utf_8 ()))
  • (, lookup_key).
+2

Source: https://habr.com/ru/post/1772154/


All Articles