c java hmacsha1,java-如何在C#中生成HMAC SHA1?

Vimvq1987答案的扩展:

return hashValue.ToString();无法产生您想要/需要的输出。 您必须将数组hashValue中的字节转换为其十六进制字符串表示形式。

可以像2983042691371631631618一样简单(打印大写字母A-F),或者如果您喜欢它则更复杂一些:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Security.Cryptography;

using System.IO;

namespace ConsoleApplication1

{

class Program

{

public static string Encode(string input, byte[] key)

{

HMACSHA1 myhmacsha1 = new HMACSHA1(key);

byte[] byteArray = Encoding.ASCII.GetBytes(input);

MemoryStream stream = new MemoryStream(byteArray);

return myhmacsha1.ComputeHash(stream).Aggregate("", (s, e) => s + String.Format("{0:x2}",e), s => s );

}

static void Main(string[] args)

{

byte[] key = Encoding.ASCII.GetBytes("abcdefghijklmnopqrstuvwxyz");

string input = "";

foreach (string s in new string[] { "Marry", " had", " a", " little", " lamb" })

{

input += s;

System.Console.WriteLine( Encode(input, key) );

}

return;

}

}

}

哪个打印

3545e064fb59bc4bfc02b6e1c3d4925c898aa504

3249f4c8468d4d67f465937da05b809eaff22fdb

87baaadf5d096677f944015e53d283834eb1e943

6325376820c29a09e3ab30db000033aa71d6927d

54579b0146e2476595381d837ee38863be358213

我得到完全相同的结果

$secretKey = 'abcdefghijklmnopqrstuvwxyz';

$signatureString = '';

foreach( array('Marry',' had',' a',' little',' lamb') as $s ) {

$signatureString .= $s;

echo hash_hmac('sha1', $signatureString, $secretKey, false), "\n";

}

编辑:Dmitriy Nemykin建议以下编辑

public static string Encode(string input, byte[] key)

{

byte[] byteArray = Encoding.ASCII.GetBytes(input);

using(var myhmacsha1 = new HMACSHA1(key))

{

var hashArray = myhmacsha1.ComputeHash(byteArray);

return hashArray.Aggregate("", (s, e) => s + String.Format("{0:x2}",e), s => s );

}

}

被拒绝了。 但是正如James在对此答案的评论中所指出的那样,至少使用using语句是一个好主意。

HmacSHA1是一种加密算法,它也不是可逆的,因此不能直接进行解密操作。HmacSHA1通常用于消息认证和完整性验证。 在Java,可以使用javax.crypto包下的Mac类来实现HmacSHA1算法。下面是一个示例代码,演示如何使用HmacSHA1进行消息认证: ```java import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.Base64; public class HmacSHA1Example { public static void main(String[] args) { try { String message = "Hello, World!"; String secretKey = "MySecretKey"; Mac sha1Hmac = Mac.getInstance("HmacSHA1"); SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA1"); sha1Hmac.init(secretKeySpec); byte[] hmacBytes = sha1Hmac.doFinal(message.getBytes()); String hmacString = Base64.getEncoder().encodeToString(hmacBytes); System.out.println("Message: " + message); System.out.println("HMAC: " + hmacString); } catch (NoSuchAlgorithmException | InvalidKeyException e) { e.printStackTrace(); } } } ``` 在上面的示例,我们使用了"Hello, World!"作为消息,"MySecretKey"作为密钥。通过调用Mac类的doFinal方法,我们可以得到HmacSHA1的结果。最后,我们将结果转换为Base64编码的字符串进行输出。 需要注意的是,HmacSHA1算法也需要一个密钥来进行计算,因此在实际使用,请确保密钥的安全性。同样地,由于HmacSHA1是不可逆的,所以无法对其进行解密操作,只能进行验证操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值