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