面java中rsa加密的Sha1加密在c#中对应要怎么写

谁能给个注释详细的SHA1算法的实现?
来源:csdn
【网上找了几个,没什么注释,看不懂的说。】
给你个E文注释的,很详细的
* &b&This Java Class consists the server side for the wondeful Javas cript library 'sha1.js'. I wrote it because I basically needed
* some cheap client/server login authentication by the usual key/data system. Besides, I got the creeps watching the password
* posted unencrypted via http requests. This class makes sure that if your client is using the 'sha1.js' to encrypt the password
* with a key sent by the server, you can always repeat the encrypting on the server side (using the same key) and compare the
* encrypted strings. Since anyone who is trapping the http requests can actually send you the same encrypted string, I suggest
* you use the client's IP address as the base for the key generation. Since IP address spoofing is not a problem, this authentication
* method is not a very secured solution. If you need a full proof solution use ssl. However, this one, sure beats nothing.
* Feel free to do with it whatever you want&/b&
* &p&&b&This Class is an Abstract Class, to make sure you do not create any new instances of it. It does not throw any exceptions and
* the code is much more 'C' like than pure object oriented. There are no implemented interfaces and no inheritance in use. In fact, it
* is written as close as possible to the original Javas cript code. I did not test tweaking the instance variables but if you do change
* them, make sure to apply the same change in the 'sha1.js' library or you won't get the same encrypted strings.
* You can call each one of the 6 work methods by using something like: SHA1.hex_hmac_sha1("key", "data");
* They are the only public methods. All are public and static. You have no reason to call the private ones anyway.&/p&&/b&
* &p&The 'sha1.js' is a Javas cript implementation of the Secure Hash Algorithm, SHA-1, as defined in FIPS PUB 180-1.
* Javas cript Version 2.1 Copyright Paul Johnston 2000 - 2002. Other contributors to Javas cript version: Greg Holt,
* Andrew Kepert, Ydnar, Lostinet Distributed under the BSD License&/p&
* &p&See &a rel="nofollow" href="http://pajhome.org.uk/crypt/md5"&http://pajhome.org.uk/crypt/md5&/a& for details.&/p&
* &p&&b&Author: &/b&T.N.Silverman (C.T.Xm - SiA Riga, LV)
&a rel="nofollow" href="mailto:"&mailto:&/a&
* &br&Creation date: (3/27/:00 PM)&/p&
* &p&Don't forget to visit my company, &b&CTXM&/b& site at &a rel="nofollow" href=""&&/a& where you will find reference to all of the games this code is used in.
public abstract class SHA1 {
private static final boolean hexcase =/* hex output format. false - true - uppercase */
private static final String b64pad = "=";
/* base-64 pad character. "=" for strict RFC compliance
private static final int chrsz = 8;
/* bits per input character. 8 - ASCII; 16 - Unicode
* This is one of the functions you'll usually want to call
* It take a string arguments and returns either hex or base-64 encoded strings
* Creation date: (3/27/:10 PM)
* @author T.N.Silverman
* @version 1.0.0
* @return java.lang.String
* @param key java.lang.String
* @param data java.lang.String
public static String b64_hmac_sha1(String key, String data) {
return binb2b64(core_hmac_sha1(key, data));
* This is one of the functions you'll usually want to call
* It take a string argument and returns either hex or base-64 encoded strings
* Creation date: (3/27/:10 PM)
* @author T.N.Silverman
* @version 1.0.0
* @return java.lang.String
* @param s java.lang.String
public static String b64_sha1(String s) {
s = (s==null) ? "" :
return binb2b64(core_sha1(str2binb(s), s.length() * chrsz));
* Convert an array of big-endian words to a base-64 string
* Creation date: (3/27/:10 PM)
* @author T.N.Silverman
* @version 1.0.0
* @return java.lang.String
* @param binarray int[]
private static String binb2b64(int[] binarray) {
String tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
String str = "";
binarray = strechBinArray(binarray, binarray.length * 4);
for (int i = 0; i & binarray.length * 4; i += 3) {
int triplet =
(((binarray[i && 2] && 8 * (3 - i % 4)) & 0xFF) && 16)
| (((binarray[i + 1 && 2] && 8 * (3 - (i + 1) % 4)) & 0xFF) && 8)
| ((binarray[i + 2 && 2] && 8 * (3 - (i + 2) % 4)) & 0xFF);
for (int j = 0; j & 4; j++) {
if (i * 8 + j * 6 & binarray.length * 32)
str += b64
str += tab.charAt((triplet && 6 * (3 - j)) & 0x3F);
return cleanB64Str(str);
* Convert an array of big-endian words to a hex string.
* Creation date: (3/27/:10 PM)
* @author T.N.Silverman
* @version 1.0.0
* @return java.lang.String
* @param binarray int[]
private static String binb2hex(int[] binarray) {
String hex_tab = hexcase ? "ABCDEF" : "abcdef";
String str = "";
for (int i = 0; i & binarray.length * 4; i++) {
char a = (char) hex_tab.charAt((binarray[i && 2] && ((3 - i % 4) * 8 + 4)) & 0xF);
char b = (char) hex_tab.charAt((binarray[i && 2] && ((3 - i % 4) * 8)) & 0xF);
str += (new Character(a).toString() + new Character(b).toString());
* Convert an array of big-endian words to a string
* Creation date: (3/27/:10 PM)
* @author T.N.Silverman
* @version 1.0.0
* @return java.lang.String
* @param bin int[]
private static String binb2str(int[] bin) {
String str = "";
int mask = (1 && chrsz) - 1;
for (int i = 0; i & bin.length * 32; i += chrsz)
str += (char) ((bin[i && 5] &&& (24 - i % 32)) & mask);
* Bitwise rotate a 32-bit number to the left.
* Creation date: (3/26/:01 PM)
* @author T.N.Silverman
* @version 1.0.0
* @return int
* @param num int
* @param cnt int
private static int bit_rol(int num, int cnt) {
return (num && cnt) | (num &&& (32 - cnt));
* Cleans a base64 String from all the trailing 'A' or other
* characters put there by binb2b64 that made the bin array
* 4 times larger than it originally was.
* Creation date: (3/27/:10 PM)
* @author T.N.Silverman
* @version 1.0.0
* @return java.lang.String
* @param str java.lang.String
private static String cleanB64Str(String str) {
str = (str==null) ? "" :
int len = str.length();
if (len &= 1)
char trailChar = str.charAt(len - 1);
String trailStr="";
for (int i=len-1;i&=0 && str.charAt(i)==trailCi--)
trailStr += str.charAt(i);
return str.substring(0,str.indexOf(trailStr));
* Makes an int array of a length less than 16 an array of length 16 with all previous
* cells at their previous indexes.
* Creation date: (3/27/:10 PM)
* @author T.N.Silverman
* @version 1.0.0
* @return int[]
* @param str java.lang.String
private static int[] complete216(int[] oldbin) {
if (oldbin.length &= 16)
int[] newbin = new int[16 - oldbin.length];
for (int i = 0; i & newbin. newbin[i] = 0, i++);
return concat(oldbin, newbin);
* Joins two int arrays and return one that contains all the previous values.
* This corresponds to the concat method of the Javas cript Array object.
* Creation date: (3/27/:10 PM)
* @author T.N.Silverman
* @version 1.0.0
* @return int[]
* @param str java.lang.String
private static int[] concat(int[] oldbin, int[] newbin) {
int[] retval = new int[oldbin.length + newbin.length];
for (int i = 0; i & (oldbin.length + newbin.length); i++) {
if (i & oldbin.length)
retval[i] = oldbin[i];
retval[i] = newbin[i - oldbin.length];
* Calculate the HMAC-SHA1 of a key and some data
* Creation date: (3/26/:01 PM)
* @author T.N.Silverman
* @version 1.0.0
* @return int
* @param x java.lang.String[]
* @param len int
private static int[] core_hmac_sha1(String key, String data) {
key = (key == null) ? "" :
data = (data == null) ? "" :
int[] bkey = complete216(str2binb(key));
if (bkey.length & 16)
bkey = core_sha1(bkey, key.length() * chrsz);
int[] ipad = new int[16];
int[] opad = new int[16];
for (int i = 0; i & 16; ipad[i] = 0, opad[i] = 0, i++);
for (int i = 0; i & 16; i++) {
ipad[i] = bkey[i] ^ 0x;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
int[] hash =
core_sha1(concat(ipad, str2binb(data)), 512 + data.length() * chrsz);
return core_sha1(concat(opad, hash), 512 + 160);
免责声明:本站部分内容、图片、文字、视频等来自于互联网,仅供大家学习与交流。相关内容如涉嫌侵犯您的知识产权或其他合法权益,请向本站发送有效通知,我们会及时处理。反馈邮箱&&&&。
学生服务号
在线咨询,奖学金返现,名师点评,等你来互动C#中应用SHA1算法 - 胜马软件 ERP CRM
- 畅享博客
|收藏博客|加入友情链接|给博主留言
共同探讨企业运营管理,信息化建设,胜马ERP CRM
C#中应用SHA1算法
C#中应用SHA1算法
&&& SHA1算法是安全哈希算法(Secure Hash Algorithm)的简称,主要适用于数字签名标准(Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signature Algorithm DSA)。对于长度小于2^64位的消息,SHA1会产生一个160位的消息摘要。当接收到消息的时候,这个消息摘要可以用来验证数据的完整性。在传输的过程中,数据很可能会发生变化,那么这时候就会产生不同的消息摘要。 SHA1有如下特性:不可以从消息摘要中复原信息;两个不同的消息不会产生同样的消息摘要。 另外,SHA1算法是不可逆的,经过加密后的字符很难解密。&&& 在C#中应用SHA1算法很简便,只需加入下面一句即可:System.Web.Security.FormAuthentication.HashPasswordForStoringInConfigFile(&要加密的字符&,&SHA1&);该句返回的是string类型的加密后的字符。我自己试了一下,如&123&经过SHA1算法加密后变为下面的字符串:40BDFCEA1FF5C5ECBDBBEEF。我想一般人甚至是计算机也很难破解这种加密,你看看加密后的字符就知道了。其实不是我想的,而是至今未有人破解SHA1加密,不过听说MD5算法被破解了。&&&& 这样,我们就可以把一些密码写成加密后的字符而应用到网页中,即使别人看到了你的加密后的密码他也不知道你的真正密码。<div class="votes" id="Score
查阅更多相关主题的帖子:
下一篇:上一篇:
您还未登录,不能对文章发表评论!请先5845人阅读
[01] .NET随笔(48)
安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准 (Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signature Algorithm DSA)
SHA-1是一种数据加密算法,该算法的思想是接收一段明文,然后以一种不可逆的方式将它转换成一段(通常更小)密文,也可以简单的理解为取一串输入码(称为预映射或信息),并把它们转化为长度较短、位数固定的输出序列即散列&#20540;(也称为信息摘要或信息认证代码)的过程。类&#20284;MD5加密
C#中进行Sha1加密字符串返回加密字符串
public class SHA1Helper
#region 获取由SHA1加密的字符串
public static string EncryptToSHA1(string str)
SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
byte[] str1 = Encoding.UTF8.GetBytes(str);
byte[] str2 = puteHash(str1);
sha1.Clear();
(sha1 as IDisposable).Dispose();
return Convert.ToBase64String(str2);
#endregion
#region 获取由MD5加密的字符串
public static string EncryptToMD5(string str)
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] str1 = Encoding.UTF8.GetBytes(str);
byte[] str2 = puteHash(str1, 0, str1.Length);
md5.Clear();
(md5 as IDisposable).Dispose();
return Convert.ToBase64String(str2);
#endregion
#region [ 生成hmacsha1的散列 ]
public static string HmacSha1(string word)
return BitConverter.ToString(SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(word))).Replace(&-&, string.Empty);
#endregion
[ 字符串的字典序排序,区分大小写 ]
public static string GetOrder(string[] ss)
var list = ss.OrderBy(x =& x, StringComparer.Ordinal).ToArray();
return string.Join(&&, list);
#endregion
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:192378次
积分:3435
积分:3435
排名:第9542名
原创:145篇
评论:41条
阅读:8797
阅读:10282
文章:23篇
阅读:38656
(2)(3)(2)(2)(2)(5)(6)(14)(2)(5)(14)(7)(19)(7)(35)(31)}

我要回帖

更多关于 java中rsa加密 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信