Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, I have this function that decrypts and I want to do the opposite I want to use the same one to encrypt strings. How can I do this?

C#
public string Decrypt(string cipherText, int Iv, string[] pass)
   {
     string password = "sdHBDHC^BGGijvn";
     byte[] buffer = Convert.FromBase64String(cipherText);
     using (Aes aes = Aes.Create())
     {
       Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, new byte[13]
       {
         (byte) 73,
         (byte) 118,
         (byte) 97,
         (byte) 110,
         (byte) 32,
         (byte) 77,
         (byte) 101,
         (byte) 100,
         (byte) 118,
         (byte) 101,
         (byte) 100,
         (byte) 101,
         (byte) 118
       });
       aes.Key = rfc2898DeriveBytes.GetBytes(32);
       aes.IV = rfc2898DeriveBytes.GetBytes(16);
       using (MemoryStream memoryStream = new MemoryStream())
       {
         using (CryptoStream cryptoStream = new CryptoStream((Stream) memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
         {
           cryptoStream.Write(buffer, 0, buffer.Length);
           cryptoStream.Close();
         }
         cipherText = Encoding.Unicode.GetString(memoryStream.ToArray());
       }
     }
     return cipherText;
   }


What I have tried:

I can decrypt with this function strings that were encrypted with it but I want to do the opposite I want to encrypt strings using this function. I know that some things need to be changed to create the encript. but how would that look? Thank you.
Posted
Updated 3-Apr-18 16:27pm
Comments
[no name] 3-Apr-18 9:44am    
Here is an example, I hope it fits: Rfc2898DeriveBytes Class (System.Security.Cryptography)[^]
ShakalX 3-Apr-18 10:49am    
Hi. Thank you, but I'm an apprentice. I do not quite understand these things. I thought I could use the base script to do the encript. For it must have few modifications to make.

 
Share this answer
 
Comments
ShakalX 3-Apr-18 22:28pm    
Thanks for help. it work. =)
Solution thanks for all
public static string Encrypt1(string clearText, int Iv, string[] pass)
           {
           string EncryptionKey = "sdHBDHC^BGGijvn";
           byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
           using (Aes encryptor = Aes.Create())
               {
               Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[13] { 73, 118, 97, 110, 32, 77, 101, 100, 118, 101, 100, 101, 118 });
               encryptor.Key = pdb.GetBytes(32);
               encryptor.IV = pdb.GetBytes(16);
               using (MemoryStream ms = new MemoryStream())
                   {
                   using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                       {
                       cs.Write(clearBytes, 0, clearBytes.Length);
                       cs.Close();
                       }
                   clearText = Convert.ToBase64String(ms.ToArray());
                   }
               }
           return clearText;
           }
 
Share this answer
 

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