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


C# Text.UnicodeEncoding类代码示例

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


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

示例1: pictureBox1_Click

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog opf = new OpenFileDialog();
                opf.Title = "";
                opf.ShowDialog();
                //string fc = System.IO.File.ReadAllText(opf.FileName);
                UnicodeEncoding ByteConverter = new UnicodeEncoding();
                byte[] dataToEncrypt = ByteConverter.GetBytes(System.IO.File.ReadAllText(opf.FileName));
                byte[] encryptedData;

                using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
                {
                    encryptedData = encryptionfuncs.RSADecrypt(dataToEncrypt, RSA.ExportParameters(true), false);

                }
                System.IO.File.WriteAllBytes(opf.FileName, encryptedData);
                MessageBox.Show("File decrypted.");
            }
            catch (Exception)
            {

            }
        }
开发者ID:techspider,项目名称:spykiller,代码行数:25,代码来源:encrypt.cs

示例2: EncryptSomeText

        static void EncryptSomeText()
        {
            string dataToBeEncrypted = "My secret text!";
            Console.WriteLine("Original: {0}", dataToBeEncrypted);

            var encryptedData = Encrypt(dataToBeEncrypted);
            Console.WriteLine("Cipher data: {0}", encryptedData.Aggregate<byte, string>("", (s, b) => s += b.ToString()));

            var decryptedString = Decrypt(encryptedData);

            Console.WriteLine("Decrypted:{0}", decryptedString);

            // As you can see, you first need to convert the data you want to encrypt to a byte sequence.
            // To encrypt the data, you need only the public key.
            // You then use the private key to decrypt the data.

            // Because of this, it’s important to store the private key in a secure location.
            // If you would store it in plain text on disk or even in a nonsecure memory location,
            // your private key could be extracted and your security would be compromised.

            // The .NET Framework offers a secure location for storing asymmetric keys in a key container.
            // A key container can be specific to a user or to the whole machine.
            // This example shows how to configure an RSACryptoServiceProvider to use a key container for saving and loading the asymmetric key.

            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            byte[] dataToEncrypt = ByteConverter.GetBytes(dataToBeEncrypted);
            string containerName = "SecretContainer";
            CspParameters csp = new CspParameters() { KeyContainerName = containerName };

            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(csp))
            {
                var encryptedByteData = RSA.Encrypt(dataToEncrypt, false);
            }
        }
开发者ID:nissbran,项目名称:Training-Certifications,代码行数:34,代码来源:Program.cs

示例3: EncryptSomeText

        public static void EncryptSomeText()
        {
            //Init keys
            GeneratePublicAndPrivateKeys();

            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            byte[] dataToEncrypt = ByteConverter.GetBytes("My ultra secret message!");

            byte[] encryptedData;
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
            {
                RSA.FromXmlString(publicKeyXML);
                encryptedData = RSA.Encrypt(dataToEncrypt, false);
            }

            byte[] decryptedData;
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
            {
                RSA.FromXmlString(privateKeyXML);
                decryptedData = RSA.Decrypt(encryptedData, false);
            }

            string decryptedString = ByteConverter.GetString(decryptedData);
            Console.WriteLine(decryptedString);
        }
开发者ID:Chaek,项目名称:MCSD,代码行数:25,代码来源:RSAEncryption.cs

示例4: ConvertDataSetToXML

 //将DataSet转换为xml对象字符串
 public static string ConvertDataSetToXML(DataSet xmlDS)
 {
     MemoryStream stream = null;
     XmlTextWriter writer = null;
     try
     {
         stream = new MemoryStream();
         //从stream装载到XmlTextReader
         writer = new XmlTextWriter(stream, Encoding.Unicode);
         //用WriteXml方法写入文件.
         xmlDS.WriteXml(writer);
         int count = (int)stream.Length;
         byte[] arr = new byte[count];
         stream.Seek(0, SeekOrigin.Begin);
         stream.Read(arr, 0, count);
         UnicodeEncoding utf = new UnicodeEncoding();
         return utf.GetString(arr).Trim();
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
     finally
     {
         if (writer != null) writer.Close();
     }
 }
开发者ID:icegithub,项目名称:csharp-exercise,代码行数:28,代码来源:XmlDatasetConvert.cs

示例5: WriteToLog

        internal static void WriteToLog(SPWeb web, string message)
        {
            ASCIIEncoding enc = new ASCIIEncoding();
            UnicodeEncoding uniEncoding = new UnicodeEncoding();

            string errors = message;

            SPFile files = web.GetFile("/" + DocumentLibraryName + "/" + LogFileName);

            if (files.Exists)
            {
                byte[] fileContents = files.OpenBinary();
                string newContents = enc.GetString(fileContents) + Environment.NewLine + errors;
                files.SaveBinary(enc.GetBytes(newContents));
            }
            else
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (StreamWriter sw = new StreamWriter(ms, uniEncoding))
                    {
                        sw.Write(errors);
                    }

                    SPFolder LogLibraryFolder = web.Folders[DocumentLibraryName];
                    LogLibraryFolder.Files.Add(LogFileName, ms.ToArray(), false);
                }
            }

            web.Update();
        }
开发者ID:JoJo777,项目名称:TTK.SP,代码行数:31,代码来源:Logging.cs

示例6: GetEncoding

		static Encoding GetEncoding (XmlNode section, string att, string enc)
		{
			Encoding encoding = null;
			try {
				switch (enc.ToLower ()) {
				case "utf-16le":
				case "utf-16":
				case "ucs-2":
				case "unicode":
				case "iso-10646-ucs-2":
					encoding = new UnicodeEncoding (false, true);
					break;
				case "utf-16be":
				case "unicodefffe":
					encoding = new UnicodeEncoding (true, true);
                                        break;
				case "utf-8":
				case "unicode-1-1-utf-8":
				case "unicode-2-0-utf-8":
				case "x-unicode-1-1-utf-8":
				case "x-unicode-2-0-utf-8":
					encoding = new UTF8Encoding (false, false);
					break;
				default:
					encoding = Encoding.GetEncoding (enc);
					break;
				}
			} catch {
				EncodingFailed (section, att, enc);
				encoding = new UTF8Encoding (false, false);
			}

			return encoding;
		}
开发者ID:calumjiao,项目名称:Mono-Class-Libraries,代码行数:34,代码来源:GlobalizationConfigurationHandler.cs

示例7: SendMessage

        public void SendMessage(string text)
        {
            var clientStream = _tcpClient.GetStream();
            var message = Encoding.UTF8.GetBytes(text);
            while (true)
            {
                int bytesRead;
                try
                {
                    bytesRead = clientStream.Read(message, 0, Size);
                }
                catch
                {
                    break;
                }

                if (bytesRead == 0)
                {
                    break;
                }

                var encoder = new UnicodeEncoding();
                var msg = encoder.GetString(message, 0, bytesRead);
                SentMessageEvent(this, new ClientSentMessageEventArgs(Name, msg));

                var result = Action(msg);
                Echo(result, encoder, clientStream);
            }
        }
开发者ID:SeriousVitamin,项目名称:Sample-Client-Server-Application,代码行数:29,代码来源:ClientHandler.cs

示例8: HashEncoding

        /// <summary>
        /// 哈希加密一个字符串
        /// </summary>
        /// <param name="Security"></param>
        /// <returns></returns>
        public static string HashEncoding(string Security)
        {
            byte[] Value;
            SHA512Managed Arithmetic = null;
            try
            {
                UnicodeEncoding Code = new UnicodeEncoding();
                byte[] Message = Code.GetBytes(Security);

                Arithmetic = new SHA512Managed();
                Value = Arithmetic.ComputeHash(Message);
                Security = "";
                foreach (byte o in Value)
                {
                    Security += (int)o + "O";
                }

                return Security;
            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                if (Arithmetic!=null)
                {
                    Arithmetic.Clear();
                }
            }
        }
开发者ID:jimlly,项目名称:SMSProject,代码行数:37,代码来源:HashEncode.cs

示例9: HashPassword

        public static string HashPassword(string password, string saltValue)
        {
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("Password is null");

            var encoding = new UnicodeEncoding();
            var hash = new SHA256CryptoServiceProvider();

            if (saltValue == null)
            {
                saltValue = GenerateSaltValue();
            }

            byte[] binarySaltValue = Convert.FromBase64String(saltValue);
            byte[] valueToHash = new byte[SaltValueSize + encoding.GetByteCount(password)];
            byte[] binaryPassword = encoding.GetBytes(password);

            binarySaltValue.CopyTo(valueToHash, 0);
            binaryPassword.CopyTo(valueToHash, SaltValueSize);

            byte[] hashValue = hash.ComputeHash(valueToHash);
            var hashedPassword = String.Empty;
            foreach (byte hexdigit in hashValue)
            {
                hashedPassword += hexdigit.ToString("X2", CultureInfo.InvariantCulture.NumberFormat);
            }

            return hashedPassword;
        }
开发者ID:Kirichenko-sanek,项目名称:SocialNetwork,代码行数:28,代码来源:PasswornHashing.cs

示例10: EncodingDetecingInputStream

		static EncodingDetecingInputStream ()
		{
			StrictUTF8 = new UTF8Encoding (false, true);
			Strict1234UTF32 = new UTF32Encoding (true, false, true);
			StrictBigEndianUTF16 = new UnicodeEncoding (true, false, true);
			StrictUTF16 = new UnicodeEncoding (false, false, true);
		}
开发者ID:nickchal,项目名称:pash,代码行数:7,代码来源:JsonReader.cs

示例11: DecryptFile

		public static void DecryptFile(string inputFile, string outputFile, string password)
		{
			{
				var unicodeEncoding = new UnicodeEncoding();
				byte[] key = unicodeEncoding.GetBytes(FormatPassword(password));

				if (!File.Exists(inputFile))
				{
					File.Create(inputFile);
				}
				FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
				var rijndaelManaged = new RijndaelManaged();
				var cryptoStream = new CryptoStream(fsCrypt, rijndaelManaged.CreateDecryptor(key, key), CryptoStreamMode.Read);
				var fileStream = new FileStream(outputFile, FileMode.Create);

				try
				{
					int data;
					while ((data = cryptoStream.ReadByte()) != -1)
						fileStream.WriteByte((byte)data);
				}
				catch { throw; }
				finally
				{
					fsCrypt.Close();
					fileStream.Close();
					cryptoStream.Close();
				}
			}
		}
开发者ID:xbadcode,项目名称:Rubezh,代码行数:30,代码来源:EncryptHelper.cs

示例12: PosTest3

 public void PosTest3()
 {
     UnicodeEncoding uEncoding = new UnicodeEncoding();
     bool actualValue;
     actualValue = uEncoding.Equals(new TimeSpan());
     Assert.False(actualValue);
 }
开发者ID:johnhhm,项目名称:corefx,代码行数:7,代码来源:UnicodeEncodingEquals.cs

示例13: HashAndSignString

        /// <summary>
        /// Hash the data and generate signature
        /// </summary>
        /// <param name="dataToSign"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string HashAndSignString(string dataToSign, RSAParameters key)
        {
            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            byte[] signatureBytes = HashAndSignBytes(ByteConverter.GetBytes(dataToSign), key);

            return ByteConverter.GetString(signatureBytes);
        }
开发者ID:akhleshg,项目名称:datastore-service,代码行数:13,代码来源:CryptographyHelper.cs

示例14: PosTest1

 public void PosTest1()
 {
     UnicodeEncoding uEncoding = new UnicodeEncoding();
     int actualValue;
     actualValue = uEncoding.GetByteCount("");
     Assert.Equal(0, actualValue);
 }
开发者ID:johnhhm,项目名称:corefx,代码行数:7,代码来源:UnicodeEncodingGetByteCount2.cs

示例15: StringToByteArray

 private static byte[] StringToByteArray(string str)
 {
     if (null == str)
         return null;
     System.Text.UnicodeEncoding enc = new System.Text.UnicodeEncoding();
     return enc.GetBytes(str);
 }
开发者ID:netintellect,项目名称:NetOffice,代码行数:7,代码来源:ChangeBinaryDialog.cs


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