Click here to Skip to main content
15,122,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to encrypt a string with a key. i have tried this code. I got this error. Please help me.
Any Symmetric Key Algorithm.

C#
Error	1	'string' does not contain a definition for 'CreateEncryptor' and no extension method 'CreateEncryptor' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)	G:\2nd Sem\Web Services\My WebService Projects in .NET\RecordProtocol\RecordProtocol\Form1.cs	68	64	RecordProtocol


What I have tried:

private void button4_Click(object sender, EventArgs e)//Encrypt
{
            string PlainText = textBox6.Text;
            string key = textBox10.Text;
            textBox7.Text = Encrypt(PlainText,key);
}
public static string Encrypt(string PlainText, string key)
{
            MemoryStream ms = new MemoryStream();
            CryptoStream crypstream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);
            StreamWriter sw = new StreamWriter(crypstream);
            sw.WriteLine(PlainText);
            sw.Close();
            crypstream.Close();
            byte[] buffer = ms.ToArray();
            ms.Close();
            string output;
            output = Encoding.ASCII.GetString(buffer);
            return output;           
}
Posted
Updated 26-Apr-16 21:28pm
v2

As the error says, CreateEncryptor is not a string function - it's a method which is called on the selected encryption algorithm. For example: SymmetricAlgorithm.CreateEncryptor Method (System.Security.Cryptography)[^] or
SymmetricAlgorithm.CreateEncryptor Method (Byte[], Byte[]) (System.Security.Cryptography)[^]
If you want encryption to work reliably - and unreliable encryption is no use to anyone - I'd suggest you start reading up on symmetric encryption: Encrypting Data[^]
The link includes working examples.
   
C#
public string Md5AddSecret(string pass)
    {
        string hashKey = "kuiygHJG564qwertynbv";
        string terminalId = "99999999";
        string reference = "0000008765";

        string x = Convert.ToBase64String(
            new SHA1CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(
                string.Concat(hashKey, terminalId, reference, pass))));

        return x;
    }
   
Comments
dipankarnalui 27-Apr-16 2:31am
   
This works fine. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900