我在SUN的主页上看到一个例子
以下代码为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;
/**
* Title:
*
* Description:
*
* Copyright: Copyright (c) 2005
*
* Company:
*
* @author not attributable
* @version 1.0
*/
public class login {
public static void main(String[] args) {
login my = new login();
my.run();
}
public void run(){
String post = "a";
initMac iMac = new initMac();
try {
byte[] rs = iMac.HmacSHA1(post);
System.out.println("加密前的信息:" + post);
System.out.println("加密后的二\u8FDB串:" + byte2hex(rs));
} catch (Exception ex) {
}
}
public String byte2hex(byte[] b)
//二行制\u8F6C字符串
{
String hs = "";
String stmp = "";
for (int n = 0; n 复制代码
|