Does c # have a simple symmetric encryption algorithm like XOR? Or is it better to fence the cycles? There is a whole library of System.Security.Criptography but it’s not even clear where to look. It is necessary to encrypt the text with a symmetric key and decrypt (registration cookie).

I have a mixed form of authentication, and I cannot get the standard library to work. It is necessary to transmit the login (string).

string user = "my_user"; System.Web.HttpContext.Current.User = new GenericPrincipial( new (GenericIdentify(user), new string[]{} ); 
  • 2
    And what do you dislike about xor in the loop? Do you think the library will do it better than yourself? - VladD
  • Ie cycle will be easier? - nick_n_a
  • Well, two lines of code plus a function header is easier than looking for a library and connecting it? I think so. - VladD
  • System.Security.Criptography does not have the necessary gamming encryption. Due to the fact that this cipher is trivial. And, as written above, to write this function is a matter of a couple of minutes. - KamikyIT

2 answers 2

The author, at least to transfer the authentication data to the remote side - this is not good. If the knowledge of the login does not allow authentication, then why protect it? If it allows, then this is the same password. And in this case it is more correct to transfer the hash from this data (n. SHA256) + salt for introducing entropy.

Otherwise, you either use the hardware to speed up the work (eg, processor intrinsic), or the most

 public void Xor(Byte[] data, Byte[] key) { for(int i = 0; i < data.Length; i++) data[i] ^= key[i % key.Length]; } 

The only thing you can speed up here is to wrap in unsafe , take a pointer with fixed and work with bare memory, bypassing checks for going beyond the array, but in the case of the first cycle, the compiler will do it himself. Perhaps speed up access to the key.

And in .NET there really is a set of encryption algorithms, including symmetric ones. For example, the old man DES.

 public static Byte[] DesEncrypt(UInt64 desKey, UInt64 desVector, Byte[] decryptedData) { Byte[] key = BitConverter.GetBytes(desKey); Byte[] vector = BitConverter.GetBytes(desVector); using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider()) { provider.Padding = PaddingMode.Zeros; using (MemoryStream inputStream = new MemoryStream(decryptedData)) using (MemoryStream outputStream = new MemoryStream(decryptedData.Length)) using (ICryptoTransform transform = provider.CreateEncryptor(key, vector)) using (CryptoStream encStream = new CryptoStream(outputStream, transform, CryptoStreamMode.Write)) { inputStream.CopyTo(encStream); encStream.FlushFinalBlock(); return outputStream.ToArray(); } } } public static Byte[] DesDecrypt(UInt64 desKey, UInt64 desVector, Byte[] encryptedData) { Byte[] key = BitConverter.GetBytes(desKey); Byte[] vector = BitConverter.GetBytes(desVector); using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider()) { provider.Padding = PaddingMode.Zeros; using (MemoryStream inputStream = new MemoryStream(encryptedData)) using (MemoryStream outputStream = new MemoryStream(encryptedData.Length)) using (ICryptoTransform transform = provider.CreateDecryptor(key, vector)) using (CryptoStream decStream = new CryptoStream(inputStream, transform, CryptoStreamMode.Read)) { decStream.CopyTo(outputStream); return outputStream.ToArray(); } } } 

    It turned out to be encrypted with the substitution of a symmetric algorithm. Without Stream turns out much shorter:

     public static string RC2Cript(string q, bool cript) { byte[] s1 = (cript)?System.Text.UTF8Encoding.UTF8.GetBytes(q):Convert.FromBase64String(q); SymmetricAlgorithm cripto = new RC2CryptoServiceProvider(); cripto.Key = new byte[] { 0x10, 0x24,0x76,0x45, 0x84, 0x64, 0x25, 0x34}; cripto.IV = new byte[] { 0x14, 0x24, 0x76, 0x48, 0x84, 0x64, 0x25, 0x34 }; s1 = ((ICryptoTransform)((cript) ? cripto.CreateEncryptor() : cripto.CreateDecryptor())).TransformFinalBlock(s1, 0, s1.Length); return (cript) ? Convert.ToBase64String(s1) : System.Text.UTF8Encoding.UTF8.GetString(s1).Trim(); } 

    With DESCryptoServiceProvider also works. Probably good, but here you can any ready to substitute.