怎么用java实现HMAC-SHA1
展开全部
以下代码为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>
[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>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
以下代码为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>
[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>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询