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


C# UTF8Encoding.GetBytes方法代码示例

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


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

示例1: Encode

        protected string Encode(string value)
        {
            UTF8Encoding encoding = new UTF8Encoding();

            switch (_DataType.Encoding)
            {
                case "BASE64": return Convert.ToBase64String(encoding.GetBytes(value));
                case "7BIT":
                case "8BIT":                
                    value = Regex.Replace(value, @"[^\r]\n", "\r\n");
                    value = Regex.Replace(value, @"\r[^\n]", "\r\n");

                    bool is7Bit = _DataType.Encoding.Equals("7BIT");

                    List<byte> data = new List<byte>(encoding.GetBytes(value));
                    for (int i = data.Count - 1; i >= 0; i--)
                    {
                        if (data[i] == 0)
                            data.RemoveAt(i);

                        if (is7Bit && data[i] > 127)
                            data.RemoveAt(i);
                    }

                    return encoding.GetString(data.ToArray());
                default:
                    return value;
            }
        }
开发者ID:xxjeng,项目名称:nuxleus,代码行数:29,代码来源:EncodableDataTypeSerializer.cs

示例2: EncryptString

 public string EncryptString(string message)
 {
     byte[] results;
     var utf8 = new UTF8Encoding();
     var hashProvider = new MD5CryptoServiceProvider();
     var tdesKey = hashProvider.ComputeHash(utf8.GetBytes(secrectKey));
     var tdesAlgorithm = new TripleDESCryptoServiceProvider
     {
         Key = tdesKey,
         Mode = CipherMode.ECB,
         Padding = PaddingMode.PKCS7
     };
     var dataToEncrypt = utf8.GetBytes(message);
     try
     {
         var encryptor = tdesAlgorithm.CreateEncryptor();
         results = encryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
     }
     finally
     {
         tdesAlgorithm.Clear();
         hashProvider.Clear();
     }
     return Convert.ToBase64String(results);
 }
开发者ID:TanmoyMajumdar,项目名称:MirrorTest,代码行数:25,代码来源:CryptographyHelper.cs

示例3: ValidateDdexWithXmlFile

        public void ValidateDdexWithXmlFile()
        {
            // Arrange
            var validator = new Mock<IDdexValidator>();
            var controller = new HomeController(validator.Object);
            var xmlMessage = TestSettings.Default.EmptyXml;

            var context = new Mock<ControllerContext>();
            var server = new Mock<HttpServerUtilityBase>();
            var request = new Mock<HttpRequestBase>();
            var files = new Mock<HttpFileCollectionBase>();
            var enc = new UTF8Encoding();

            var file1 = new Mock<HttpPostedFileBase>();
            file1.Setup(d => d.FileName).Returns("AudioSingle.33.xml");
            file1.Setup(d => d.InputStream).Returns(new MemoryStream(enc.GetBytes(Resources.AudioSingle_33)));
            file1.Setup(d => d.ContentLength).Returns(enc.GetBytes(Resources.AudioSingle_33).Length);

            context.Setup(x => x.HttpContext.Request).Returns(request.Object);
            context.Setup(x => x.HttpContext.Server).Returns(server.Object);
            context.Setup(x => x.HttpContext.Request.Files.Count).Returns(1);
            context.Setup(x => x.HttpContext.Request.Files[0]).Returns(file1.Object);
            controller.ControllerContext = context.Object;
            validator.Setup(x => x.Transform(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns("");

            // Act
            var result = controller.ValidateDdex("Audio Single", "10", xmlMessage) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }
开发者ID:eddleston,项目名称:DDEX-Validator,代码行数:31,代码来源:HomeControllerTest.cs

示例4: EncryptData

        public static string EncryptData(string Message)
        {
            byte[] Results;
            string passphrase = ReadCert();

            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passphrase));
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
            byte[] DataToEncrypt = UTF8.GetBytes(Message);
            try
            {
                ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            return Convert.ToBase64String(Results);
        }
开发者ID:ranjancse26,项目名称:EHealthCareProviderApp,代码行数:25,代码来源:EncryptDecryptHelper.cs

示例5: Generate

        public World Generate()
        {
            Console.WriteLine("Generating new World...");
            world.Day = 1;
            world.Width = 50;
            world.Height = 50;
            world.Tiles = GenerateMap(world.Width, world.Height);
            world.Registrations = new List<Registration>();
            world.Players = new List<Player>();
            world.Villages = new List<Village>();
            world.IPBans = new List<IPBan>();

            #if DEBUG
            System.Security.Cryptography.SHA256Managed sha256 = new System.Security.Cryptography.SHA256Managed();
            UTF8Encoding utf8Encoder = new UTF8Encoding();
            Player player = new Player();
            player.Name = "Majzlík";
            player.Login = "Majzlík";
            player.Password = sha256.ComputeHash(utf8Encoder.GetBytes("majzlik"));
            world.Players.Add(player);
            player = new Player();
            player.Name = "Setal";
            player.Login = "Setal";
            player.Password = sha256.ComputeHash(utf8Encoder.GetBytes("setal"));
            world.Players.Add(player);
            #endif
            Console.WriteLine("New World Generated");
            return world;
        }
开发者ID:bobrekjiri,项目名称:StrategyServer,代码行数:29,代码来源:WorldController.cs

示例6: GenerateSignature

        private string GenerateSignature(string httpMethod, string secret, string apiUrl, string parameters)
        {
            string encodedUrl = UrlHelper.Encode(apiUrl);
            string encodedParameters = UrlHelper.Encode(parameters);

            //generate the basestring
            string basestring = httpMethod + "&" + encodedUrl + "&" + encodedParameters;

            //hmac-sha1 encryption:
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();

            //create key (request_token can be an empty string)
            string key = consumerSecret + "&" + secret;
            byte[] keyByte = encoding.GetBytes(key);

            //create message to encrypt
            byte[] messageBytes = encoding.GetBytes(basestring);

            //encrypt message using hmac-sha1 with the provided key
            HMACSHA1 hmacsha1 = new HMACSHA1(keyByte);
            byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);

            //signature is the base64 format for the genarated hmac-sha1 hash
            string signature = System.Convert.ToBase64String(hashmessage);

            //encode the signature to make it url safe and return the encoded url
            return UrlHelper.Encode(signature);
        }
开发者ID:powerytg,项目名称:indulged-flickr,代码行数:28,代码来源:AnacondaCoreOAuthExtension.cs

示例7: GetDecryptedText

        public static string GetDecryptedText(string EncryptedString)
        {
            string sdec = "";
            try
            {
                string smsg = EncryptedString;

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                System.Text.Encoding utf = new System.Text.UTF8Encoding();

                byte[] key = utf.GetBytes("12348765");
                byte[] iv ={ 1, 2, 3, 4, 8, 7, 6, 5 };

                ICryptoTransform decryptor = des.CreateDecryptor(key, iv);

                byte[] bmsg = utf.GetBytes(smsg);

                byte[] benc1 = System.Convert.FromBase64String(EncryptedString);
                byte[] bdec = decryptor.TransformFinalBlock(benc1, 0, benc1.Length);
                sdec = utf.GetString(bdec);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
            return sdec;
        }
开发者ID:guarddoggps,项目名称:xtremek,代码行数:28,代码来源:EncDec.cs

示例8: GetSignature

 private string GetSignature (string canonicalString)
 {
     Encoding ae = new UTF8Encoding();
     HMACSHA1 signature = new HMACSHA1(ae.GetBytes(m_awsSecretKey));
     string b64 = Convert.ToBase64String(signature.ComputeHash(ae.GetBytes(canonicalString.ToCharArray())));
     return HttpUtility.UrlEncode(b64);
 }
开发者ID:xxjeng,项目名称:nuxleus,代码行数:7,代码来源:HttpQueryConnection.cs

示例9: CreateSignature

		/// <summary>
		/// Creates a signature useful in making GoBucket requests. It automatically includes the secret key
		/// as the first item in the sigature, and the date.
		/// </summary>
		/// <param name="verb">VERB for this REST call</param>
		/// <param name="endpoint">The endpoint name being called</param>
		/// <param name="ingredients">Items to include to generate the signature</param>
		public string CreateSignature(string verb, string endpoint, IEnumerable<string> ingredients=null) {
			var encoding = new System.Text.UTF8Encoding();
			byte[] secretKeyBytes = encoding.GetBytes(this.secretKey);
			var algorithm = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(MacAlgorithm.HmacSha256);

			StringBuilder fullContents = new StringBuilder();
			fullContents.Append(this.secretKey);
			fullContents.Append(verb);
			fullContents.Append(DateTime.UtcNow.ToString("yyyy-MM-dd"));
			fullContents.Append(endpoint);

			if (ingredients != null) {
				foreach (string ingredient in ingredients) {
					fullContents.Append(ingredient);
				}
			}

			byte[] contentBytes = encoding.GetBytes(fullContents.ToString());

			using (CryptographicHash hasher = algorithm.CreateHash(secretKeyBytes)) {
				hasher.Append(contentBytes);
				byte[] hashedContents = hasher.GetValueAndReset();
				return Convert.ToBase64String(hashedContents);
			}
		}
开发者ID:gobucket,项目名称:gobucketcsharp,代码行数:32,代码来源:Signing.cs

示例10: read

        public static String read(byte[] cypher, string KeyString, string IVString)
        {
            var sRet = "";

            var encoding = new UTF8Encoding();
            var Key = encoding.GetBytes(KeyString);
            var IV = encoding.GetBytes(IVString);

            using (var rj = new RijndaelManaged())
            {
                try
                {
                    rj.Padding = PaddingMode.PKCS7;
                    rj.Mode = CipherMode.CBC;
                    rj.KeySize = 256;
                    rj.BlockSize = 256;
                    rj.Key = Key;
                    rj.IV = IV;
                    var ms = new MemoryStream(cypher);

                    using (var cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Read))
                    {
                        using (var sr = new StreamReader(cs))
                        {
                            sRet = sr.ReadLine();
                        }
                    }
                }
                finally
                {
                    rj.Clear();
                }
            }
            return sRet;
        }
开发者ID:neosb,项目名称:cryptoTriangular,代码行数:35,代码来源:Auth.cs

示例11: Encrypt

        public string Encrypt(string message)
        {
            byte[] results;
            UTF8Encoding utf8 = new UTF8Encoding();
            //to create the object for UTF8Encoding  class
            //TO create the object for MD5CryptoServiceProvider
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] deskey = md5.ComputeHash(utf8.GetBytes(Passphrase));
            //to convert to binary passkey
            //TO create the object for  TripleDESCryptoServiceProvider
            TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
            desalg.Key = deskey;//to  pass encode key
            desalg.Mode = CipherMode.ECB;
            desalg.Padding = PaddingMode.PKCS7;
            byte[] encryptData = utf8.GetBytes(message);
            //to convert the string to utf encoding binary

            try
            {

                //To transform the utf binary code to md5 encrypt
                ICryptoTransform encryptor = desalg.CreateEncryptor();
                results = encryptor.TransformFinalBlock(encryptData, 0, encryptData.Length);

            }
            finally
            {
                //to clear the allocated memory
                desalg.Clear();
                md5.Clear();
            }
            //to convert to 64 bit string from converted md5 algorithm binary code
            return Convert.ToBase64String(results);
        }
开发者ID:saditya90,项目名称:MvcPlugin,代码行数:34,代码来源:Helper.cs

示例12: Encrypt

        public static string Encrypt(string message)
        {
            byte[] results;
            UTF8Encoding utf8 = new UTF8Encoding();

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] deskey = md5.ComputeHash(utf8.GetBytes(passphrase));

            TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
            desalg.Key = deskey;//to  pass encode key
            desalg.Mode = CipherMode.ECB;
            desalg.Padding = PaddingMode.PKCS7;
            byte[] encrypt_data = utf8.GetBytes(message);
            try
            {
                ICryptoTransform encryptor = desalg.CreateEncryptor();
                results = encryptor.TransformFinalBlock(encrypt_data, 0, encrypt_data.Length);
            }
            finally
            {
                desalg.Clear();
                md5.Clear();
            }
            return Convert.ToBase64String(results);
        }
开发者ID:trquth,项目名称:greenhousebanner-Edit,代码行数:25,代码来源:MD5Helper.cs

示例13: SignAndEncode

 public static string SignAndEncode(string value, string sharedKey)
 {
     MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
     UTF8Encoding utf8 = new UTF8Encoding();
     string signature = BytesToHexString(md5.ComputeHash(utf8.GetBytes(sharedKey + value)));
     string hexString = BytesToHexString(utf8.GetBytes(value));
     return signature + hexString;
 }
开发者ID:ahtraddis,项目名称:HostedUIResources,代码行数:8,代码来源:BazaarvoiceUtils.cs

示例14: Hash

 public static string Hash(string key, string message)
 {
     UTF8Encoding encoding = new UTF8Encoding();
     var messageBytes = encoding.GetBytes(message);
     var md5Hasher = new HMACMD5(encoding.GetBytes(key));
     var hashBytes = md5Hasher.ComputeHash(messageBytes);
     return new string(hashBytes.SelectMany(b => b.ToString("X2")).ToArray()).ToLower();
 }
开发者ID:cembasaranoglu,项目名称:payuloyaltypoint,代码行数:8,代码来源:Helper.cs

示例15: CalculateHMAC256

        /// <summary>
        /// Calculates the HMAC256 string based on the given string and secret access key.
        /// </summary>
        /// <param name="stringToSign">The string to sign.</param>
        /// <param name="secretAccessKey">The secret access key to sign the string with.</param>
        /// <returns>The calculated HMAC256 string.</returns>
        private static string CalculateHMAC256(string stringToSign, string secretAccessKey)
        {
            UTF8Encoding encoding = new UTF8Encoding();

            HMACSHA256 hmac = new HMACSHA256(encoding.GetBytes(secretAccessKey));
            byte[] hash = hmac.ComputeHash(encoding.GetBytes(stringToSign));

            return Convert.ToBase64String(hash);
        }
开发者ID:avido,项目名称:bol.com-plaza-api-clients,代码行数:15,代码来源:Utils.cs


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