专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »算法 » java算法:SIPC的认证算法java实现 »正文

java算法:SIPC的认证算法java实现

来源: 发布时间:星期四, 2009年2月12日 浏览:10次 评论:0


SIPC认证算法支持SHA1和MD5



import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;

/**
* @author zackel
* @date 2007-11-22 21:38:07
* @project
*/
public GenAuthResponse {
// Fields
private String cnonce;
private String do;
private String nonce;
private String password;
private Random random;
private String sid;
private boolean usingSHA1=false;
private String salt;
private String encryptPassword;

public void Salt(String salt) {
this.salt = salt;
}

public String getEncryptPassword {
encryptPassword;
}

public void EncryptPassword(String encryptPassword) {
this.encryptPassword = encryptPassword;
}

public String getSalt {
salt;
}

// public void Salt(String salt) {
// this.salt = salt;
// }

public boolean isUsingSHA1 {
usingSHA1;
}

public void UsingSHA1(boolean usingSHA1) {
this.usingSHA1 = usingSHA1;
}

public String getCnonce {

this.cnonce;
}

public void Cnonce(String cnonce) {
this.cnonce = cnonce;
}

public String getDo {
do;
}

public void Do(String do) {
this.do = do;
}

public String getNonce {
nonce;
}

public void Nonce(String nonce) {
this.nonce = nonce;
}

public String getPassword {
password;
}

public void Password(String password) {
this.password = password;
}

public String getSid {
sid;
}

public void Sid(String sid) {
this.sid = sid;
}

// Methods
public GenAuthResponse(String sid, String password, String do, String nonce)
{
this.sid = sid;
this.password = password;
this.do = do;
this.nonce = nonce;


Calendar cal = GregorianCalendar;
seed = cal.get(Calendar.DAY_OF_YEAR) * 0xf4240;
seed cal.get(Calendar.HOUR) * 0x2710;
seed cal.get(Calendar.MINUTE) * 100;
seed cal.get(Calendar.SECOND);
// random = Random(.currentTimeMillis);

random = Random(seed);
this.cnonce = GenCNonce;
this.salt=GenSalt;
// .out.prln(salt);
}

private String BinToHex( binary)
{
// StringBuilder builder = StringBuilder;
// for( num : binary)
// {
// n = num;
// (n < 0)
// n = n &0xff;
//
// (()n > 15)
// {
// builder.append(String.format("%X",n));
//
// }
//
// {
// builder.append(String.format("0%X",n));
//
// }
// }
//// .out.prln(builder.toString);
// builder.toString;
(binary null)
"null";
(binary.length 0)
"";

StringBuilder buf = StringBuilder;

for ( i = 0; i < binary.length; i) {
(binary[i] < 0)
buf.append(Integer.toHexString(binary[i]&0xff));
(binary[i] < 16) {
buf.append('0');
buf.append(Integer.toHexString(binary[i]));
} {
buf.append(Integer.toHexString(binary[i]));
}
}

buf.toString.toUpperCase;
}

private String GenH1( key)
{
String s = String.format(":%s:%s", this.nonce, this.getCnonce);
// .out.prln("nonce:"+this.nonce );
// .out.prln("Cnonce:"+this.getCnonce);
// .out.prln(s);
s;
try {
// s = s.getBytes("UTF-8");
s= String(s.getBytes,"utf-8").getBytes;

// .out.prln("h1 s:"+this.BinToHex(s));
.gif' /> = [key.length + s.length];
..gif' />copy(key,0,.gif' />,0,key.length);
..gif' />copy(s, 0, .gif' />, key.length, s.length);

// .out.prln("h1 .gif' />:"+this.BinToHex(.gif' />));
MD5ToHex(.gif' />);
} catch (UnsupportedEncodingException e) {
e.prStackTrace;
}
null;

}

private String GenH2
{
String s = String.format("REGISTER:%s", this.sid);
// .out.prln(s);
try {

// MD5ToHex(s.getBytes("UTF-8"));
MD5ToHex( String(s.getBytes,"utf-8").getBytes);
} catch (UnsupportedEncodingException e) {
e.prStackTrace;


}
null;
}

private GenKey
{
(this.usingSHA1true){
String s1 = String.format("%s:%s:", this.sid, this.do);
// .out.prln(s1);
try {
this.encryptPassword=DoHashPassword(this.password);
// .out.prln("GenKey enc psw:");
// .out.prln(this.encryptPassword);

s=( String(s1.getBytes,"utf-8").getBytes);
// .out.prln("GenKey s:"+BinToHex(s));
src=this.HexToBin(this.encryptPassword.sub(8));//和C#定义不样?
// .out.prln("psw0-8:"+BinToHex(src));
// src=this.HexToBin(this.salt);
dst= [s.length +src.length ];
..gif' />copy(s,0,dst,0,s.length);
..gif' />copy(src,0,dst,s.length,src.length);
// .out.prln("GenKey dst:"+BinToHex(dst));
SHA1.getDigestOfBytes(dst);

} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.prStackTrace;
}

}
String s = String.format("%s:%s:%s", this.sid, this.do, this.password);
// .out.prln(s);
try {
// MD5(s.getBytes("UTF-8"));
MD5( String(s.getBytes,"utf-8").getBytes);
} catch (UnsupportedEncodingException e) {

e.prStackTrace;
}
null;
}

public String GenResponse
{
key = this.GenKey;
// .out.prln("Key:");
// .out.prln(this.BinToHex(key));
String str = this.GenH1(key);
// .out.prln("H1:");
// .out.prln(str);
String str2 = this.GenH2;
// .out.prln("H2:");
// .out.prln(str2);
this.GenResponse(str, str2);
}

private String GenResponse(String h1, String h2)
{
String s = String.format("%s:%s:%s", h1, this.nonce, h2);
// .out.prln(s);
try {
// MD5ToHex(s.getBytes("UTF-8"));
MD5ToHex( String(s.getBytes,"utf-8").getBytes);
} catch (UnsupportedEncodingException e) {

e.prStackTrace;
}
null;
}

private String GenCNonce
{
num = 0;
num2 = 0;
num3 = 0;
num4 = 0;
synchronized (random)
{
num = random.nextInt;
num2 = random.nextInt;
num3 = random.nextInt;
num4 = random.nextInt;
}
((num >> 0x18) < 0x10)
{

num 0x10000000;
}
((num2 >> 0x18) < 0x10)
{
num2 0x10000000;
}
((num3 >> 0x18) < 0x10)
{
num3 0x10000000;
}
((num4 >> 0x18) < 0x10)
{
num4 0x10000000;
}
String.format("%X%X%X%X", Object { num, num2, num3, num4 });
//
}


private HexToBin(String hex)
{
hex=hex.toUpperCase;
((hex null) || (hex.length < 1))
{
[0];
}
num = hex.length / 2;
buffer = [num];
num *= 2;
for ( i = 0; i < num; i)
{
// .out.prln(hex.sub(i, i+2));
char c1,c2;
c1=hex.sub(i,i+1).charAt(0);
c2=hex.sub(i+1, i+2).charAt(0);
// .out.prln(c1);
(c1>='A')
c1=(char) (10+c1-'A');

c1=(char) (c1-'0');
(c2>='A')
c2=(char) (10+c2-'A');

c2=(char) (c2-'0');

// .out.prln(c1-'A');
// .out.prln(c2);
// .out.prln(c1*16+c2);
num3=c1*16+c2;
// num3 = Integer.parseInt(hex.sub(i, i+2));
buffer[i / 2] = () num3;
i;

}
// .out.prln(BinToHex(buffer));
buffer;
}

private MD5( data)
{
MD5Util.MD5Encode(data);
}

private String MD5ToHex( data)
{
// data = MD5(data);
BinToHex(MD5(data));
}

public String DoHashPassword(String _password){
try {
DoHashPassword( String(_password.getBytes,"utf-8").getBytes,this.HexToBin(this.salt));
} catch (UnsupportedEncodingException e) {
e.prStackTrace;
}
null;

}
public String DoHashPassword( password, b0)
{
SHA1 sha=SHA1.Create;


src = sha.getDigestOfBytes(password);
// .out.prln("psw sha1:");
// .out.prln(this.BinToHex(src));
for ( i = 0; i < password.length; i)
{
password[i] = 0;
}
dst = [b0.length + src.length];


..gif' />copy(b0, 0, dst, 0, b0.length);
..gif' />copy(src, 0, dst, b0.length, src.length);
buffer3 = sha.getDigestOfBytes(dst);

// .out.prln("buffer3 sha1:");
// .out.prln(this.BinToHex(buffer3));

buffer4 = [b0.length + buffer3.length];
..gif' />copy(b0, 0, buffer4, 0, b0.length);
..gif' />copy(buffer3, 0, buffer4, b0.length, buffer3.length);
// .out.prln("enc psw:");
// .out.prln(this.BinToHex(buffer4));

this.BinToHex(buffer4);

}

public String GenSalt{
salt = [4];

synchronized (random)
{
salt[0] = ()random.nextInt(255);
salt[1] = ()random.nextInt(255);
salt[2] = ()random.nextInt(255);
salt[3] = ()random.nextInt(255);
// .out.prln(salt[3]);
this.BinToHex(salt);
// salt;
}



}
// public String EncodePassword( password, b0)
// {
// using (SHA1 sha = SHA1.Create)
// {
// src = sha.ComputeHash(password);
// for ( i = 0; i < password.Length; i)
// {
// password[i] = 0;
// }
// dst = [b0.Length + src.Length];
// Buffer.BlockCopy(b0, 0, dst, 0, b0.Length);
// Buffer.BlockCopy(src, 0, dst, b0.Length, src.Length);
// buffer3 = sha.ComputeHash(dst);
// buffer4 = [b0.Length + buffer3.Length];
// Buffer.BlockCopy(b0, 0, buffer4, 0, b0.Length);
// Buffer.BlockCopy(buffer3, 0, buffer4, b0.Length, buffer3.Length);
// BinaryToHex(buffer4);
// }
// }
// public String BinaryToHex( binary)
// {
// StringBuilder builder = StringBuilder;
// for( num:binary)
// {
// (num > 15)
// {
// builder.("{0:X}", num);
// }
//
// {
// builder.AppendFormat("0{0:X}", num);
// }
// }
// builder.toString;
// }

// public void (String args){
// GenAuthResponse genAuthResponse= GenAuthResponse("759909","A","fetion.com.cn", "250BA679516452E860C02B1638D52849");
// genAuthResponse.UsingSHA1(true);
// genAuthResponse.Cnonce("798AC8BD240DABB21C9259C8148373F2");
// genAuthResponse.Salt("FD761703");
//
//// genAuthResponse.HexToBin("E0FF0100");
// .out.prln(genAuthResponse.GenResponse);
//// .out.prln(genAuthResponse.getCnonce);
//
//// Random random = Random(10);
//// i=random.nextInt(9);
// num3;
//// .out.prln(num3 = Integer.parseInt("0x5A"));
//
// }
}

0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: