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; } }
两者结果一致
相关推荐
pb12.5外部函数扩展pbfunc(包含HMACSHA1加密、HMACSHA256加密、文件哈希算法、字符串哈希算法、二维码、json)demo.zip
易语言HmacSHA256加密源码。@资源源码站。
详情见https://mp.csdn.net/console/editor/html/106577281
下面小编就为大家带来一篇OAuth认证协议中的HMACSHA1加密算法(实例)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
c++,MFC调用hamcsha1算法对数据进行加密,测试通过..
有些地方加密要HMACSHA1,我研究了开发了DELPHI版本的DLL,源码和资源如上
易语言HmacSHA256加密源码,HmacSHA256加密,HmacSHA256,CoUninitialize,CoInitialize
一组HmacSHA加密方法封装类,方便在编程过程中直接使用
阿里云物联网套件设备登录里的加密算法,C语言源代码 用于C语言平台的子设备登录密码计算
HmacSHA256加密.rar
易语言源码易语言HmacSHA256加密源码.rar
易语言HmacSHA256加密源码.7z
文字在线加密解密、散列/哈希、BASE64、SHA1、SHA224、SHA256、SHA384、SHA512、MD5、HmacSHA1、HmacSHA224、HmacSHA256、HmacSHA384、HmacSHA512、HmacMD5、urlencode、urldecode
HMACSHA1加密demo,C++的HMAC_SHA1加密算法源码,你自己可以做成动态库或者直接接到工程调用。
sha256_Hmac加密; sha256_Hmac解密;
hmacsha256加密算法,lua版本,接入闲聊机器人可用到。hmacsha256加密算法,lua版本,接入闲聊机器人可用到。hmacsha256加密算法,lua版本,接入闲聊机器人可用到。
压缩包内包含加密解密帮助类,包含功能:MD5 加密,加盐MD5加密,SHA 加密,SHA256 加密,SHA512 加密,HmacSha1 加密,HmacSha256 加密,HmacSha512 加密,HmacSha384 加密,HmacMd5 加密,HmacRipeMd160 加密,AES...
---HMACSHA1--- DATA: 0a7ec5f83fbeb938f155a265c431a09457c43a76 BASE64: Cn7F+D++uTjxVaJlxDGglFfEOnY= ---SHA256--- DATA: 1d6bf7fc06eb47cca7c310ef87656920e24c65a23780e8ba53b8317b5c79fd1f BASE64: HWv3/...
C#基于Hmac sha256及Hmac sha 512 做的对称加密解密