onvif中的ws |
您所在的位置:网站首页 › token算法加密 › onvif中的ws |
https://stackoverflow.com/questions/25262909/c-sharp-encoding-wsse-security-password-digest-with-sha-1-and-base64-not-matchin
onvif中的ws-username token 加密算法 Password_Digest = Base64 ( SHA-1 ( nonce + created + password ) ) 编码为utf-8 注意这里的加密不是使用字符串进行加密,而是字节数组;
public class WsseHelper { /// /// 获取加密的字节数组 /// /// /// /// /// private static byte[] buildBytes(string nonce, string createdString, string basedPassword) { byte[] nonceBytes = System.Convert.FromBase64String(nonce); byte[] time = Encoding.UTF8.GetBytes(createdString); byte[] pwd = Encoding.UTF8.GetBytes(basedPassword); byte[] operand = new byte[nonceBytes.Length + time.Length + pwd.Length]; Array.Copy(nonceBytes, operand, nonceBytes.Length); Array.Copy(time, 0, operand, nonceBytes.Length, time.Length); Array.Copy(pwd, 0, operand, nonceBytes.Length + time.Length, pwd.Length); return operand; } /// /// 计算指定字节数组的哈希值。 /// /// /// public static byte[] SHAOneHash(byte[] data) { using (SHA1Managed sha1 = new SHA1Managed()) { var hash = sha1.ComputeHash(data); return hash; } } /// /// 获取加密后的字符串 /// /// /// /// /// public static string GetPasswordDigest(string nonce, string createdString, string password) { byte[] combined = buildBytes(nonce, createdString, password); string output = System.Convert.ToBase64String(SHAOneHash(combined)); return output; } /// /// 加密的时间戳 /// /// public static string GetCreated() { return DateTime.Now.ToString("yyyy-MM-ddThh:mm:ssZ"); } /// /// 将随机的16位字节数据加密成nonce /// /// /// public static string GetNonce() { byte[] nonce = new byte[16]; new Random().NextBytes(nonce); return System.Convert.ToBase64String(nonce); } /// /// 获取加密后的头部 /// /// /// /// public static string GetHead(string username, string password) { string res = string.Empty; string format = @" {0} {1} {2} {3} "; string nonce = GetNonce(); string created = GetCreated(); string pas = GetPasswordDigest(nonce, created, password); res = string.Format(format, username, pas, nonce, created); return res; } } |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |