• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# PaddingMode类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: DecryptManaged

        private byte[] DecryptManaged(byte[] Key, byte[] Vector, byte[] Data, PaddingMode Padding = PaddingMode.Zeros)
        {
            byte[] decryptedBytes;
            int count = 0;

            using (MemoryStream stream = new MemoryStream(Data))
            {
                using (RijndaelManaged cipher = new RijndaelManaged())
                {
                    cipher.Mode = CipherMode.CBC;
                    cipher.Padding = Padding;
                    cipher.KeySize = Key.Length * 8;
                    cipher.BlockSize = Vector.Length * 8;

                    using (ICryptoTransform decryptor = cipher.CreateDecryptor(Key, Vector))
                    {
                        using (CryptoStream reader = new CryptoStream(stream, decryptor, CryptoStreamMode.Read))
                        {
                            decryptedBytes = new byte[stream.Length];
                            count = reader.Read(decryptedBytes, 0, decryptedBytes.Length);
                        }
                    }
                    cipher.Clear();
                }
            }
            return decryptedBytes;
        }
开发者ID:modulexcite,项目名称:CEX,代码行数:27,代码来源:RijndaelEquality.cs


示例2: Decrypt

        // Decrypt a byte array into a byte array using a key and an IV
        public static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV, CipherMode cipherMode, PaddingMode paddingMode)
        {
            // Create a MemoryStream that is going to accept the
            // decrypted bytes
            MemoryStream ms = new MemoryStream();

            // Create a symmetric algorithm.
            // We are going to use Rijndael because it is strong and
            // available on all platforms.
            // You can use other algorithms, to do so substitute the next
            // line with something like
            //     TripleDES alg = TripleDES.Create();

            Rijndael alg = Rijndael.Create();

            // Now set the key and the IV.
            // We need the IV (Initialization Vector) because the algorithm
            // is operating in its default
            // mode called CBC (Cipher Block Chaining). The IV is XORed with
            // the first block (8 byte)
            // of the data after it is decrypted, and then each decrypted
            // block is XORed with the previous
            // cipher block. This is done to make encryption more secure.
            // There is also a mode called ECB which does not need an IV,
            // but it is much less secure.

            alg.Mode = cipherMode;
            alg.Padding = paddingMode;
            alg.Key = Key;
            alg.IV = IV;

            // Create a CryptoStream through which we are going to be
            // pumping our data.
            // CryptoStreamMode.Write means that we are going to be
            // writing data to the stream
            // and the output will be written in the MemoryStream
            // we have provided.

            CryptoStream cs = new CryptoStream(ms,
                alg.CreateDecryptor(), CryptoStreamMode.Write);

            // Write the data and make it do the decryption
            cs.Write(cipherData, 0, cipherData.Length);

            // Close the crypto stream (or do FlushFinalBlock).
            // This will tell it that we have done our decryption
            // and there is no more data coming in,
            // and it is now a good time to remove the padding
            // and finalize the decryption process.

            cs.Close();

            // Now get the decrypted data from the MemoryStream.
            // Some people make a mistake of using GetBuffer() here,
            // which is not the right way.

            byte[] decryptedData = ms.ToArray();

            return decryptedData;
        }
开发者ID:hjmb,项目名称:allps3tools,代码行数:61,代码来源:FormMain.cs


示例3: Decrypt

 public static string Decrypt(string cipherText, string Password, CipherMode cipherMode, PaddingMode paddingMode)
 {
     byte[] cipherData = Convert.FromBase64String(cipherText);
     PasswordDeriveBytes bytes = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 110, 0x20, 0x4d, 0x65, 100, 0x76, 0x65, 100, 0x65, 0x76 });
     byte[] buffer2 = Decrypt(cipherData, bytes.GetBytes(0x20), bytes.GetBytes(0x10), cipherMode, paddingMode);
     return Encoding.Unicode.GetString(buffer2);
 }
开发者ID:Hector-Ab,项目名称:PeXploit,代码行数:7,代码来源:AESEngine.cs


示例4: Decrypt

		public static byte[] Decrypt(byte[] value, byte[] key, byte[] iv, CipherMode mode = CipherMode.CBC, PaddingMode padding = PaddingMode.None)
		{
			if (value == null || value.Length <= 0)
				throw new ArgumentNullException("value");
			if (key == null || key.Length <= 0)
				throw new ArgumentNullException("key");
			if (iv == null || iv.Length <= 0)
				throw new ArgumentNullException("iv");

			byte[] result;
			using (RijndaelManaged rijndael = new RijndaelManaged())
			{
				rijndael.Key = key;
				rijndael.IV = iv;
				rijndael.Mode = mode;
				rijndael.Padding = padding;

				ICryptoTransform transform = rijndael.CreateDecryptor(rijndael.Key, rijndael.IV);
				using (MemoryStream memory = new MemoryStream())
				using (CryptoStream crypto = new CryptoStream(memory, transform, CryptoStreamMode.Read))
				{
					crypto.Write(value, 0, value.Length);
					result = memory.ToArray();
				}
			}

			return result;
		}
开发者ID:lockflatboy,项目名称:PlayStationStorePackage,代码行数:28,代码来源:Rijndael.cs


示例5: EncryptIt

        public String EncryptIt(String s, byte[] key = null, byte[] IV = null, PaddingMode padding = PaddingMode.PKCS7)
        {
            String result;
            //magically assign key and IV if one isn't given as an argument
            key = key ?? cryptKey;
            IV = IV ?? cryptIV;
            RijndaelManaged rijn = new RijndaelManaged();
            rijn.Mode = CipherMode.CBC;
            rijn.Padding = padding;
            rijn.BlockSize = 256;

            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (ICryptoTransform encryptor = rijn.CreateEncryptor(key, IV))
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(s);
                        }
                    }
                }
                result = Convert.ToBase64String(msEncrypt.ToArray());
            }
            rijn.Clear();

            return result;
        }
开发者ID:qinmenghua,项目名称:pw-chat,代码行数:29,代码来源:dataHandler.cs


示例6: DecryptData

 /// <summary>
 /// Decrypts a byte array with a password
 /// </summary>
 /// <param name="data">Data to decrypt</param>
 /// <param name="password">Password to use</param>
 /// <param name="paddingMode">Padding mode to use</param>
 /// <returns>Decrypted byte array</returns>
 /// <exception cref="System.ArgumentNullException">
 /// data
 /// or
 /// password
 /// </exception>
 /// <exception cref="ArgumentNullException"></exception>
 public static byte[] DecryptData(byte[] data, string password, PaddingMode paddingMode)
 {
     if (data == null || data.Length == 0)
         throw new ArgumentNullException("data");
     if (password == null)
         throw new ArgumentNullException("password");
     var pdb = new PasswordDeriveBytes(password, Encoding.UTF8.GetBytes("Salt"));
     var rm = new RijndaelManaged { Padding = paddingMode };
     ICryptoTransform decryptor = rm.CreateDecryptor(pdb.GetBytes(16), pdb.GetBytes(16));
     pdb.Dispose();
     using (var msDecrypt = new MemoryStream(data))
     using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
     {
         // Decrypted bytes will always be less then encrypted bytes, so length of encrypted data will be big enough for buffer.
         byte[] fromEncrypt = new byte[data.Length];
         // Read as many bytes as possible.
         int read = csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
         if (read < fromEncrypt.Length)
         {
             // Return a byte array of proper size.
             byte[] clearBytes = new byte[read];
             Buffer.BlockCopy(fromEncrypt, 0, clearBytes, 0, read);
             return clearBytes;
         }
         return fromEncrypt;
     }
 }
开发者ID:rickyHong,项目名称:IPCameraCtl,代码行数:40,代码来源:EncDec.cs


示例7: ValidatePaddingMode_NonISO10126

        private static void ValidatePaddingMode_NonISO10126(PaddingMode paddingMode, int expectedPaddingSize, string plainTextStr, string expectedCipherStr)
        {
            Assert.True(paddingMode != PaddingMode.ISO10126, "This tests only non-ISO10126 padding");

            byte[] key = "1ed2f625c187b993256a8b3ccf9dcbfa5b44b4795c731012f70e4e64732efd5d".HexToByteArray();
            byte[] iv = "47d1e060ba3c8643f9f8b65feeda4b30".HexToByteArray();

            byte[] plainText = plainTextStr.HexToByteArray();
            byte[] expectedCipher = expectedCipherStr == null ? Array.Empty<byte>() : expectedCipherStr.HexToByteArray();

            using (Aes a = Aes.Create())
            {
                a.Key = key;
                a.IV = iv;
                a.Mode = CipherMode.CBC;
                a.Padding = paddingMode;

                byte[] cipher = a.Encrypt(plainText);

                Assert.Equal(expectedCipherStr, cipher.ByteArrayToHex());

                // decrypt it with PaddingMode.None so that we can inspect the padding manually
                a.Padding = PaddingMode.None;
                byte[] decrypted = a.Decrypt(cipher);
                ValidatePadding(decrypted, paddingMode, expectedPaddingSize);
            }
        }
开发者ID:chcosta,项目名称:corefx,代码行数:27,代码来源:PaddingModeTests.cs


示例8: Decrypt

 public static string Decrypt(EncryptionAlgorithm algorithm, CiphertextFormat format, string data, Key key,
     byte[] salt, byte[] iv, PaddingMode paddingMode)
 {
     var _d = new byte[0];
     switch (format)
     {
         case CiphertextFormat.Base64:
         {
             _d = Convert.FromBase64String(data);
             break;
         }
         case CiphertextFormat.Hex:
         {
             _d = HexToByteArray(data);
             break;
         }
         case CiphertextFormat.Url:
         {
             var encoding = new UTF8Encoding();
             _d = encoding.GetBytes(HttpUtility.UrlDecode(data));
             break;
         }
     }
     return Decrypt(algorithm, _d, key, salt, iv, paddingMode);
 }
开发者ID:blinds52,项目名称:ActiveDirectoryServices,代码行数:25,代码来源:Crypto.cs


示例9: DecryptIt

        public String DecryptIt(String s, byte[] key = null, byte[] IV = null, PaddingMode padding = PaddingMode.PKCS7)
        {
            String result;
            //magically assign key and IV if one isn't given as an argument
            key = key ?? cryptKey;
            IV = IV ?? cryptIV;
            RijndaelManaged rijn = new RijndaelManaged();
            rijn.Mode = CipherMode.CBC;
            rijn.Padding = padding;
            rijn.BlockSize = 256;

            using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(s)))
            {
                using (ICryptoTransform decryptor = rijn.CreateDecryptor(key, IV))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader swDecrypt = new StreamReader(csDecrypt))
                        {
                            result = swDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            rijn.Clear();
            return result;
        }
开发者ID:qinmenghua,项目名称:pw-chat,代码行数:27,代码来源:dataHandler.cs


示例10: buttonGenerator_Click

        private void buttonGenerator_Click(object sender, EventArgs e)
        {
            try
            {
                // Selected 'Preset'
                if (tabControl.SelectedIndex == 0)
                {
                    CurrentBlockSize = comboBoxPresets.SelectedIndex == 0 ? 128 : 256;
                    CurrentKeySize = comboBoxPresets.SelectedIndex == 0 ? 128 : 256;
                    SelectedCipher = CipherMode.CBC;
                    SelectedPadding = PaddingMode.PKCS7;
                }

                int index = tabControl.SelectedIndex;
                if ((index == 0 && comboBoxPresets.SelectedIndex == 1) ||
                    (index == 1 && radioButtonAes.Checked))
                    Build(new RijndaelManaged());
                else
                    Build(new AesManaged());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
开发者ID:lockflatboy,项目名称:AES-Builder,代码行数:25,代码来源:frmMain.cs


示例11: Create

 public static ICryptoTransform Create(PaddingMode paddingMode, BasicSymmetricCipher cipher, bool encrypting)
 {
     if (encrypting)
         return new UniversalCryptoEncryptor(paddingMode, cipher);
     else
         return new UniversalCryptoDecryptor(paddingMode, cipher);
 }
开发者ID:Czapek83,项目名称:corefx,代码行数:7,代码来源:UniversalCryptoTransform.cs


示例12: SymmetricKey

        public SymmetricKey(SymmetricAlgorithmType type, byte[] iv, byte[] key, CipherModePlus cipherMode, PaddingMode paddingMode, bool enableIVShuffle)
        {
            _type = type;
            switch (type) {
                case SymmetricAlgorithmType.None:
                    _algo = null;
                    return;
                case SymmetricAlgorithmType.Camellia:
                    _algo = new openCrypto.CamelliaManaged ();
                    break;
                case SymmetricAlgorithmType.Rijndael:
                    _algo = new openCrypto.RijndaelManaged ();
                    break;
                default:
                    throw new ArgumentOutOfRangeException ();
            }

            _algo.ModePlus = cipherMode;
            _algo.Padding = paddingMode;
            _algo.KeySize = key.Length << 3;
            _algo.BlockSize = iv.Length << 3;
            _algo.FeedbackSize = iv.Length << 3;
            _iv = iv;
            _key = key;
            _ivShuffle = enableIVShuffle;
        }
开发者ID:kazuki,项目名称:p2pncs,代码行数:26,代码来源:SymmetricKey.cs


示例13: ApplyPadding

        public static byte[] ApplyPadding(PaddingMode mode, byte[] bytes, int blockSizeInBytes)
        {
            int paddingBytesNeeded = GetPaddingBytesNeeded(mode, bytes.Length, blockSizeInBytes);
            if (paddingBytesNeeded == 0)
            {
                // sanity check
                return ByteUtilities.Clone(bytes);
            }

            byte[] output = new byte[blockSizeInBytes];
            Buffer.BlockCopy(bytes, 0, output, 0, bytes.Length);

            switch (mode)
            {
                case PaddingMode.ANSIX923:
                    ApplyAnsiX923Padding(output, paddingBytesNeeded);
                    break;
                case PaddingMode.ISO10126:
                    ApplyIso10126Padding(output, paddingBytesNeeded);
                    break;
                case PaddingMode.PKCS7:
                    ApplyPkcs7Padding(output, paddingBytesNeeded);
                    break;
                case PaddingMode.Zeros:
                    // nop
                    break;
                default:
                    throw new NotImplementedException("Padding mode not implemented");
            }

            return output;
        }
开发者ID:hartsock,项目名称:AES-Illustrated,代码行数:32,代码来源:PaddingUtilities.cs


示例14: CreateTransformCore

        private static ICryptoTransform CreateTransformCore(
            CipherMode cipherMode,
            PaddingMode paddingMode,
            byte[] key,
            byte[] iv,
            int blockSize,
            bool encrypting)
        {
            // The algorithm pointer is a static pointer, so not having any cleanup code is correct.
            IntPtr algorithm;
            switch (cipherMode)
            {
                case CipherMode.CBC:
                    algorithm = Interop.Crypto.EvpDesCbc();
                    break;
                case CipherMode.ECB:
                    algorithm = Interop.Crypto.EvpDesEcb();
                    break;
                default:
                    throw new NotSupportedException();
            }

            BasicSymmetricCipher cipher = new OpenSslCipher(algorithm, cipherMode, blockSize, key, 0, iv, encrypting);
            return UniversalCryptoTransform.Create(paddingMode, cipher, encrypting);
        }
开发者ID:dotnet,项目名称:corefx,代码行数:25,代码来源:DesImplementation.Unix.cs


示例15: RijndaelDecryptionCfbTransform

 public RijndaelDecryptionCfbTransform(Rijndael rijndael, int feedbackSizeInBits, byte[] initializationVector,
                                       PaddingMode paddingMode)
     : base(rijndael, paddingMode)
 {
     _FeedbackSizeInBytes = feedbackSizeInBits/Constants.BitsPerByte;
     _FeedbackIterations = rijndael.BlockSize/feedbackSizeInBits;
     _LastVector = ByteUtilities.Clone(initializationVector);
 }
开发者ID:hartsock,项目名称:AES-Illustrated,代码行数:8,代码来源:RijndaelDecryptionCfbTransform.cs


示例16: Encrypt

		private byte[] Encrypt (SymmetricAlgorithm algo, PaddingMode padding, byte[] data) 
		{
			algo.IV = new byte [algo.BlockSize >> 3];
			algo.Mode = CipherMode.CBC;
			algo.Padding = padding;
			ICryptoTransform ct = algo.CreateEncryptor ();
			return ct.TransformFinalBlock (data, 0, data.Length);
		}
开发者ID:KonajuGames,项目名称:SharpLang,代码行数:8,代码来源:PaddingModeTest.cs


示例17: CapiSymmetricAlgorithm

 public CapiSymmetricAlgorithm(int blockSize, int feedbackSize, SafeCspHandle provider, SafeCapiKeyHandle key, byte[] iv, CipherMode cipherMode, PaddingMode paddingMode, EncryptionMode encryptionMode)
 {
     this.m_blockSize = blockSize;
     this.m_encryptionMode = encryptionMode;
     this.m_paddingMode = paddingMode;
     this.m_provider = provider.Duplicate();
     this.m_key = SetupKey(key, ProcessIV(iv, blockSize, cipherMode), cipherMode, feedbackSize);
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:8,代码来源:CapiSymmetricAlgorithm.cs


示例18: SymmetricAlgorithm

 /// <summary>
 /// Initializes a new instance of the SymmetricAlgorithm class.
 /// </summary>
 /// <param name="session">The cryptoki session context for which the symmectric algorithm will execute.</param>
 /// <param name="ownsSession">true if the session should be closed by this base class, false otherwise.</param>
 protected SymmetricAlgorithm(Session session, bool ownsSession)
     : base(session, ownsSession)
 {
     // Default to cipher block chaining (CipherMode.CBC) and
     // PKCS-style padding (pad n bytes with value n)
     ModeValue = CipherMode.CBC;
     PaddingValue  = PaddingMode.PKCS7;
 }
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:13,代码来源:SymmetricAlgorithm.cs


示例19: HwAes

 public HwAes(Connection connection, byte[] Key, int KeySize, CipherMode cipherMode, PaddingMode padding)
 {
     this.AES = new AesCryptoServiceProvider();
     this.AES.Padding = padding;
     this.AES.Mode = cipherMode;
     this.AES.KeySize = KeySize;
     this.IvConfuser = new DataConfuser(connection.PrivateSeed, 16);
     ApplyKey(Key);
 }
开发者ID:PavilionVI,项目名称:SecureSocketProtocol,代码行数:9,代码来源:HwAes.cs


示例20: GetPaddingBytesNeeded

        public static int GetPaddingBytesNeeded(PaddingMode mode, int byteLength, int blockSizeInBytes)
        {
            if (mode == PaddingMode.None)
            {
                return 0;
            }

            return blockSizeInBytes - byteLength;
        }
开发者ID:hartsock,项目名称:AES-Illustrated,代码行数:9,代码来源:PaddingUtilities.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Paddle类代码示例发布时间:2022-05-24
下一篇:
C# Padding类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap