可能重复:
如何将字节数组转换为十六进制字符串,反之亦然?
我需要一种有效而快速的方法来进行这种转换.我尝试了两种不同的方法,但它们对我来说效率不高.对于具有大量数据的应用程序,是否有任何其他快速方法可以实时完成此操作?
public byte[] StringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length / 2).Select(x => Byte.Parse(hex.Substring(2 * x, 2), NumberStyles.HexNumber)).ToArray();
}
Run Code Online (Sandbox Code Playgroud)
上面的一个对我来说更有效率.
public static byte[] stringTobyte(string hexString)
{
try
{
int bytesCount = (hexString.Length) / 2;
byte[] bytes = new byte[bytesCount];
for (int x = 0; x < bytesCount; ++x)
{
bytes[x] = Convert.ToByte(hexString.Substring(x * 2, 2), 16);
}
return bytes;
}
catch
{
throw;
}
Run Code Online (Sandbox Code Playgroud) 我在.NET中得到的结果如下:
var lastRowVersion = SqlHelper.ExecuteScalar(connStr, CommandType.Text, "select
top 1 rowversion from dbo.sdb_x_orginfo order by rowversion desc");
Run Code Online (Sandbox Code Playgroud)
结果是一个字节数组[0]= 0,[1]=0,[2]=0,[3]=0,[4]=0,[5]=0,[6]=30,[7]=138,但SQL Server中的结果是0x0000000000001E8A.
如何"0x0000000000001E8A"在.NET中获得价值?
Facebook要求我创建appsecret_proof:https: //developers.facebook.com/docs/graph-api/securing-requests
我使用以下代码完成了此操作:
public string FaceBookSecret(string content, string key)
{
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(key);
byte[] messageBytes = encoding.GetBytes(content);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
return Convert.ToBase64String(hashmessage);
}
}
Run Code Online (Sandbox Code Playgroud)
一切看起来都不错,但facebook说appsecret_proof无效.我已登录,当我删除密钥时,我可以正常地完成所有操作.所以节省一些时间:
使用示例
dynamic results = client.Post("/" + model.PostAsId + "/feed", new { message = model.Message, appsecret_proof = FaceBookSecret(postAs.AuthToken, AppSecret) });
Run Code Online (Sandbox Code Playgroud)
我认为它可能与编码或其他东西有关,但说实话,我只是不知道.
我也在使用Facebook .net SDK,但这在文档中没有多少,并且似乎没有涉及与自动化,服务器端操作等有关的任何事情.
谢谢
如何在Elixir中将二进制字符串转换为十六进制字符串,反之亦然?
对于其他"主流"语言,关于此主题,有一些关于SO的帖子.甚至还有一篇SO帖子对各种C#实现进行了基准测试
我们如何在长生不老药中做到这一点?
我的实施太难看了,不能分享...... :(
为了测试我的加密算法,我提供了密钥,纯文本及其生成的密文.
键和明文是字符串
我如何将其转换为十六进制字节数组?
像这样的东西: E8E9EAEBEDEEEFF0F2F3F4F5F7F8F9FA
对于这样的事情:
byte[] key = new byte[16] { 0xE8, 0xE9, 0xEA, 0xEB, 0xED, 0xEE, 0xEF, 0xF0, 0xF2, 0xF3, 0xF4, 0xF5, 0xF7, 0xF8, 0xF9, 0xFA} ;
Run Code Online (Sandbox Code Playgroud)
提前Thanx :)
我正在寻找一种将任意长度的字节数组转换为十六进制字符串的最快方法.StackOverflow for C#已经完全回答了这个问题.可以在这里找到C++中的一些解决方案.
是否存在问题的"交钥匙"或"现成"解决方案?欢迎使用C风格的解决方案.
为什么,当我将 INT 值转换为字节和 ASCII 并返回时,我得到另一个值?
例子:
var asciiStr = new string(Encoding.ASCII.GetChars(BitConverter.GetBytes(2000)));
var intVal = BitConverter.ToInt32(Encoding.ASCII.GetBytes(asciiStr), 0);
Console.WriteLine(intVal);
// Result: 1855
Run Code Online (Sandbox Code Playgroud) 我的Win表单应用程序似乎不喜欢FormsAuthentication,我对哈希很新,所以任何转换它的帮助都会非常受欢迎.谢谢.
//Write hash
protected TextBox tbPassword;
protected Literal liHashedPassword;
{
string strHashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(tbPassword.Text, "sha1");
liHashedPassword.Text = "Hashed Password is: " + strHashedPassword;
}
//read hash
string strUserInputtedHashedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile( tbPassword.Text, "sha1");
if(strUserInputtedHashedPassword == GetUsersHashedPasswordUsingUserName(tbUserName.Text))
{
// sign-in successful
}
else
{
// sign-in failed
}
Run Code Online (Sandbox Code Playgroud) 我有一个这样的字符串:"021500010000146DE6D800000000000000003801030E9738"
我需要的是以下字节数组:02 15 00 01 00 00 14 6D E6 D8 00 00 00 00 00 00 00 00 38 01 03 0E 97 38(每对数字是相应字节中的十六进制值).
有关如何进行此转换的任何想法?谢谢!!
假设我需要在Powershell中执行此操作:
$SecurePass = Get-Content $CredPath | ConvertTo-SecureString -Key (1..16)
[String]$CleartextPass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($CredPass));
Run Code Online (Sandbox Code Playgroud)
$ CredPath的内容是一个包含ConvertFrom-SecureString -Key(1..16)输出的文件.
如何ConvertTo-SecureString -key (1..16)在C#/ .NET中完成该部分?
我知道如何创建SecureString,但我不确定应该如何处理加密.
我是否使用AES加密每个字符,或者解密字符串然后为每个字符创建一个安全字符串?
我对密码学几乎一无所知,但从我收集的内容中我可能只想使用C#调用Powershell命令.
作为参考,我在这里发现了类似的关于AES加密/解密的帖子: 在C#中使用AES加密
UPDATE
我已经回顾了Keith发布的链接,但我还面临其他未知数.DecryptStringFromBytes_Aes有三个参数:
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
Run Code Online (Sandbox Code Playgroud)
第一个参数是字节数组表示加密文本.这里的问题是,如何在字节数组中表示字符串?它应该用或不用编码表示?
byte[] ciphertext = Encoding.ASCII.GetBytes(encrypted_text);
byte[] ciphertext = Encoding.UTF8.GetBytes(encrypted_text);
byte[] ciphertext = Encoding.Unicode.GetBytes(encrypted_text);
byte[] ciphertext = new byte[encrypted_password.Length * sizeof(char)];
System.Buffer.BlockCopy(encrypted_password.ToCharArray(), 0, text, 0, text.Length);
Run Code Online (Sandbox Code Playgroud)
第二个字节数组的键应该只是一个整数数组:
byte[] key = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
Run Code Online (Sandbox Code Playgroud)
第三个字节数组是"初始化向量" - 看起来Aes.Create()调用将随机生成一个用于IV的byte [].阅读,我发现我可能需要使用相同的IV.由于ConvertFrom-SecureString和ConvertTo-SecureString能够使用简单的密钥加密/解密,因此我假设IV []可以是随机的 - 或者 - 具有静态定义. …