本文整理汇总了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;
}
}
示例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);
}
示例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);
}
示例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);
}
示例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;
}
示例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);
}
示例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;
}
示例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);
}
示例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);
}
}
示例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;
}
示例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);
}
示例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);
}
示例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;
}
示例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();
}
示例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);
}