/// <summary> /// Initializes a new instance of the <see cref="RC4CryptoServiceProvider"/> class. /// </summary> public RC4CryptoServiceProvider() { // acquire an RC4 context m_Provider = CAPIProvider.Handle; /* if (SspiProvider.CryptAcquireContext(ref m_Provider, IntPtr.Zero, null, SecurityConstants.PROV_RSA_FULL, 0) == 0) { if (Marshal.GetLastWin32Error() == SecurityConstants.NTE_BAD_KEYSET) SspiProvider.CryptAcquireContext(ref m_Provider, IntPtr.Zero, null, SecurityConstants.PROV_RSA_FULL, SecurityConstants.CRYPT_NEWKEYSET); }*/ if (m_Provider != 0) { int dwFlags = SecurityConstants.CRYPT_FIRST; bool found = false; IntPtr provEnum = Marshal.AllocHGlobal(100); int dwSize; do { dwSize = 100; if (SspiProvider.CryptGetProvParam(m_Provider, SecurityConstants.PP_ENUMALGS_EX, provEnum, ref dwSize, dwFlags) == 0) break; dwFlags = 0; PROV_ENUMALGS_EX eax = (PROV_ENUMALGS_EX)Marshal.PtrToStructure(provEnum, typeof(PROV_ENUMALGS_EX)); if (eax.aiAlgid == SecurityConstants.CALG_RC4) { found = true; m_MinLen = eax.dwMinLen; m_MaxLen = eax.dwMaxLen; } } while (!found); Marshal.FreeHGlobal(provEnum); /* if (!found) { SspiProvider.CryptReleaseContext(m_Provider, 0); m_Provider = 0; }*/ } m_Managed = new ARCFourManaged(); }
static void Main(string[] args) { Console.WriteLine("This example shows how to use the symmetric encryption classes from the Security Library.\r\n"); SymmetricAlgorithm algorithm; ICryptoTransform encryptor, decryptor; Console.WriteLine("Select the symmetric algorithm you want to use:"); Console.WriteLine(" [1] ARCFour [managed RC4]"); Console.WriteLine(" [2] RC4 [unmanaged]"); Console.WriteLine(" [3] Rijndael"); Console.Write("Your choice: "); string input = Console.ReadLine().Trim(); // initialize the selected symmetric algorithm switch(input) { case "1": algorithm = new ARCFourManaged(); break; case "2": algorithm = new RC4CryptoServiceProvider(); break; case "3": algorithm = new RijndaelCryptoServiceProvider(); break; default: Console.WriteLine("Invalid input."); return; } Console.WriteLine("Enter some text that will be encrypted:"); input = Console.ReadLine(); byte[] plaintext = Encoding.ASCII.GetBytes(input); // generate an IV that consists of bytes with the value zero // in real life applications, the IV should not be set to // an array of bytes with the value zero! algorithm.IV = new byte[algorithm.BlockSize / 8]; // generate a new key algorithm.GenerateKey(); // create the encryption and decryption objects encryptor = algorithm.CreateEncryptor(); decryptor = algorithm.CreateDecryptor(); // encrypt the bytes byte[] encrypted = encryptor.TransformFinalBlock(plaintext, 0, plaintext.Length); // decrypt the encrypted bytes byte[] decrypted = decryptor.TransformFinalBlock(encrypted, 0, encrypted.Length); // write the byte arrays to the console Console.WriteLine("\r\nResults:"); Console.WriteLine(" Input data: " + Encoding.ASCII.GetString(plaintext)); Console.WriteLine(" Key: " + BytesToHex(algorithm.Key)); Console.WriteLine(" Encrypted data: " + BytesToHex(encrypted)); Console.WriteLine(" Decrypted data: " + Encoding.ASCII.GetString(decrypted)); Console.WriteLine("\r\nPress ENTER to continue..."); Console.ReadLine(); // dispose of the resources algorithm.Clear(); encryptor.Dispose(); decryptor.Dispose(); }
/// <summary> /// Initializes a new instance of the <see cref="RC4CryptoServiceProvider"/> class. /// </summary> public RC4CryptoServiceProvider() { // acquire an RC4 context m_Provider = CAPIProvider.Handle; /* if (SspiProvider.CryptAcquireContext(ref m_Provider, IntPtr.Zero, null, SecurityConstants.PROV_RSA_FULL, 0) == 0) { * if (Marshal.GetLastWin32Error() == SecurityConstants.NTE_BAD_KEYSET) * SspiProvider.CryptAcquireContext(ref m_Provider, IntPtr.Zero, null, SecurityConstants.PROV_RSA_FULL, SecurityConstants.CRYPT_NEWKEYSET); * }*/ if (m_Provider != 0) { int dwFlags = SecurityConstants.CRYPT_FIRST; bool found = false; IntPtr provEnum = Marshal.AllocHGlobal(100); int dwSize; do { dwSize = 100; if (SspiProvider.CryptGetProvParam(m_Provider, SecurityConstants.PP_ENUMALGS_EX, provEnum, ref dwSize, dwFlags) == 0) { break; } dwFlags = 0; PROV_ENUMALGS_EX eax = (PROV_ENUMALGS_EX)Marshal.PtrToStructure(provEnum, typeof(PROV_ENUMALGS_EX)); if (eax.aiAlgid == SecurityConstants.CALG_RC4) { found = true; m_MinLen = eax.dwMinLen; m_MaxLen = eax.dwMaxLen; } } while (!found); Marshal.FreeHGlobal(provEnum); /* if (!found) { * SspiProvider.CryptReleaseContext(m_Provider, 0); * m_Provider = 0; * }*/ } m_Managed = new ARCFourManaged(); }
/// <summary> /// Releases all managed and unmanaged resources used by this class. /// </summary> private void _Dispose() { if (!m_Disposed) { m_Disposed = true; if (m_Managed != null) { m_Managed.Clear(); m_Managed = null; } /* if (m_Provider != 0) { SspiProvider.CryptReleaseContext(m_Provider, 0); m_Provider = 0; }*/ try { GC.SuppressFinalize(this); } catch {} } }
/// <summary> /// Releases all managed and unmanaged resources used by this class. /// </summary> protected override void Dispose(bool disposing) { base.Dispose(disposing); if (disposing && !m_Disposed) { m_Disposed = true; if (m_Managed != null) { m_Managed.Clear(); m_Managed = null; } /* if (m_Provider != 0) { SspiProvider.CryptReleaseContext(m_Provider, 0); m_Provider = 0; }*/ try { GC.SuppressFinalize(this); } catch { } } }