How to encrypt and decrypt a string in C #

I want to encrypt and decrypt a string in C # using an algorithm that will lead me to the same encrypted string. For example, if I encrypt the line 122ujhdheiwe and the result is uoi8asdf8asdf , and again, if I encrypt the same line 122ujhdheiwe , it will lead me to uoi8asdf8asdf . What is a possible encryption algorithm that I can use and how?

+3
source share
6 answers

There are many such algorithms in .NET, look here: http://www.codeproject.com/KB/security/SimpleEncryption.aspx

+3
source

rot13 (), ? 13. , , .

+1

:

http://remy.supertext.ch/2011/01/simple-c-encryption-and-decryption/

Bascially :

DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] Key = { 12, 13, 14, 15, 16, 17, 18, 19 };
byte[] IV =  { 12, 13, 14, 15, 16, 17, 18, 19 };

ICryptoTransform encryptor = des.CreateEncryptor(Key, IV);

byte[] IDToBytes = ASCIIEncoding.ASCII.GetBytes(source);
byte[] encryptedID = encryptor.TransformFinalBlock(IDToBytes, 0, IDToBytes.Length);
return Convert.ToBase64String(encryptedID);

.

+1

ProtectedData, :

using System;
using System.Security.Cryptography;
using System.Text;

private void example()
{
    string data = "122ujhdheiwe";

    // Encrypt
    UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
    byte[] secret = ProtectedData.Protect(unicodeEncoding.GetBytes(data), null, DataProtectionScope.CurrentUser);
    Console.WriteLine(BitConverter.ToString(secret));

    // If you need it as a printable string, you can convert the binary to Base64
    string base64 = Convert.ToBase64String(secret);
    Console.WriteLine(base64);

    // Back to binary...
    byte[] backagain = Convert.FromBase64String(base64);

    // Decrypt
    byte[] clearbytes = ProtectedData.Unprotect(backagain, null, DataProtectionScope.CurrentUser);
    string roundtripped = unicodeEncoding.GetString(clearbytes);
    Console.WriteLine(roundtripped);
}

. ProtectedDataClass

, , , (122ujhdheiwe == > uoi8asdf8asdf), , , - , - cf. , .

EDIT: , , , ProtectedData , , , .

+1

Crypto Class Triple Des

.NET: .setting file

, / :)

0

enter image description here

- ? using System.Security.Cryptography;

        public string encryptus(string x, string encrypt)//function
    {
        try
        {

            string y = x;
            byte[] etext = UTF8Encoding.UTF8.GetBytes(y);
            string Code = encrypt;
            MD5CryptoServiceProvider mdhash = new MD5CryptoServiceProvider();
            byte[] keyarray = mdhash.ComputeHash(UTF8Encoding.UTF8.GetBytes(Code));
            TripleDESCryptoServiceProvider tds = new TripleDESCryptoServiceProvider();
            tds.Key = keyarray;
            tds.Mode = CipherMode.ECB;
            tds.Padding = PaddingMode.PKCS7;

            ICryptoTransform itransform = tds.CreateEncryptor();
            byte[] result = itransform.TransformFinalBlock(etext, 0, etext.Length);
            string encryptresult = Convert.ToBase64String(result);
            return encryptresult.ToString();
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
        public string dencryptus(string x, string keyai)
    {
        try
        {
            string y = x.Replace("\0", null);
            byte[] etext = Convert.FromBase64String(y);
            string key = keyai;
            MD5CryptoServiceProvider mdhash = new MD5CryptoServiceProvider();
            byte[] keyarray = mdhash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            TripleDESCryptoServiceProvider tds = new TripleDESCryptoServiceProvider();
            tds.Key = keyarray;
            tds.Mode = CipherMode.ECB;
            tds.Padding = PaddingMode.PKCS7;

            ICryptoTransform itransform = tds.CreateDecryptor();
            byte[] result = itransform.TransformFinalBlock(etext, 0, etext.Length);
            string dencryptresult = UTF8Encoding.UTF8.GetString(result);
            return dencryptresult.ToString();
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

:

Encrypted.Text = encryptus(Message.Text, EncryptCode.Text.ToString());
Decrypted.Text = dencryptus(Message.Text, EncryptCode.Text.ToString());
0

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


All Articles