【SHA1】求加密代码

healer_kx 2010-11-30 11:18:19
求Win32 的SHA1的加密算法,得到的结果和下面这段java代码的结果一致。:

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;


public class HMAC_SHA1 {


public static void main(String[] args) {


System.out.println(base64sha1("message", "234123"));
}

public static String base64sha1(String str, String key) {
try {
String Algorithm = "HmacSHA1";
SecretKey sk = new SecretKeySpec(key.getBytes(), Algorithm);
Mac mac = Mac.getInstance(Algorithm);
mac.init(sk);
byte[] result = mac.doFinal(str.getBytes());

for (byte b : result) {
System.out.print(b);
System.out.print(" ");
}
return new String(Base64.encodeBase64(result));
} catch (NoSuchAlgorithmException e) {

e.printStackTrace();
} catch (InvalidKeyException e) {

e.printStackTrace();
}
return key;
}

}



我参考了很多代码,都不行。。。
比如这个。
...全文
532 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
strsss 2011-07-26
  • 打赏
  • 举报
回复
csdn下载得了。
用户 昵称 2010-12-07
  • 打赏
  • 举报
回复

//[/color]
//----------
pack00_len implementation of alignment when input data length is not multiple times of the length
//----------
input = 32 33 34 31 32 33
//----------
output = 32 33 34 31 32 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//

//----
xor_whole XOR data 1 to data 2, the returned data length is the length of the data 2
//----
data 1 = 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C
//----
data 2 = 32 33 34 31 32 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//----
result = 6E 6F 68 6D 6E 6F 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C
//

//------
xor_whole XOR data 1 to data 2, the returned data length is the length of the data 2
//------
data 1 = 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36
//------
data 2 = 32 33 34 31 32 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//------
result = 04 05 02 07 04 05 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36
//

//----
sha1_hash
//----
input =
//--------
04 05 02 07 04 05 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36
//--------
36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36
//--------
6D 65 73 73 61 67 65
//----
output = CB 03 87 7F ED 03 DE 7B B7 1A B9 F3 B8 CF 22 1B 53 79 7D FD
//

//--
sha1_hash
//--
input =
//------
6E 6F 68 6D 6E 6F 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C
//------
5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C
//------
CB 03 87 7F ED 03 DE 7B B7 1A B9 F3 B8 CF 22 1B 53 79 7D FD
//--
output = 22 24 EE 75 01 9B 1E 69 AD 71 AB 74 29 E2 92 F3 35 00 3A 45
//
用户 昵称 2010-12-07
  • 打赏
  • 举报
回复
下面是俺手工计算的结果,算是有个交待


//
sha1_hash ( xor_whole ( dup ( int ( 64 ) , 5c ) , pack00_len ( ansi_string ( "234123" ) , int ( 64 ) ) ) sha1_hash ( xor_whole ( dup ( int ( 64 ) , 36 ) , pack00_len ( ansi_string ( "234123" ) , int ( 64 ) ) ) ansi_string ( "message" ) ) )
//--------
int
//--------
input = 64 hex = 00 00 00 40
//

//----------
ansi_string convert utf16-little-endian format input data to ansi format
//----------
input = 32 00 33 00 34 00 31 00 32 00 33 00
//----------
output = 32 33 34 31 32 33
//

//----------
int
//----------
input = 64 hex = 00 00 00 40
//

//----------------
int
//----------------
input = 64 hex = 00 00 00 40
//

//------------------
ansi_string convert utf16-little-endian format input data to ansi format
//------------------
input = 32 00 33 00 34 00 31 00 32 00 33 00
//------------------
output = 32 33 34 31 32 33
//

//------------------
int
//------------------
input = 64 hex = 00 00 00 40
//

//------------------
ansi_string convert utf16-little-endian format input data to ansi format
//------------------
input = 6D 00 65 00 73 00 73 00 61 00 67 00 65 00
//------------------
output = 6D 65 73 73 61 67 65
//

//------
dup repeat the specified number of times the input data
//------
result = 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C 5C
//

//------
pack00_len implementation of alignment when input data length is not multiple times of the length
//------
input = 32 33 34 31 32 33
//------
output = 32 33 34 31 32 33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//

//----------
dup repeat the specified number of times the input data
//----------
result = 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 [color=#F0F0F0]
hzy694358 2010-12-07
  • 打赏
  • 举报
回复
都是牛人啊

第一次看大版求源码,呵呵……
用户 昵称 2010-12-06
  • 打赏
  • 举报
回复
结贴了,俺正在手工实现sha1_hmac呢。
OpenHero 2010-12-06
  • 打赏
  • 举报
回复
DSA可以联系我~大数库,可以联系我~
healer_kx 2010-12-06
  • 打赏
  • 举报
回复
看来我应该把这个过程好好了解一下,以及那些API的准确含义。。。
mszjk 2010-12-05
  • 打赏
  • 举报
回复
SHA用java实现?面向对象...算法...好蛋疼...
快乐鹦鹉 2010-12-04
  • 打赏
  • 举报
回复
SHA1算法可以找到源代码,目前我也正在收集.
我需要用到SHA1,Blowfish和DSA三个算法,现在还有DSA算法没有找到源代码.哪位有啊?
用户 昵称 2010-12-04
  • 打赏
  • 举报
回复
刚找到hmac的流程,先将明文处理成64字节,然后经过两次xor与hash( sha1, sha256........ )
healer_kx 2010-12-03
  • 打赏
  • 举报
回复
悲剧,难道我再转回C++版。。。
healer_kx 2010-12-02
  • 打赏
  • 举报
回复
System.out.println(base64sha1("message", "234123"));
这不是信息和密钥吗?
用户 昵称 2010-12-02
  • 打赏
  • 举报
回复
那叫hmac,你说“求Win32 的SHA1的加密算法,得到的结果和下面这段java代码的结果一致。:”,你要知道,不是每个人都有java环境的,你必须给出你的输入和输出,别人才有可能达到“结果一致
healer_kx 2010-12-02
  • 打赏
  • 举报
回复
咕~~(╯﹏╰)b,还是不对,

注意到没? 那个java代码里面是可以传递一个Key的,就是私钥,他参与了加密过程。
但是下载到的那个代码,只是Degist运算。

所以还是不行啊,各位老大再帮忙啊~~~~
梧桐126 2010-12-02
  • 打赏
  • 举报
回复
哇,大神真多! 偶是路过的...
用户 昵称 2010-12-02
  • 打赏
  • 举报
回复
应该有的是源代码吧,俺在csdn下载频道下载的。
Eleven 2010-12-02
  • 打赏
  • 举报
回复
ayw215 2010-12-01
  • 打赏
  • 举报
回复
#include <openssl/sha.h>
healer_kx 2010-12-01
  • 打赏
  • 举报
回复
谢谢,有空试一试。结果是一样的吗?
cunyan_0519 2010-12-01
  • 打赏
  • 举报
回复
等待结贴,呵呵。希望得分。
加载更多回复(8)

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧