Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
public static string Encrypt(string toEncrypt, bool useHashing)
    {
      byte[] bytes = Encoding.UTF8.GetBytes(toEncrypt);
      string s = (string) new AppSettingsReader().GetValue("SecurityKey", typeof (string));
      byte[] numArray;
      if (useHashing)
      {
        MD5CryptoServiceProvider cryptoServiceProvider = new MD5CryptoServiceProvider();
        numArray = cryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(s));
        cryptoServiceProvider.Clear();
      }
      else
        numArray = Encoding.UTF8.GetBytes(s);
      TripleDESCryptoServiceProvider cryptoServiceProvider1 = new TripleDESCryptoServiceProvider();
      cryptoServiceProvider1.Key = numArray;
      cryptoServiceProvider1.Mode = CipherMode.ECB;
      cryptoServiceProvider1.Padding = PaddingMode.PKCS7;
      byte[] inArray = cryptoServiceProvider1.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
      cryptoServiceProvider1.Clear();
      return Convert.ToBase64String(inArray, 0, inArray.Length);
    }

    public static string Decrypt(string cipherString, bool useHashing)
    {
      byte[] inputBuffer = Convert.FromBase64String(cipherString);
      string s = (string) new AppSettingsReader().GetValue("SecurityKey", typeof (string));
      byte[] numArray;
      if (useHashing)
      {
        MD5CryptoServiceProvider cryptoServiceProvider = new MD5CryptoServiceProvider();
        numArray = cryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(s));
        cryptoServiceProvider.Clear();
      }
      else
        numArray = Encoding.UTF8.GetBytes(s);
      TripleDESCryptoServiceProvider cryptoServiceProvider1 = new TripleDESCryptoServiceProvider();
      cryptoServiceProvider1.Key = numArray;
      cryptoServiceProvider1.Mode = CipherMode.ECB;
      cryptoServiceProvider1.Padding = PaddingMode.PKCS7;
      byte[] bytes = cryptoServiceProvider1.CreateDecryptor().TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
      cryptoServiceProvider1.Clear();
      return Encoding.UTF8.GetString(bytes);
    }


What I have tried:

Error1: Decleration Terminal Incorect
Posted
Updated 18-Apr-16 21:06pm
v2
Comments
Patrice T 19-Apr-16 1:52am    
Do you plan to tell us where the error occurs ?
CPallini 19-Apr-16 2:46am    
You should post the exact error message, in order to get help.

1 solution

That is not C++, which is likely to be your problem.
That is C# - and while they look superficially similar and share some common syntax they are completely different languages.
For example, your declaration of bytes in the first method:
C#
byte[] bytes = ...

If C++ would be either:
C++
byte bytes[] = ...
Or:
C++
array<Byte>^bytes = ...

If you aren't trying to compile C# code as C++, then you need to show us exactly where the error is, and what message you get.
 
Share this answer
 
v2

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