0

I'm looking for an encryption method to encrypt a string. The important thing is the output result (because of some reasons).The security is not very important but speed is.

I was testing some algorithm and on my case it's really important to have not some characters like "+" and "=" on output result.

Is it possible to handle it, if yes which encryption algorithm or method I can use?

motevalizadeh
  • 5,244
  • 14
  • 61
  • 108
  • 1
    Encrypting a string in C# is easy - look at the algorithms in System.Security.Cryptography. Figuring out how to manage the keys is the tricky part. The output is a byte array. Often, that will be encoded to a Base-64 string (which is what you likely have when you have "+" and "="). If you don't like that, encode it as a hex string (look on this site for the best way to do that) – Flydog57 Mar 07 '19 at 16:20
  • As a veteran of SO, you shouldn't be surprised by this question: what have you tried? – Peter B Mar 07 '19 at 16:20
  • @Chrᴉz: MD5 is a hashing algorithm, not an encryption algorithm. It's one-way only (you can't "decrypt" it). Yes, the OP said that "security is not very important", but, still, no one should be using MD5 for anything (look it up on Wikipedia, and the first paragraph has a quote that says that MD5 is _"cryptographically broken and unsuitable for further use"_. – Flydog57 Mar 07 '19 at 16:23
  • @Flydog57 your answer is so great and it resolved my issue, pleas post it as the answer, I used hex string to handle it, Thanks – motevalizadeh Mar 07 '19 at 16:40

2 Answers2

2

The problem you have with the "+" and "=" is that the byte array you have as a result of your encryption is being converted to a Base64 string (which likes "=" and "+"). Convert it to a Hex string instead (where all the characters are 0-9 and A-F). Take a look at: How do you convert a byte array to a hexadecimal string, and vice versa?.

As to encryption...

Encryption is easy in C# (take a look at the algorithms in System.Security.Cryptography). Managing the keys is the tricky part. You really need to put some thought into your use case.

Flydog57
  • 6,851
  • 2
  • 17
  • 18
  • 2
    Encryption is definitely *not* easy, in C# or otherwise. I would argue that 90% of the code examples on StackOverflow that demonstrate "how to encrypt" actually demonstrate "how to encrypt with multiple significant security vulnerabilities". – Luke Joshua Park Mar 07 '19 at 21:15
  • 1
    Yes, I completely agree. But, the act of encrypting data is very easy in C#. However, doing it securely is **_HARD_**. – Flydog57 Mar 07 '19 at 21:48
1

If you separate escaping and encryption you can use any encryption you like. Once you have the encrypted string you can use a very simple escaping such as:

return encrypted.Replace("\"", "\"\"") // \ -> \\
       .Replace("+", "\"Plus")         // + -> \Plus
       .Replace("=", "\"Eq");          // = -> \Eq

Before decrypting you have to unescape this result:

return escaped.Replace("\"Eq", "=")
       .Replace("\"Plus", "+")
       .Replace("\"\"", "\"");

Where encrypted and escaped can also be StringBuilder instances.

György Kőszeg
  • 17,093
  • 6
  • 37
  • 65