怎么用java实现HMAC-SHA1

 我来答
蘑菇饭资讯
推荐于2016-01-03 · TA获得超过6万个赞
知道大有可为答主
回答量:1.7万
采纳率:90%
帮助的人:1.2亿
展开全部
以下代码为JAVABEAN,加密用
[PHP]
package test;

/*
* Copyright 1997-2001 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Sun Microsystems, Inc. ("Confidential Information". You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with Sun.
*/

import java.security.*;
import javax.crypto.*;

/*
* This program demonstrates how to generate a secret-key object for
* HMAC-SHA1, and initialize an HMAC-SHA1 object with it.
*/

public class initMac {
public initMac() {
}

//定义加密算法
String Algorithm = "HmacSHA1";

public byte[] HmacSHA1(String post) throws Exception {
// Generate secret key for HMAC-SHA1
KeyGenerator kg = KeyGenerator.getInstance(Algorithm);
SecretKey sk = kg.generateKey();
// Get instance of Mac object implementing HMAC-SHA1, and
// initialize it with the above secret key
Mac mac = Mac.getInstance(Algorithm);
mac.init(sk);
byte[] result = mac.doFinal(post.getBytes());

return result;
}
}
[/PHP]
以下代码为调用上面JAVABEAN的部分

[PHP]package test;

import test.initMac;

/**
* <p>Title: </p>
*
* <p>Description: </p>
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
11无敌队长
推荐于2016-04-25 · TA获得超过167个赞
知道小有建树答主
回答量:380
采纳率:50%
帮助的人:73.2万
展开全部
以下代码为JAVABEAN,加密用
[PHP]
package test;

/*
* Copyright 1997-2001 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Sun Microsystems, Inc. ("Confidential Information". You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with Sun.
*/

import java.security.*;
import javax.crypto.*;

/*
* This program demonstrates how to generate a secret-key object for
* HMAC-SHA1, and initialize an HMAC-SHA1 object with it.
*/

public class initMac {
public initMac() {
}

//定义加密算法
String Algorithm = "HmacSHA1";

public byte[] HmacSHA1(String post) throws Exception {
// Generate secret key for HMAC-SHA1
KeyGenerator kg = KeyGenerator.getInstance(Algorithm);
SecretKey sk = kg.generateKey();
// Get instance of Mac object implementing HMAC-SHA1, and
// initialize it with the above secret key
Mac mac = Mac.getInstance(Algorithm);
mac.init(sk);
byte[] result = mac.doFinal(post.getBytes());

return result;
}
}
[/PHP]
以下代码为调用上面JAVABEAN的部分

[PHP]package test;

import test.initMac;

/**
* <p>Title: </p>
*
* <p>Description: </p>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式