public static string Decrypt(string input, string key) { byte[] inputArray = System.Convert.FromBase64String(input); RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider(); rc2.Key = UTF8Encoding.UTF8.GetBytes(key); rc2.Mode = CipherMode.ECB; rc2.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rc2.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length); rc2.Clear(); return UTF8Encoding.UTF8.GetString(resultArray); }
/// <summary> /// RC2 解密(用变长密钥对大量数据进行加密) /// </summary> /// <param name="DecryptString">待解密密文</param> /// <param name="DecryptKey">解密密钥</param> /// <returns>returns</returns> public static string RC2Decrypt(string DecryptString, string DecryptKey) { if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); } if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); } if (DecryptKey.Length < 5 || DecryptKey.Length > 16) { throw (new Exception("密钥必须为5-16位")); } byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; string m_strDecrypt = ""; RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider(); try { byte[] m_btDecryptString = Convert.FromBase64String(DecryptString); MemoryStream m_stream = new MemoryStream(); CryptoStream m_cstream = new CryptoStream(m_stream, m_RC2Provider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write); m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length); m_cstream.FlushFinalBlock(); m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray()); m_stream.Close(); m_stream.Dispose(); m_cstream.Close(); m_cstream.Dispose(); } catch (IOException ex) { throw ex; } catch (CryptographicException ex) { throw ex; } catch (ArgumentException ex) { throw ex; } catch (Exception ex) { throw ex; } finally { m_RC2Provider.Clear(); } return m_strDecrypt; }
/// <summary> Function to encrypt an one content bytes /// </summary> /// <param name="content">Function to encrypt an one content bytes</param> /// <returns>Array of bytes</returns> public byte[] Encrypt(byte[] content) { if (!IsHashAlgorithm && _key == null) { throw new CryptographicException(Resources.Cypher_NoKey); } if (content == null || content.Equals(String.Empty)) { throw new CryptographicException(Resources.Cypher_NoContent); } byte[] cipherBytes = null; int NumBytes = 0; #if !CompactFramework if (_algorithm == Algorithm.RSA) { //This is an asymmetric call, which has to be treated differently cipherBytes = RSAEncrypt(content); } else #endif if (IsHashAlgorithm) { string hash = GenerateHash(System.Text.Encoding.UTF8.GetString(content, 0, content.Length)); cipherBytes = System.Text.Encoding.UTF8.GetBytes(hash); } else { SymmetricAlgorithm provider; switch (_algorithm) { case Algorithm.DES: provider = new DESCryptoServiceProvider(); NumBytes = Convert.ToInt32(AlgorithmKeySize.DES); break; case Algorithm.TripleDES: provider = new TripleDESCryptoServiceProvider(); NumBytes = Convert.ToInt32(AlgorithmKeySize.TripleDES); break; case Algorithm.Rijndael: provider = new RijndaelManaged(); NumBytes = Convert.ToInt32(AlgorithmKeySize.Rijndael); break; case Algorithm.RC2: provider = new RC2CryptoServiceProvider(); NumBytes = Convert.ToInt32(AlgorithmKeySize.RC2); break; default: throw new CryptographicException(Resources.Cypher_InvalidProvider); } try { //Encrypt the string cipherBytes = SymmetricEncrypt(provider, content, _key, NumBytes); } finally { //Free any resources held by the SymmetricAlgorithm provider provider.Clear(); } } return cipherBytes; }
/// <summary> Function to decrypt an one content bytes /// </summary> /// <param name="content">To be encrypted content</param> /// <returns>Array of bytes</returns> public byte[] Decrypt(byte[] content) { if (IsHashAlgorithm) { throw new CryptographicException(Resources.Cypher_DecryptHash); } if (_key == null) { throw new CryptographicException(Resources.Cypher_NoKey); } if (content == null || content.Equals(String.Empty)) { throw new CryptographicException(Resources.Cypher_NoContent); } string encText = System.Text.Encoding.UTF8.GetString(content, 0, content.Length); if (_encodingType == EncodingType.Base64) { //We need to convert the content to Hex before decryption encText = BytesToHex(System.Convert.FromBase64String(encText)); } byte[] clearBytes = null; int NumBytes = 0; #if !CompactFramework if (_algorithm == Algorithm.RSA) { clearBytes = RSADecrypt(encText); } else #endif { SymmetricAlgorithm provider; switch (_algorithm) { case Algorithm.DES: provider = new DESCryptoServiceProvider(); NumBytes = Convert.ToInt32(AlgorithmKeySize.DES); break; case Algorithm.TripleDES: provider = new TripleDESCryptoServiceProvider(); NumBytes = Convert.ToInt32(AlgorithmKeySize.TripleDES); break; case Algorithm.Rijndael: provider = new RijndaelManaged(); NumBytes = Convert.ToInt32(AlgorithmKeySize.Rijndael); break; case Algorithm.RC2: provider = new RC2CryptoServiceProvider(); NumBytes = Convert.ToInt32(AlgorithmKeySize.RC2); break; default: throw new CryptographicException(Resources.Cypher_InvalidProvider); } try { clearBytes = SymmetricDecrypt(provider, encText, _key, NumBytes); } finally { //Free any resources held by the SymmetricAlgorithm provider provider.Clear(); } } //Now return the plain text content return clearBytes; }
/// <summary> /// RC2 加密(用变长密钥对大量数据进行加密) /// </summary> /// <param name="encryptString">待加密密文</param> /// <param name="encryptKey">加密密钥</param> /// <returns>returns</returns> public static string RC2Encrypt(string encryptString, string encryptKey) { if (string.IsNullOrEmpty(encryptString)) { throw (new Exception("密文不得为空")); } if (string.IsNullOrEmpty(encryptKey)) { throw (new Exception("密钥不得为空")); } if (encryptKey.Length < 5 || encryptKey.Length > 16) { throw (new Exception("密钥必须为5-16位")); } string m_strEncrypt = ""; byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider(); try { byte[] m_btencryptString = Encoding.Default.GetBytes(encryptString); using (var m_stream = new MemoryStream()) { using (var m_cstream = new CryptoStream(m_stream, m_RC2Provider.CreateEncryptor(Encoding.Default.GetBytes(encryptKey), m_btIV), CryptoStreamMode.Write)) { m_cstream.Write(m_btencryptString, 0, m_btencryptString.Length); m_cstream.FlushFinalBlock(); m_strEncrypt = Convert.ToBase64String(m_stream.ToArray()); } } } catch (Exception ex) { throw ex; } finally { m_RC2Provider.Clear(); } return m_strEncrypt; }