300分求帮忙,请问C#怎么做 pack "H*"的效果, 请帮忙实现以下功能

2002pine 2014-07-10 10:00:09
不会perl,我要做一个加密的程序。

或者大家给我一个C#的 请帮忙实现以下功能的



$challenge='27c81c82d52252c2fd0567e859df3c31';
$portal_secret=‘ABCD’;

$ascii_key = pack "H*", $portal_secret;
$login_params='res=success'+'&challenge=$challenge'
my $digest = `echo -n "$login_params" | openssl dgst -sha1 -hmac "$ascii_key"`;
$digest =~ s/[\n\r\s]+$//;
$digest= substr($digest,-40,40);


我用的是,可惜结果不样,请高手帮忙
public static string HashUTF8(string keyStr,string sourceStr)

{
byte[] key = System.Text.Encoding.UTF8.GetBytes(keyStr);
HMACSHA1 hmac = new HMACSHA1(key);
// Convert the input string to a byte array and compute the hash.
byte[] data = hmac.ComputeHash(Encoding.UTF8.GetBytes(sourceStr));

// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();

// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}

// Return the hexadecimal string.
return sBuilder.ToString();
}
...全文
214 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
2002pine 2014-07-12
  • 打赏
  • 举报
回复
还是一样的,你那个packh 函数和我的是一样的,所以结果和我的是一样 但与perl执行的不样,
2002pine 2014-07-11
  • 打赏
  • 举报
回复
顶顶顶顶顶顶
Cnwanglin 2014-07-11
  • 打赏
  • 举报
回复
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Security.Cryptography; using System.Security; namespace 9989889 { class Program { public static byte[] PackH(string hex) { if ((hex.Length % 2) == 1) hex += '0'; byte[] bytes = new byte[hex.Length / 2]; for (int i = 0; i < hex.Length; i += 2) { bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16); } return bytes; } public static string Encode(string keyStr, string sourceStr) { byte[] key = PackH(keyStr); HMACSHA1 hmac = new HMACSHA1(key); // Convert the input string to a byte array and compute the hash. byte[] data = hmac.ComputeHash(Encoding.ASCII.GetBytes(sourceStr)); // Create a new Stringbuilder to collect the bytes // and create a string. StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } return sBuilder.ToString(); } static void Main(string[] args) { string challenge = "27c81c82d52252c2fd0567e859df3c31"; string login_params = "res=success&challenge={0}"; login_params = string.Format(login_params, challenge); //string portal_secret = "AirTight"; string portal_secret = "ABCD"; string digest = Encode(portal_secret, login_params); } } }
Cnwanglin 2014-07-11
  • 打赏
  • 举报
回复
PERL的参数和返回结果和C#的一样吗?
Cnwanglin 2014-07-11
  • 打赏
  • 举报
回复
你有QQ吗,短我
2002pine 2014-07-10
  • 打赏
  • 举报
回复
private static byte[] strToToHexByte(string hexString) { hexString = hexString.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString += " "; byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); return returnBytes; } public static string Encode(string keyStr, string sourceStr) { byte[] key = strToToHexByte(keyStr); HMACSHA1 hmac = new HMACSHA1(key); // Convert the input string to a byte array and compute the hash. byte[] data = hmac.ComputeHash(Encoding.ASCII.GetBytes(sourceStr)); // Create a new Stringbuilder to collect the bytes // and create a string. StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } return sBuilder.ToString(); } 我认为我上面的程序已和perl的相同了,为什么得出来的结果不样,请大家帮看看 用perl跑出来的是46f058768347c2aa67311576fdbc09a6bbb70423 用c# challenge = "27c81c82d52252c2fd0567e859df3c31"; string login_params = "res=success&challenge={0}"; login_params = string.Format(login_params, challenge); //string portal_secret = "AirTight"; string portal_secret = "ABCD"; string digest = tool.Encode(portal_secret, login_params); 算出来的却是7f06024932cfd3143ed9c4adfb2fe0a58646b97e 请大家看看为什么 另

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧