导航:
论坛 -> DELPHI技术
斑竹:liumazi,sephil
作者:
2020/9/16 14:55:45
标题:
Delphi HMACSHA256有换行的字符时不一样问题
浏览:1494
加入我的收藏
楼主:
大佬们好!!! 加密的函数用的是这个 uses IdGlobal, IdHashSHA, IdHMAC, IdHMACSHA1, IdSSLOpenSSL; function CalculateHMACSHA256(const value, salt: String): String; var hmac: TIdHMACSHA256; hash: TIdBytes; begin LoadOpenSSLLibrary; if not TIdHashSHA256.IsAvailable then raise Exception.Create('SHA256 hashing is not available!'); hmac := TIdHMACSHA256.Create; try hmac.Key := IndyTextEncoding_UTF8.GetBytes(salt); hash := hmac.HashValue(IndyTextEncoding_UTF8.GetBytes(value)); Result := ToHex(hash); finally hmac.Free; end; end; 在没换行的情况下,它加密出来的和在线加密的一样,但是只要有换行,它加密出来的就不一样了,我小白,不知道哪里出现了问题,请大佬帮忙,谢谢。
----------------------------------------------
-
作者:
2020/9/16 15:31:55
1楼:
把换行符去掉嘛
----------------------------------------------
-
作者:
2020/9/16 15:54:30
2楼:
但是需要加密的字符就有换行在里面
----------------------------------------------
-
作者:
2020/9/16 16:10:14
3楼:
这其实是个小常识。Web网页上和Linux一样,用的是/n Delphi的Memo,按Windows的规则,用的是/r/n,所以,你只要在加密前替换下 /r/n -> /n 就可以和网页一致了。
----------------------------------------------
-
作者:
2020/9/16 16:11:13
4楼:
一样不一样总有一个对比吧。 没有对比,怎么知道不一样。
----------------------------------------------
(C)(P)Flying Wang
作者:
2020/9/16 17:31:47
5楼:
Delphi ========== function CalculateHMACSHA256(const value, salt: String): String; var hmac: TIdHMACSHA256; hash: TIdBytes; begin LoadOpenSSLLibrary; try if not TIdHashSHA256.IsAvailable then raise Exception.Create('SHA256 hashing is not available!'); hmac := TIdHMACSHA256.Create; try hmac.Key := IndyTextEncoding_UTF8.GetBytes(salt); hash := hmac.HashValue(IndyTextEncoding_UTF8.GetBytes(value)); Result := ToHex(hash); finally hmac.Free; end; finally UnLoadOpenSSLLibrary; end; end; procedure TForm1.Button1Click(Sender: TObject); var S: string; begin S := CalculateHMACSHA256('12345'^m'67890', 'aaa'); Memo1.Text := S; end; Java ========== public void sha256() throws Exception { String s = HMACSHA256("12345\r67890", "aaa"); System.out.println(s); } public static String HMACSHA256(String data, String key) throws Exception { Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256"); sha256_HMAC.init(secret_key); byte[] array = sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8)); StringBuilder sb = new StringBuilder(); for (byte item : array) { sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3)); } return sb.toString().toUpperCase(); } 两个的结果一样,至少可以肯定算法没有问题 6DEAA3675C9EA24076D9A84887F5D96FC565810EC5661C6BE8FBFBCFBDAEDA89 你可以替换那个回车符看看,因为不同系统处理换行回车是不一样的
----------------------------------------------
-
作者:
2020/9/16 17:46:14
6楼:
发现小白的注册时间也是10年前了,都是老鸟了
----------------------------------------------
-
作者:
2020/9/16 21:52:40
7楼:
0.0
此帖子包含附件: 大小: 4,041B
----------------------------------------------
[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/
作者:
2020/9/18 11:25:11
8楼:
感谢大佬们,用AdjustLineBreaks可以,stringreplace是不行的。
----------------------------------------------
-