HMACSHA1加密

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "YKfuZFBpng594pRhgE81wFah";
            string src = "AccessorId";
            HMACSHA1 hmac = new HMACSHA1();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(str);
            hmac.Key = bs;
            byte[] srcb = System.Text.Encoding.UTF8.GetBytes(src);
            byte[] result = hmac.ComputeHash(srcb);
            string strResult = Convert.ToBase64String(result);
            Console.WriteLine(strResult);
            Console.ReadLine();
        }
    }
}

 java版本

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;  
import java.security.NoSuchAlgorithmException;  
  
import javax.crypto.Mac;  
import javax.crypto.spec.SecretKeySpec;

public class Util {

	private static final String HMAC_SHA1 = "HmacSHA1";     
    
    /**   
     * 生成签名数据   
     *    
     * @param data 待加密的数据
     * @param key  加密使用的key
     * 
     * return 成功返回签名串,失败返回空串
     */    
    public static String getSignature(String data,String key) {
    	
    	key = key.replace('-', '+');
    	key = key.replace('_', '/');
    	
    	byte[] keyBytes=key.getBytes();  
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA1);     
        
		try {
			Mac mac = Mac.getInstance(HMAC_SHA1);
			mac.init(signingKey);     
	        byte[] rawHmac = mac.doFinal(data.getBytes());  
	        
	        return encode64(rawHmac);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		}     
        
        return "";
    }
    
    /**
     * rawurlEncode
     * 
     * @param source
     * @return
     */
    public static String rawurlEncode(String source, String encoding) 
    		throws UnsupportedEncodingException{
		if(source == null){
			return null;
		}
		
		source = URLEncoder.encode(source, encoding);
		source = source.replace("+", "%20");
		return source;
	}
    
    /*
    private static String byteToHexString(byte ib){  
    	char[] Digit={  
    			'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'  
    	};  
    	char[] ob=new char[2];  
    	ob[0]=Digit[(ib>>>4)& 0X0f];  
    	ob[1]=Digit[ib & 0X0F];  
    	String s=new String(ob);  
    	return s;           
    }*/

    public static String encode64(byte[] raw) {
    	StringBuffer encoded = new StringBuffer();
    	for (int i = 0; i < raw.length; i += 3) {
    		encoded.append(encodeBlock(raw, i));
    	}
    	return encoded.toString();
    }

    protected static char[] encodeBlock(byte[] raw, int offset) {
    	int block = 0;
    	int slack = raw.length - offset - 1;
    	int end = (slack >= 2) ? 2 : slack;
    	for (int i = 0; i <= end; i++) {
    		byte b = raw[offset + i];
    		int neuter = (b < 0) ? b + 256 : b;
    		block += neuter << (8 * (2 - i));
    	}
    	char[] base64 = new char[4];
    	for (int i = 0; i < 4; i++) {
    		int sixbit = (block >>> (6 * (3 - i))) & 0x3f;
    		base64[i] = getChar(sixbit);
    	}
    	if (slack < 1) base64[2] = '=';
    	if (slack < 2) base64[3] = '=';
    	return base64;
    }

    protected static char getChar(int sixBit) {
    	if (sixBit >= 0 && sixBit <= 25)
    		return (char)('A' + sixBit);
    	if (sixBit >= 26 && sixBit <= 51)
    		return (char)('a' + (sixBit - 26));
    	if (sixBit >= 52 && sixBit <= 61)
    		return (char)('0' + (sixBit - 52));
    	if (sixBit == 62) return '+';
    	if (sixBit == 63) return '/';
    	return '?';
    }

    public static byte[] decode64(String base64) {
    	int pad = 0;
    	for (int i = base64.length() - 1; base64.charAt(i) == '='; i--)
    		pad++;
    	int length = base64.length() * 6 / 8 - pad;
    	byte[] raw = new byte[length];
    	int rawIndex = 0;
    	for (int i = 0; i < base64.length(); i += 4) {
    		int block = (getValue(base64.charAt(i)) << 18)
    				+ (getValue(base64.charAt(i + 1)) << 12)
    				+ (getValue(base64.charAt(i + 2)) << 6)
    				+ (getValue(base64.charAt(i + 3)));
    		for (int j = 0; j < 3 && rawIndex + j < raw.length; j++)
    			raw[rawIndex + j] = (byte)((block >> (8 * (2 - j))) & 0xff);
    		rawIndex += 3;
    	}
    	return raw;
    }

    protected static int getValue(char c) {
    	if (c >= 'A' && c <= 'Z') return c - 'A';
    	if (c >= 'a' && c <= 'z') return c - 'a' + 26;
    	if (c >= '0' && c <= '9') return c - '0' + 52;
    	if (c == '+') return 62;
    	if (c == '/') return 63;
    	if (c == '=') return 0;
    	return -1;
    }
}

 两者结果一致

你可能感兴趣的:(hmacsha1)