当前位置: 首页>>代码示例>>C#>>正文


C# Cryptography.SymmetricAlgorithm类代码示例

本文整理汇总了C#中System.Security.Cryptography.SymmetricAlgorithm的典型用法代码示例。如果您正苦于以下问题:C# SymmetricAlgorithm类的具体用法?C# SymmetricAlgorithm怎么用?C# SymmetricAlgorithm使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


SymmetricAlgorithm类属于System.Security.Cryptography命名空间,在下文中一共展示了SymmetricAlgorithm类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: IsolatedStorageOfflineContext

        /// <summary>
        /// Constructor for the offline context which allows a symmetric encryption algorithm to be specified.
        /// </summary>
        /// <param name="schema">The schema that specifies the set of the collections for the context.</param>
        /// <param name="scopeName">The scope name used to identify the scope on the service.</param>
        /// <param name="cachePath">Path in isolated storage where the data will be stored.</param>
        /// <param name="uri">Uri of the scopeName.  Used to intialize the CacheController.</param>
        /// <param name="encryptionAlgorithm">The symmetric encryption algorithm to use for files on disk</param>
        /// <remarks>
        /// If the Uri specified is different from the one that is stored in the cache path, the
        /// Load method will throw an InvalidOperationException.
        /// </remarks>
        public IsolatedStorageOfflineContext(IsolatedStorageSchema schema, string scopeName, string cachePath,
            Uri uri, SymmetricAlgorithm encryptionAlgorithm)
        {
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            if (string.IsNullOrEmpty(scopeName))
            {
                throw new ArgumentNullException("scopeName");
            }

            if (string.IsNullOrEmpty(cachePath))
            {
                throw new ArgumentNullException("cachePath");
            }

            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            _isDisposed = false;

            _schema = schema;
            _scopeUri = uri;
            _scopeName = scopeName;
            _cachePath = cachePath;
            _storageHandler = new SQLiteStorageHandler(this, schema, cachePath, encryptionAlgorithm);
            _saveSyncLock = new AutoResetLock();
            
            CreateCacheController();
        }
开发者ID:Fedorm,项目名称:core-master,代码行数:46,代码来源:IsolatedStorageOfflineContext.cs

示例2: EncryptBytes

        public static byte[] EncryptBytes(SymmetricAlgorithm symAlg, byte[] inBlock)
        {
            ICryptoTransform xfrm = symAlg.CreateEncryptor();
            byte[] outBlock = xfrm.TransformFinalBlock(inBlock, 0, inBlock.Length);

            return outBlock;
        }
开发者ID:TheNaked,项目名称:Firewind,代码行数:7,代码来源:Encryption.cs

示例3: Decrypt

 private static string Decrypt(byte[] cipher, string passPhrase, string salt, SymmetricAlgorithm algorithm)
 {
     var saltBytes = Encoding.UTF8.GetBytes(salt);
     algorithm.Padding = PaddingMode.None;
     using (algorithm)
     {
         using (var password = new Rfc2898DeriveBytes(passPhrase, saltBytes))
         {
             algorithm.Key = password.GetBytes(algorithm.KeySize / 8);
             algorithm.IV = password.GetBytes(algorithm.BlockSize / 8);
             using (var memStream = new MemoryStream(cipher))
             {
                 using (
                     var cryptoStream = new CryptoStream(memStream, algorithm.CreateDecryptor(),
                                                         CryptoStreamMode.Read))
                 {
                     using (var sr = new StreamReader(cryptoStream))
                     {
                         return sr.ReadToEnd();
                     }
                 }
             }
         }
     }
 }
开发者ID:gilles1977,项目名称:epayment-integration,代码行数:25,代码来源:CryptoUtils.cs

示例4: SymCryptography

        public SymCryptography(string serviceProviderName)
        {
            // Select symmetric algorithm
            switch (serviceProviderName.ToLower())
            {
                case "rijndael":
                    serviceProviderName = "Rijndael";
                    _algorithm = ServiceProviderEnum.Rijndael;
                    break;
                case "rc2":
                    serviceProviderName = "RC2";
                    _algorithm = ServiceProviderEnum.RC2;
                    break;
                case "des":
                    serviceProviderName = "DES";
                    _algorithm = ServiceProviderEnum.DES;
                    break;
                case "tripledes":
                    serviceProviderName = "TripleDES";
                    _algorithm = ServiceProviderEnum.TripleDES;
                    break;
            }

            // Set symmetric algorithm
            _cryptoService = (SymmetricAlgorithm)CryptoConfig.CreateFromName(serviceProviderName);
            _cryptoService.Mode = CipherMode.CBC;
        }
开发者ID:rdvanbuuren,项目名称:SecureSettingsManager,代码行数:27,代码来源:SymCryptography.cs

示例5: CipherTextStealingMode

        /// <summary>
        /// Initialize CipherTextStealingMode with a specific symmetric algorithm
        /// </summary>
        /// <param name="symmetricAlgorithm">The symmetric algorithm</param>
        public CipherTextStealingMode(SymmetricAlgorithm symmetricAlgorithm)
        {
            // in CTS Mode there is no padding
            symmetricAlgorithm.Padding = PaddingMode.None;

            // set the symmetric algorithm's mode to ECB
            // (for single block encryption and decryption)
            symmetricAlgorithm.Mode = CipherMode.ECB;

            // get the symmetric algorithm's block size in bytes
            blockSize = symmetricAlgorithm.BlockSize / 8;
            if (blockSize != symmetricAlgorithm.IV.Length)
            {
                throw new ArgumentException(
                    "The IV size should equal to the block size.");
            }

            // initialize local IV
            iv = symmetricAlgorithm.IV;

            // initialize cipher state using the symmetric algorithms's IV
            cipherState = new byte[blockSize];
            symmetricAlgorithm.IV.CopyTo(cipherState, 0);

            // create encryptor and decryptor
            encryptor = symmetricAlgorithm.CreateEncryptor();
            decryptor = symmetricAlgorithm.CreateDecryptor();
        }
开发者ID:LiuXiaotian,项目名称:WindowsProtocolTestSuites,代码行数:32,代码来源:CipherTextStealingMode.cs

示例6: EncryptionWrapper

        public EncryptionWrapper(string algorithmName)
        {
            if (string.IsNullOrEmpty(algorithmName))
                throw new ArgumentNullException("algorithmName");

            algorithm = SymmetricAlgorithm.Create(algorithmName);
        }
开发者ID:scopely,项目名称:aws-sdk-net,代码行数:7,代码来源:EncryptionWrapper.cs

示例7: Crypter

        public Crypter(SymmetricAlgorithm symmetricAlg)
        {
            if (symmetricAlg == null)
            throw new ArgumentNullException();

              algorithm = symmetricAlg;
        }
开发者ID:Nirklav,项目名称:TCPChat,代码行数:7,代码来源:Crypter.cs

示例8: NetCryptoProviderBase

		public NetCryptoProviderBase(NetPeer peer, SymmetricAlgorithm algo)
			: base(peer)
		{
			m_algorithm = algo;
			m_algorithm.GenerateKey();
			m_algorithm.GenerateIV();
		}
开发者ID:KennethYap,项目名称:MonoGame,代码行数:7,代码来源:NetCryptoProviderBase.cs

示例9: CryptoTransformBase

        public CryptoTransformBase(SymmetricAlgorithm algo, bool encryption, byte[] rgbKey, byte[] rgbIV)
        {
            if (rgbKey == null)
                throw new CryptographicException("Invalid (null) key");

            BlockSizeByte = (algo.BlockSize >> 3);

            if (rgbIV == null)
            {
                iv = new byte[BlockSizeByte];
                this.Random(iv, 0, BlockSizeByte);
            }
            else
            {
                // compare the IV length with the "currently selected" block size and *ignore* IV that are too big
                if (rgbIV.Length < BlockSizeByte)
                {
                    string msg = Locale.GetText("IV is too small ({0} bytes), it should be {1} bytes long.",
                        rgbIV.Length, BlockSizeByte);
                    throw new CryptographicException(msg);
                }
                iv = (byte[])rgbIV.Clone();
            }

            encrypt = encryption;
            padding = algo.Padding;

            // transform buffer
            workBuff = new byte[BlockSizeByte];
        }
开发者ID:symform,项目名称:crimson,代码行数:30,代码来源:CryptoTransformBase.cs

示例10: Decrypt

        /// <summary>
        /// 解密数据.
        /// </summary>
        /// <param name="Doc"></param>
        /// <param name="Alg"></param>
        public static void Decrypt(XmlDocument Doc, SymmetricAlgorithm Alg)
        {
            // Check the arguments.
            if (Doc == null)
                throw new ArgumentNullException("Doc");
            if (Alg == null)
                throw new ArgumentNullException("Alg");

            // Find the EncryptedData element in the XmlDocument.
            XmlElement encryptedElement = Doc.GetElementsByTagName("EncryptedData")[0] as XmlElement;

            // If the EncryptedData element was not found, throw an exception.
            if (encryptedElement == null)
            {
                throw new XmlException("The EncryptedData element was not found.");
            }

            // Create an EncryptedData object and populate it.
            EncryptedData edElement = new EncryptedData();
            edElement.LoadXml(encryptedElement);

            // Create a new EncryptedXml object.
            EncryptedXml exml = new EncryptedXml();

            // Decrypt the element using the symmetric key.
            byte[] rgbOutput = exml.DecryptData(edElement, Alg);

            // Replace the encryptedData element with the plaintext XML element.
            exml.ReplaceData(encryptedElement, rgbOutput);
        }
开发者ID:mahuidong,项目名称:my-csharp-sample,代码行数:35,代码来源:Program.cs

示例11: DecryptBytes

        private static byte[] DecryptBytes(SymmetricAlgorithm symAlg, byte[] inBytes)
        {
            ICryptoTransform xfrm = symAlg.CreateDecryptor();
            byte[] outBlock = xfrm.TransformFinalBlock(inBytes, 0, inBytes.Length);

            return outBlock;
        }
开发者ID:TheNaked,项目名称:Firewind,代码行数:7,代码来源:Program.cs

示例12: DeCrypt

        /// <summary>
        /// 通过制定的算法模式来加密一个字符串(不支持中文)
        /// </summary>
        /// <param name="Algorithm">解密的算法</param>
        /// <param name="ValueToDeCrypt">将要被解密的值</param>
        private static String DeCrypt(SymmetricAlgorithm Algorithm, String ValueToDeCrypt)
        {
            // Put the input string into the byte array.
            Byte[] InputByteArray = new Byte[ValueToDeCrypt.Length / 2];

            for (Int32 i = 0; i < ValueToDeCrypt.Length / 2; i++)
            {
                Int32 Value = (Convert.ToInt32(ValueToDeCrypt.Substring(i * 2, 2), 16));
                InputByteArray[i] = (Byte)Value;
            }

            // Create the crypto objects.
            String EncryptionKey = WhfEncryption.EncryptionKey;
            // Create the key.
            Byte[] Key = ASCIIEncoding.ASCII.GetBytes(EncryptionKey);
            Algorithm.Key = (Byte[])WhfEncryption.ReDim(Key, Algorithm.Key.Length);
            Algorithm.IV = (Byte[])WhfEncryption.ReDim(Key, Algorithm.IV.Length);

            MemoryStream MemStream = new MemoryStream();
            CryptoStream CrypStream = new CryptoStream(MemStream, Algorithm.CreateDecryptor(), CryptoStreamMode.Write);

            // Flush the data through the crypto stream into the memory stream.
            CrypStream.Write(InputByteArray, 0, InputByteArray.Length);
            CrypStream.FlushFinalBlock();

            // Get the decrypted data back from the memory stream.
            StringBuilder StringBuilder = new StringBuilder();

            for (Int32 i = 0; i < MemStream.ToArray().Length; i++)
            {
                StringBuilder.Append((Char)MemStream.ToArray()[i]);
            }

            return StringBuilder.ToString();
        }
开发者ID:kevin-h-wang,项目名称:tuopu,代码行数:40,代码来源:WhfEncryption.cs

示例13: EncryptString

        public static string EncryptString(string Value, string parKey)
        {
            mCSP = SetEnc();
            string iv = "PenS8UCVF7s=";
            mCSP.IV = Convert.FromBase64String(iv);
            string key = SetLengthString(parKey.ToString(), 32);
            mCSP.Key = Convert.FromBase64String(key);
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            Byte[] byt = new byte[64];

            try
            {
                ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
                byt = Encoding.UTF8.GetBytes(Value);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();

                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception Ex)
            {
                throw (new Exception("An error occurred while encrypting string"));
            }
        }
开发者ID:danygolden,项目名称:gianfratti,代码行数:29,代码来源:Crypto.cs

示例14: SymCryptography

        public SymCryptography(ServiceProviderEnum serviceProvider)
        {
            // Select symmetric algorithm

            switch (serviceProvider)
            {
                case ServiceProviderEnum.Rijndael:
                    mCryptoService = new RijndaelManaged();
                    mAlgorithm = ServiceProviderEnum.Rijndael;
                    break;
                case ServiceProviderEnum.RC2:
                    mCryptoService = new RC2CryptoServiceProvider();
                    mAlgorithm = ServiceProviderEnum.RC2;
                    break;
                case ServiceProviderEnum.DES:
                    mCryptoService = new DESCryptoServiceProvider();
                    mAlgorithm = ServiceProviderEnum.DES;
                    break;
                case ServiceProviderEnum.TripleDES:
                    mCryptoService = new TripleDESCryptoServiceProvider();
                    mAlgorithm = ServiceProviderEnum.TripleDES;
                    break;
            }
            mCryptoService.Mode = CipherMode.CBC;
        }
开发者ID:halcwb,项目名称:GenPres_Old,代码行数:25,代码来源:Crypt.cs

示例15: Main

 public Main(
      RemoteHooking.IContext InContext,
      String InChannelName)
 {
     // connect to host...
     Interface = RemoteHooking.IpcConnectClient<FileMonInterface>(InChannelName);
     myChannelName = InChannelName;
     aes = new AesCryptoServiceProvider();
     dataIgnition = new byte[MAX_BLOCK_SIZE];
     for (int i = 0; i < MAX_BLOCK_SIZE; i++)
     {
         dataIgnition[i] = (byte)i;
     }
     dataToEncrypt = new byte[MAX_BLOCK_SIZE];
     OutputDebugString(Encoding.ASCII.GetString(dataIgnition));
     Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(Password, Encoding.ASCII.GetBytes(Salt));
     aes.Key = key.GetBytes(aes.KeySize / 8);
     aes.Padding = PaddingMode.Zeros;
     byte[] IV = Interface.GetIV();
     if (IV == null)
     {
         aes.GenerateIV();
         Interface.SaveIV(aes.IV);
     }
     aes.Mode = CipherMode.CFB;
     Interface.Ping();
 }
开发者ID:tqtam,项目名称:LUANVAN,代码行数:27,代码来源:InjectDLL.cs


注:本文中的System.Security.Cryptography.SymmetricAlgorithm类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。