當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。