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

C# DESCryptoServiceProvider类代码示例

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

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



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

示例1: Crypt

	public static string Crypt(this string text)
	{
		try
		{
			//SymmetricAlgorithm algorithm = DES.Create();
			DES des = new DESCryptoServiceProvider();
			des.Mode = CipherMode.ECB;
			des.Padding = PaddingMode.PKCS7;
			des.Key = key;
			des.IV = iv;
			//ICryptoTransform transform = algorithm.CreateEncryptor(key, iv);
			ICryptoTransform transform = des.CreateEncryptor(key,iv);
			byte[] inputbuffer = Encoding.Unicode.GetBytes(text);
			byte[] outputBuffer;
			try
			{
				outputBuffer = transform.TransformFinalBlock(inputbuffer, 0, inputbuffer.Length);
			}
			catch(Exception e)
			{
				inputbuffer = Encoding.UTF8.GetBytes(text);
				outputBuffer = transform.TransformFinalBlock(inputbuffer, 0, inputbuffer.Length);
			}
			string asdfs = Convert.ToBase64String(outputBuffer);
			return asdfs;
		}
		catch (Exception e)
		{
			return "";
		}

	}
开发者ID:seokmin,项目名称:NetWorkProject-MultiChat,代码行数:32,代码来源:Encrypter.cs


示例2: Main

	static int Main ()
	{
		string filename = Path.Combine (AppDomain.CurrentDomain.BaseDirectory,
			"encrypt.tmp");
		string data = "this is sensitive data";

		DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
		des.GenerateIV ();
		des.GenerateKey ();

		// -----------  WRITING ENCRYPTED SERIALIZED DATA ------------------
		Stream stream = new FileStream (filename, FileMode.Create, FileAccess.Write);
		stream = new CryptoStream (stream, des.CreateEncryptor (), CryptoStreamMode.Write);
		BinaryFormatter bformatter = new BinaryFormatter ();
		bformatter.Serialize (stream, data);
		stream.Close ();

		stream = null;
		bformatter = null;
		data = string.Empty;

		// -----------  READING ENCRYPTED SERIALIZED DATA ------------------
		stream = new FileStream (filename, FileMode.Open, FileAccess.Read);
		stream = new CryptoStream (stream, des.CreateDecryptor (), CryptoStreamMode.Read);
		bformatter = new BinaryFormatter ();
		data = (string) bformatter.Deserialize (stream);
		stream.Close ();

		//----------- CHECK RESULTS ----------------
		if (data != "this is sensitive data")
			return 1;

		return 0;
	}
开发者ID:mono,项目名称:gert,代码行数:34,代码来源:test.cs


示例3: DecryptFile

 /// <summary>
 /// 对文件内容进行DES解密
 /// </summary>
 /// <param name="sourceFile">待解密的文件绝对路径</param>
 /// <param name="destFile">解密后的文件保存的绝对路径</param>
 public static void DecryptFile(string sourceFile, string destFile)
 {
     if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);
     byte[] btKey = Encoding.Default.GetBytes(key);
     byte[] btIV = Encoding.Default.GetBytes(iv);
     DESCryptoServiceProvider des = new DESCryptoServiceProvider();
     byte[] btFile = File.ReadAllBytes(sourceFile);
     using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
     {
         try
         {
             using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
             {
                 cs.Write(btFile, 0, btFile.Length);
                 cs.FlushFinalBlock();
             }
         }
         catch
         {
             throw;
         }
         finally
         {
             fs.Close();
         }
     }
 }
开发者ID:RushHang,项目名称:H_DataAssembly,代码行数:32,代码来源:DES.cs


示例4: TryDecrypt

    public bool TryDecrypt(string encryptedItem, string password, out string decryptedItem)
    {
        if (string.IsNullOrEmpty (encryptedItem) ||
            string.IsNullOrEmpty (password)) {
            decryptedItem = "";
            return false;
        }

        try {
            byte[] cipherBytes = Convert.FromBase64String (encryptedItem);

            using (var memoryStream = new MemoryStream(cipherBytes)) {
                var des = new DESCryptoServiceProvider ();

                byte[] iv = new byte[8];
                memoryStream.Read (iv, 0, iv.Length);

                var rfc2898DeriveBytes = new Rfc2898DeriveBytes (password, iv, ITERATIONS);

                byte[] key = rfc2898DeriveBytes.GetBytes (8);

                using (var cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(key, iv), CryptoStreamMode.Read))
                using (var streamReader = new StreamReader(cryptoStream)) {
                    decryptedItem = streamReader.ReadToEnd ();
                    return true;
                }
            }
        } catch (Exception ex) {
            Logger.instance.Log (new Logger.Message (this, ex));

            decryptedItem = "";
            return false;
        }
    }
开发者ID:GandhiGames,项目名称:encrypted_storage_unity,代码行数:34,代码来源:DESEncryption.cs


示例5: DeCryption

    private string DeCryption(string content, string sKey, string sIV)
    {
        try
        {
            byte[] inputByteArray = Convert.FromBase64String(content);
            using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider())
            {
                //byte[] inputByteArray = Convert.FromBase64String(content);
                desProvider.Key = System.Text.Encoding.ASCII.GetBytes(sKey);
                desProvider.IV = System.Text.Encoding.ASCII.GetBytes(sIV);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms,desProvider.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    //cs.Clear();
                    cs.Write(inputByteArray,0,inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }

                string str = System.Text.Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return str;
            }
        }
        catch
        {
            return "String Not Base64 Encode!!!";
        }
    }
开发者ID:IanWendy,项目名称:IanTools,代码行数:29,代码来源:CryptionManager.cs


示例6: Load

	public static SaveData Load()
	{
		if (!File.Exists("save01.sav"))
			return null;
			
		SaveData data = null;
		using(Stream stream = File.Open("save01.sav", FileMode.Open)) {
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			binaryFormatter.Binder = new VersionDeserializationBinder(); 
			
			byte[] byKey = Encoding.UTF8.GetBytes(strEncrypt.Substring(0, 8));
			using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) {
				using (Stream cryptoStream = new CryptoStream(stream, des.CreateDecryptor(byKey, dv), CryptoStreamMode.Read))
				{
					Log.debug("Save", "Loading...");
					try {
						data = (SaveData) binaryFormatter.Deserialize(cryptoStream);
					} catch (Exception e) {
						Log.debug("Save", ""+e);
						return new SaveData();
					}
					Log.debug("Save", "Loaded (level={0})", data.level);
					cryptoStream.Close();
				}
			}
			stream.Close();
		}
		return data;
	}
开发者ID:Niklas83,项目名称:UnityGame1,代码行数:29,代码来源:SaveData.cs


示例7: Encrypt

    public string Encrypt(string item, string password)
    {
        if (item == null) {
            throw new ArgumentNullException ("item");
        }

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

        var des = new DESCryptoServiceProvider ();

        des.GenerateIV ();

        var rfc2898DeriveBytes = new Rfc2898DeriveBytes (password, des.IV, ITERATIONS);

        byte[] key = rfc2898DeriveBytes.GetBytes (8);

        using (var memoryStream = new MemoryStream()) {
            using (var cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(key, des.IV), CryptoStreamMode.Write)) {
                memoryStream.Write (des.IV, 0, des.IV.Length);

                byte[] bytes = Encoding.UTF8.GetBytes (item);

                cryptoStream.Write (bytes, 0, bytes.Length);
                cryptoStream.FlushFinalBlock ();

                return Convert.ToBase64String (memoryStream.ToArray ());
            }
        }
    }
开发者ID:GandhiGames,项目名称:encrypted_storage_unity,代码行数:31,代码来源:DESEncryption.cs


示例8: Encrypt

    public static string Encrypt(string originalString, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            // 把字符串放到byte数组中
            byte[] inputByteArray = Encoding.Default.GetBytes(originalString);

            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  //原文使用ASCIIEncoding.ASCII方法的

                                                            //GetBytes方法
            MemoryStream ms = new MemoryStream();         //使得输入密码必须输入英文文本
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();

            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            ret.ToString();
            return ret.ToString();
    }
开发者ID:qq5013,项目名称:JXNG,代码行数:25,代码来源:CookiesHelper.cs


示例9: Decrypt

    //cmThe function used to decrypt the text
    private static string Decrypt(string strText, string SaltKey)
    {
        byte[] byKey = { };
        byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
        byte[] inputByteArray = new byte[strText.Length + 1];

        try
        {
            byKey = System.Text.Encoding.UTF8.GetBytes(SaltKey.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(strText);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            System.Text.Encoding encoding = System.Text.Encoding.UTF8;

            return encoding.GetString(ms.ToArray());

        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
开发者ID:Vinhbaba,项目名称:dskfeorfqlhvsea,代码行数:27,代码来源:Cipher.cs


示例10: Decrypt

    public static string Decrypt(string originalString, string sKey)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();

        byte[] inputByteArray = new byte[originalString.Length / 2];
        for (int x = 0; x < originalString.Length / 2; x++)
        {
            int i = (Convert.ToInt32(originalString.Substring(x * 2, 2), 16));
            inputByteArray[x] = (byte)i;
        }

        //建立加密对象的密钥和偏移量,此值重要,不能修改

        des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);

        des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();

        //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
        StringBuilder ret = new StringBuilder();

        return System.Text.Encoding.Default.GetString(ms.ToArray());
    }
开发者ID:qq5013,项目名称:JXNG,代码行数:27,代码来源:CookiesHelper.cs


示例11: Decode

        /// <summary>
        /// 对数据进行解密
        /// </summary>
        /// <param name="decryptstring">需要解密的数据</param>
        /// <returns></returns>
        public static string Decode(string data)
        {
            byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
            byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

            byte[] byEnc;
            try
            {
                byEnc = Convert.FromBase64String(data);
            }
            catch
            {
                return null;
            }

            try
            {
                DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream(byEnc);
                CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cst);
                return sr.ReadToEnd();
            }
            catch
            {
                return null;
            }

        }
开发者ID:maanshancss,项目名称:ClassLibrary,代码行数:34,代码来源:1438759115$SoftDog.cs


示例12: EncryptData

	private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
	 {
	     //Create the file streams to handle the input and output files.
	     FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
	     FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
	     fout.SetLength(0);

	     //Create variables to help with read and write.
	     byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
	     long rdlen = 0;              //This is the total number of bytes written.
	     long totlen = fin.Length;    //This is the total length of the input file.
	     int len;                     //This is the number of bytes to be written at a time.

	     SymmetricAlgorithm des = new DESCryptoServiceProvider();
         des.Padding = PaddingMode.PKCS7;
	     CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);

	     Console.WriteLine("Encrypting...");

	     //Read from the input file, then encrypt and write to the output file.
	     while(rdlen < totlen)
	     {
	         len = fin.Read(bin, 0, 100);
	         encStream.Write(bin, 0, len);
	         rdlen = rdlen + len;
             Console.WriteLine("{0} bytes processed", rdlen);
         }
	     encStream.Close();
    }
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:29,代码来源:EncDecFileScn.cs


示例13: Decrypt

 public static string Decrypt(string stringToDecrypt)
 {
     string sEncryptionKey = "thuynnxkey";
     byte[] key = { };
     byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
     byte[] inputByteArray = new byte[stringToDecrypt.Length];
     try
     {
         key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8));
         DESCryptoServiceProvider des = new DESCryptoServiceProvider();
         inputByteArray = Convert.FromBase64String(stringToDecrypt);
         MemoryStream ms = new MemoryStream();
         CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
         cs.Write(inputByteArray, 0, inputByteArray.Length);
         cs.FlushFinalBlock();
         cs.Close();
         Encoding encoding = Encoding.UTF8;
         return encoding.GetString(ms.ToArray());
     }
     catch (System.Exception ex)
     {
         SiAuto.Main.LogString("Lỗi :", ex.ToString());
         return (string.Empty);
     }
 }
开发者ID:trantrung2608,项目名称:ilinkbay,代码行数:25,代码来源:Utils.cs


示例14: EncryptString

 // 加密字符串
 public string EncryptString(string sInputString, string sKey)
 {
     byte[] data = Encoding.UTF8.GetBytes(sInputString);
     var DES = new DESCryptoServiceProvider {Key = Encoding.ASCII.GetBytes(sKey), IV = Encoding.ASCII.GetBytes(sKey)};
     ICryptoTransform desencrypt = DES.CreateEncryptor();
     byte[] result = desencrypt.TransformFinalBlock(data,0,data.Length);
     return BitConverter.ToString(result);
 }
开发者ID:Makk24,项目名称:GetHtmlPage,代码行数:9,代码来源:Program1.cs


示例15: Decrypt

 public string Decrypt(string SourceData)
 {
     byte[] EncryptedDataBytes = Convert.FromBase64String(SourceData);
     MemoryStream TempStream = new MemoryStream(EncryptedDataBytes, 0, EncryptedDataBytes.Length);
     DESCryptoServiceProvider Decryptor = new DESCryptoServiceProvider();
     CryptoStream DecryptionStream = new CryptoStream(TempStream, Decryptor.CreateDecryptor(Key, Iv), CryptoStreamMode.Read);
     StreamReader AllDataReader = new StreamReader(DecryptionStream);
     return AllDataReader.ReadToEnd();
 }
开发者ID:eamelaram,项目名称:prueba,代码行数:9,代码来源:Encryptor.cs


示例16: Test

 static Boolean Test()
 {
     Byte[]  PlainText = {0, 1, 2, 3, 4, 5, 6, 7}; //, 8, 9, 10, 11, 12, 13, 14, 15};
     Byte[]  Key = {1, 1, 1, 1, 1, 1, 1, 1};
     Byte[]  IV = {0, 0, 0, 0, 0, 0, 0, 0};
     DESCryptoServiceProvider     des = new DESCryptoServiceProvider();
     des.Key = Key;
     
     return false;
 }
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:10,代码来源:WeakKeyDES.cs


示例17: DESDecrypt

 public static string DESDecrypt(string input)
 {
     byte[] bytes = Encoding.ASCII.GetBytes((string)ConfigurationSettings.AppSettings["CipherKeyProd"]);
     DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
     MemoryStream memoryStream = new MemoryStream
             (Convert.FromBase64String(input));
     CryptoStream cryptoStream = new CryptoStream(memoryStream,
         cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
     StreamReader reader = new StreamReader(cryptoStream);
     return reader.ReadToEnd();
 }
开发者ID:protonate,项目名称:csharp_sample1,代码行数:11,代码来源:FFCrypto.cs


示例18: Decrypt

    public string Decrypt(string input)
    {
        string output = "";

        DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
        MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(input));
        CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(cryptBytes, cryptBytes), CryptoStreamMode.Read);
        StreamReader reader = new StreamReader(cryptoStream);

        output = reader.ReadToEnd();
        return output;
    }
开发者ID:vatsal,项目名称:Stoocker,代码行数:12,代码来源:DBOperations.cs


示例19: DesDecrypt

    /**/
    /// <summary>
    /// 对称加密解密的密钥
    /// </summary>
    public static string Key
    {
        get
        {
            return key;
        }
        set
        {
            key = value;
        }
    }

    #endregion Properties

    #region Methods

    /**/
    /// <summary>
    /// DES解密
    /// </summary>
    /// <param name="decryptString"></param>
    /// <returns></returns>
    public static string DesDecrypt(string decryptString)
    {
        byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
        byte[] keyIV = keyBytes;
        byte[] inputByteArray = Convert.FromBase64String(decryptString);
        DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
        MemoryStream mStream = new MemoryStream();
        CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
        cStream.Write(inputByteArray, 0, inputByteArray.Length);
        cStream.FlushFinalBlock();
        return Encoding.UTF8.GetString(mStream.ToArray());
    }
开发者ID:ZH0322,项目名称:ZOUKE,代码行数:38,代码来源:DESzh.cs


示例20: Encrypt

 public string Encrypt(string SourceData)
 {
     byte[] SourceDataBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(SourceData);
     MemoryStream TempStream = new MemoryStream();
     DESCryptoServiceProvider Encryptor = new DESCryptoServiceProvider();
     CryptoStream EncryptionStream = new CryptoStream(TempStream, Encryptor.CreateEncryptor(Key, Iv), CryptoStreamMode.Write);
     EncryptionStream.Write(SourceDataBytes, 0, SourceDataBytes.Length);
     EncryptionStream.FlushFinalBlock();
     byte[] EncryptedDataBytes = TempStream.GetBuffer();
     //MsgBox(Convert.ToBase64String(EncryptedDataBytes, 0, TempStream.Length))
     return  Convert.ToBase64String(EncryptedDataBytes,0, Convert.ToInt16(TempStream.Length));
 }
开发者ID:eamelaram,项目名称:prueba,代码行数:12,代码来源:Encryptor.cs



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
C# DEV_BROADCAST_DEVICEINTERFACE类代码示例发布时间:2022-05-24
下一篇:
C# DELAYTYPE类代码示例发布时间: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