|
|
导航: |
论坛 -> 网络通讯
斑竹:liumazi,sephil |
|
作者: |
denis (Denis) |
★☆☆☆☆ |
-
|
盒子活跃会员 |
|
2024/1/16 12:03:24 |
标题: |
HMACSHA1 delphi与C#不一致的问题求助 |
浏览:903 |
|
加入我的收藏 |
楼主: |
生成HMACSHA1签名,delphi与C#生成的不一样,C#的结果是正确的,不知问题出在哪
delphi: procedure TForm1.Button1Click(Sender: TObject); var UserKey, StringToSign, SignText: String; bs: TIdBytes; begin UserKey := 'r1t51acedv9cpmuswrti28dpkynut9r9'; StringToSign := 'POST /lighting/v2/push/task' + '\n' + '202401020001123' + '\n' + '012134567'; with TIdHMACSHA1.Create() do try key := ToBytes(UserKey); bs := HashValue(ToBytes(StringToSign)); finally free; end; SignText := BytesToBase64(bs); Edit2.Text := SignText; end; 结果是 3A1loGO7fBM4fxE7+03HbZCYFXM=
C#: private void button1_Click(object sender, EventArgs e) { String Key = "r1t51acedv9cpmuswrti28dpkynut9r9"; String StringToSign = "POST /lighting/v2/push/task" + "\n" + "202401020001123" + "\n" + "012134567";
String SignText = HmacSha1Sign.sign(StringToSign, Key); textBox1.Text = SignText; }
public class HmacSha1Sign { public static string sign(string text, string key) { Encoding encode = Encoding.GetEncoding("utf-8"); byte[] byteData = encode.GetBytes(text); byte[] byteKey = encode.GetBytes(key); HMACSHA1 hmac = new HMACSHA1(byteKey); CryptoStream cs = new CryptoStream(Stream.Null, hmac, CryptoStreamMode.Write); cs.Write(byteData, 0, byteData.Length); cs.Close(); return Convert.ToBase64String(hmac.Hash); } }
结果是 vwRyUi72UBEzZiHvCyGUVy8n4UU=
----------------------------------------------
- |
作者: |
|
2024/1/16 12:40:13 |
1楼: |
你调试下看看变量 UserKey Key StringToSign 在内存是否一致,通常是编码不一致导致
----------------------------------------------
[alias] co = clone --recurse-submodules up = submodule update --init --recursiveupd = pullinfo = statusrest = reset --hard懒鬼提速https://www.cctry.com/>http://qalculate.github.io/downloads.htmlhttps://www.cctry.com/
|
作者: |
denis (Denis) |
★☆☆☆☆ |
-
|
盒子活跃会员 |
|
2024/1/16 13:49:55 |
2楼: |
调试看变量 参数一致 delphi里的 HashValue 跟 C#里的 hmac.Hash 返回的结果不同
----------------------------------------------
-
|
作者: |
|
2024/1/16 14:07:38 |
3楼: |
跟踪一下delphi下的 ToBytes(UserKey); ToBytes(StringToSign)); 跟C#下的 byte[] byteData = encode.GetBytes(text); byte[] byteKey = encode.GetBytes(key); 是不是一致?
----------------------------------------------
-
|
作者: |
denis (Denis) |
★☆☆☆☆ |
-
|
盒子活跃会员 |
|
2024/1/16 14:23:34 |
4楼: |
回复3楼,是一致
----------------------------------------------
-
|
作者: |
|
2024/1/16 15:39:12 |
5楼: |
你是看的内存字节还是字符串本身? https://bbs.csdn.net/topics/380063930
----------------------------------------------
[alias] co = clone --recurse-submodules up = submodule update --init --recursiveupd = pullinfo = statusrest = reset --hard懒鬼提速https://www.cctry.com/>http://qalculate.github.io/downloads.htmlhttps://www.cctry.com/
|
作者: |
|
2024/1/16 16:21:45 |
6楼: |
uses 中添加 System.Hash, System.NetEncoding。
procedure TForm1.btn2Click(Sender: TObject); var UserKey, StringToSign, SignText: String; begin UserKey := 'r1t51acedv9cpmuswrti28dpkynut9r9'; StringToSign := 'POST /lighting/v2/push/task' + #10 + '202401020001123' + #10 + '012134567'; SignText := TNetEncoding.Base64.EncodeBytesToString(THashSHA1.GetHMACAsBytes(StringToSign, UserKey)); mmo1.Lines.Add(SignText); end;
这样两者的结果就是一样的了,你忽略了原始数据,Delphi中的字符串没有转义。
----------------------------------------------
-
|
作者: |
denis (Denis) |
★☆☆☆☆ |
-
|
盒子活跃会员 |
|
2024/1/16 16:24:26 |
7楼: |
谢谢6楼!问题解决。
----------------------------------------------
-
|
作者: |
|
2024/1/16 22:06:19 |
8楼: |
'\n' #10 原来是这个不一样
----------------------------------------------
-
|
作者: |
|
2024/1/17 12:04:31 |
9楼: |
楼上正解,换成#10
----------------------------------------------
-
|
作者: |
|
2024/1/17 13:11:17 |
10楼: |
所以楼主给我的回答是 '\n' #10
这两玩意在内存一致
----------------------------------------------
[alias] co = clone --recurse-submodules up = submodule update --init --recursiveupd = pullinfo = statusrest = reset --hard懒鬼提速https://www.cctry.com/>http://qalculate.github.io/downloads.htmlhttps://www.cctry.com/
|
作者: |
|
2024/1/17 16:08:40 |
11楼: |
delphi 换行符: #10 C# 换行符: \n
----------------------------------------------
^_^
|
|