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 class GenAuthResponse {
 // Fields
 private String cnonce;
 private String domain;
 private String nonce;
 private String password;
 private static Random random;
 private String sid;
 private boolean usingSHA1=false;
 private String salt;
 private String encryptPassword;
 
 public void setSalt(String salt) {
  this.salt = salt;
 }

public String getEncryptPassword() {
  return encryptPassword;
 }
 
 public void setEncryptPassword(String encryptPassword) {
  this.encryptPassword = encryptPassword;
 }
 
 public String getSalt() {
  return salt;
 }
 
// public void setSalt(String salt) {
// this.salt = salt;
// }
 
 public boolean isUsingSHA1() {
  return usingSHA1;
 }
 
 public void setUsingSHA1(boolean usingSHA1) {
  this.usingSHA1 = usingSHA1;
 }
 
 public String getCnonce() {
  
  return this.cnonce;
 }
 
 public void setCnonce(String cnonce) {
  this.cnonce = cnonce;
 }
 
 public String getDomain() {
  return domain;
 }
 
 public void setDomain(String domain) {
  this.domain = domain;
 }
 
 public String getNonce() {
  return nonce;
 }
 
 public void setNonce(String nonce) {
  this.nonce = nonce;
 }
 
 public String getPassword() {
  return password;
 }
 
 public void setPassword(String password) {
  this.password = password;
 }
 
 public String getSid() {
  return sid;
 }
 
 public void setSid(String sid) {
  this.sid = sid;
 }
 
 // Methods
 public GenAuthResponse(String sid, String password, String domain, String nonce)
 {
  this.sid = sid;
  this.password = password;
  this.domain = domain;
  this.nonce = nonce;
  
  
  Calendar cal = new GregorianCalendar();
  int 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 = new Random(System.currentTimeMillis());   
  
  random = new Random(seed);
  this.cnonce = GenCNonce();
  this.salt=GenSalt();
//  System.out.println(salt);
 }
 
 private static String BinToHex(byte[] binary)
 {
//  StringBuilder builder = new StringBuilder();
//  for(byte num : binary)
//  {
//  int n = num;
//  if (n < 0)
//  n =  n &0xff;
//  
//  if ((int)n > 15)
//  {
//  builder.append(String.format("%X",n));
//  
//  }
//  else
//  {
//  builder.append(String.format("0%X",n));
//  
//  }
//  }
  System.out.println(builder.toString());
//  return builder.toString();
  if (binary == null)
   return "null";
  if (binary.length == 0)
   return "";
  
  StringBuilder buf = new StringBuilder();
  
  for (int i = 0; i < binary.length; i++) {
   if (binary[i] < 0)
    buf.append(Integer.toHexString(binary[i]&0xff));
   else if (binary[i] < 16) {
    buf.append('0');
    buf.append(Integer.toHexString(binary[i]));
   } else {
    buf.append(Integer.toHexString(binary[i]));
   }
  }
  
  return buf.toString().toUpperCase();
 }
 
 private String GenH1(byte[] key)
 {
  String s = String.format(":%s:%s", this.nonce, this.getCnonce());
//  System.out.println("nonce:"+this.nonce );
//  System.out.println("Cnonce:"+this.getCnonce());
//  System.out.println(s);
  byte[] bytes;
  try {
//   bytes = s.getBytes("UTF-8");
   bytes=new String(s.getBytes(),"utf-8").getBytes();
   
//   System.out.println("h1 bytes:"+this.BinToHex(bytes));
   byte[] array = new byte[key.length + bytes.length];
   System.arraycopy(key,0,array,0,key.length);
   System.arraycopy(bytes, 0, array, key.length, bytes.length);
   
//   System.out.println("h1 array:"+this.BinToHex(array));
   return MD5ToHex(array);
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return null;
  
 }
 
 private String GenH2()
 {
  String s = String.format("REGISTER:%s", this.sid);
//  System.out.println(s);
  try {
   
//   return MD5ToHex(s.getBytes("UTF-8"));
   return MD5ToHex(new String(s.getBytes(),"utf-8").getBytes());
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return null;
 }
 
 private byte[] GenKey()
 {
  if(this.usingSHA1==true){
   String s1 = String.format("%s:%s:", this.sid, this.domain);
//   System.out.println(s1);
   try {
    this.encryptPassword=DoHashPassword(this.password);
//    System.out.println("GenKey enc psw:");
//    System.out.println(this.encryptPassword);
    
    byte[] bytes=(new String(s1.getBytes(),"utf-8").getBytes());
//    System.out.println("GenKey bytes:"+BinToHex(bytes));
    byte[] src=this.HexToBin(this.encryptPassword.substring(8));//和C#定义不一样?
//    System.out.println("psw0-8:"+BinToHex(src));
//    byte[] src=this.HexToBin(this.salt);
    byte[] dst=new byte[bytes.length +src.length ];
    System.arraycopy(bytes,0,dst,0,bytes.length);
    System.arraycopy(src,0,dst,bytes.length,src.length);
//    System.out.println("GenKey dst:"+BinToHex(dst));
    return  new SHA1().getDigestOfBytes(dst);
    
   } catch (UnsupportedEncodingException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   }
   
  }
  String s = String.format("%s:%s:%s", this.sid, this.domain, this.password);
//  System.out.println(s);
  try {
//   return MD5(s.getBytes("UTF-8"));
   return MD5(new String(s.getBytes(),"utf-8").getBytes());
  } catch (UnsupportedEncodingException e) {
   
   e.printStackTrace();
  }
  return null;
 }
 
 public String GenResponse()
 {
  byte[] key = this.GenKey();
//  System.out.println("Key:");
//  System.out.println(this.BinToHex(key));
  String str = this.GenH1(key);
//  System.out.println("H1:");
//  System.out.println(str);
  String str2 = this.GenH2();
//  System.out.println("H2:");
//  System.out.println(str2);
  return this.GenResponse(str, str2);
 }
 
 private String GenResponse(String h1, String h2)
 {
  String s = String.format("%s:%s:%s", h1, this.nonce, h2);
//  System.out.println(s);
  try {
//   return MD5ToHex(s.getBytes("UTF-8"));
   return MD5ToHex(new String(s.getBytes(),"utf-8").getBytes());
  } catch (UnsupportedEncodingException e) {
   
   e.printStackTrace();
  }
  return null;
 }
 
 private String GenCNonce()
 {
  int num = 0;
  int num2 = 0;
  int num3 = 0;
  int num4 = 0;
  synchronized (random)
  {
   num = random.nextInt();
   num2 = random.nextInt();
   num3 = random.nextInt();
   num4 = random.nextInt();
  }
  if ((num >> 0x18) < 0x10)
  {
   
   num += 0x10000000;
  }
  if ((num2 >> 0x18) < 0x10)
  {
   num2 += 0x10000000;
  }
  if ((num3 >> 0x18) < 0x10)
  {
   num3 += 0x10000000;
  }
  if ((num4 >> 0x18) < 0x10)
  {
   num4 += 0x10000000;
  }
  return String.format("%X%X%X%X", new Object[] { num, num2, num3, num4 });
//  
 }
 
 
 private static byte[] HexToBin(String hex)
 {
  hex=hex.toUpperCase();
  if ((hex == null) || (hex.length() < 1))
  {
   return new byte[0];
  }
  int num = hex.length() / 2;
  byte[] buffer = new byte[num];
  num *= 2;
  for (int i = 0; i < num; i++)
  {
//   System.out.println(hex.substring(i, i+2));
   char c1,c2;
   c1=hex.substring(i,i+1).charAt(0);
   c2=hex.substring(i+1, i+2).charAt(0);
//   System.out.println(c1);
   if(c1>='A')
    c1=(char) (10+c1-'A');
   else
    c1=(char) (c1-'0');
   if(c2>='A')
    c2=(char) (10+c2-'A');
   else
    c2=(char) (c2-'0');
   
//   System.out.println(c1-'A');
//   System.out.println(c2);
//   System.out.println(c1*16+c2);
   int num3=c1*16+c2;
//   int num3 = Integer.parseInt(hex.substring(i, i+2));
   buffer[i / 2] = (byte) num3;
   i++;
   
  }
//  System.out.println(BinToHex(buffer));
  return buffer;
 }
 
 private byte[] MD5(byte[] data)
 {
  return MD5Util.MD5Encode(data);
 }
 
 private String MD5ToHex(byte[] data)
 {
//  data = MD5(data);
  return BinToHex(MD5(data));
 }
 
 public String DoHashPassword(String _password){
  try {
   return DoHashPassword(new String(_password.getBytes(),"utf-8").getBytes(),this.HexToBin(this.salt));
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return null;
  
 }
 public String DoHashPassword(byte[] password, byte[] b0)
 {
  SHA1 sha=SHA1.Create();
  
  
  byte[] src = sha.getDigestOfBytes(password);
//  System.out.println("psw sha1:");
//  System.out.println(this.BinToHex(src));
  for (int i = 0; i < password.length; i++)
  {
   password[i] = 0;
  }
  byte[] dst = new byte[b0.length + src.length];
  System.arraycopy(b0, 0, dst, 0, b0.length);
  System.arraycopy(src, 0, dst, b0.length, src.length);
  byte[] buffer3 = sha.getDigestOfBytes(dst);
  
//  System.out.println("buffer3 sha1:");
//  System.out.println(this.BinToHex(buffer3));
  
  byte[] buffer4 = new byte[b0.length + buffer3.length];
  System.arraycopy(b0, 0, buffer4, 0, b0.length);
  System.arraycopy(buffer3, 0, buffer4, b0.length, buffer3.length);
//  System.out.println("enc psw:");
//  System.out.println(this.BinToHex(buffer4));
  
  return this.BinToHex(buffer4);
  
 }
 
 public String GenSalt(){
  byte[] salt = new byte[4];
  
  synchronized (random)
  {
   salt[0] = (byte)random.nextInt(255);
   salt[1] = (byte)random.nextInt(255);
   salt[2] = (byte)random.nextInt(255);
   salt[3] = (byte)random.nextInt(255);
//   System.out.println(salt[3]);
   return this.BinToHex(salt);
//   return salt;
  }
  
  
  
 }
// public static String EncodePassword(byte[] password, byte[] b0)
// {
// using (SHA1 sha = SHA1.Create())
// {
// byte[] src = sha.ComputeHash(password);
// for (int i = 0; i < password.Length; i++)
// {
// password[i] = 0;
// }
// byte[] dst = new byte[b0.Length + src.Length];
// Buffer.BlockCopy(b0, 0, dst, 0, b0.Length);
// Buffer.BlockCopy(src, 0, dst, b0.Length, src.Length);
// byte[] buffer3 = sha.ComputeHash(dst);
// byte[] buffer4 = new byte[b0.Length + buffer3.Length];
// Buffer.BlockCopy(b0, 0, buffer4, 0, b0.Length);
// Buffer.BlockCopy(buffer3, 0, buffer4, b0.Length, buffer3.Length);
// return BinaryToHex(buffer4);
// }
// }
// public static String BinaryToHex(byte[] binary)
// {
// StringBuilder builder = new StringBuilder();
// for(byte num:binary)
// {
// if (num > 15)
// {
// builder.("{0:X}", num);
// }
// else
// {
// builder.AppendFormat("0{0:X}", num);
// }
// }
// return builder.toString();
// }
 
// public static void main(String[] args){
//  GenAuthResponse genAuthResponse=new GenAuthResponse("759909","A","fetion.com.cn", "250BA679516452E860C02B1638D52849");
//  genAuthResponse.setUsingSHA1(true);
//  genAuthResponse.setCnonce("798AC8BD240DABB21C9259C8148373F2");
//  genAuthResponse.setSalt("FD761703");
//  
  genAuthResponse.HexToBin("E0FF0100");
//  System.out.println(genAuthResponse.GenResponse());
  System.out.println(genAuthResponse.getCnonce());
//  
  Random random = new Random(10);
  int i=random.nextInt(9);
//  int num3;
  System.out.println(num3 = Integer.parseInt("0x5A"));
//  
// }
}

SIPC的认证算法java实现相关推荐

  1. 视频教程-内功修炼之数据结构与算法-Java

    内功修炼之数据结构与算法 2018年以超过十倍的年业绩增长速度,从中高端IT技术在线教育行业中脱颖而出,成为在线教育领域一匹令人瞩目的黑马.咕泡学院以教学培养.职业规划为核心,旨在帮助学员提升技术技能 ...

  2. 推特雪花算法 java实现

    2019独角兽企业重金招聘Python工程师标准>>> package twiter.snowflake;/*** twitter的snowflake算法 -- java实现*/ p ...

  3. java dh算法_dh密钥交换算法java

    dh密钥交换算法java 迪菲-赫尔曼密钥交换(Diffie–Hellman key exchange,简称"D–H") 是一种安全协议. 它可以让双方在完全没有对方任何预先信息的 ...

  4. 数据结构和算法(Java)-张晨光-专题视频课程

    数据结构和算法(Java)-579人已学习 课程介绍         如果说各种编程语言是程序员的招式,那么数据结构和算法就相当于程序员的内功. 想写出精炼.优秀的代码,不通过不断的锤炼,是很难做到的 ...

  5. floyed java_Floyd算法java实现demo

    Floyd算法java实现,如下: package a; /** * ┏┓ ┏┓+ + * ┏┛┻━━━┛┻┓ + + * ┃ ┃ * ┃ ━ ┃ ++ + + + * ████━████ ┃+ * ...

  6. 快速排序算法 java 实现

    快速排序算法 java 实现 快速排序算法Java实现 白话经典算法系列之六 快速排序 快速搞定 各种排序算法的分析及java实现 算法概念 快速排序是C.R.A.Hoare于1962年提出的一种划分 ...

  7. java 哈希一致算法_一致哈希算法Java实现

    一致哈希算法(Consistent Hashing Algorithms)是一个分布式系统中常用的算法.传统的Hash算法当槽位(Slot)增减时,面临所有数据重新部署的问题,而一致哈希算法确可以保证 ...

  8. java进程调度怎么画图,[Java教程]进程调度的两种算法JAVA实现

    [Java教程]进程调度的两种算法JAVA实现 0 2015-10-21 12:00:08 (SJF分为preemptive shortest job first(抢占式)和non-preemptiv ...

  9. LRU算法java实现

    LRU全称是Least Recently Used,即最近最久未使用的意思. LRU算法的设计原则是:如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也很小.也就是说,当限定的空间已 ...

  10. 数据结构与算法-java笔记一 更新中

    数据结构与算法-java笔记一 更新中 数据结构与算法 什么是数据结构.算法 数据结构学了有什么用: 线性结构 数组 特点 应用 链表 存储结构 链表类型 单链表 双向链表 双向循环链表 链表与数组的 ...

最新文章

  1. 字节跳动年度《算法资料合集》首次公开,限时下载!
  2. 翻译关于ViewController的一篇文章
  3. 关于sendmail报错“did not issue MAIL/EXPN/VRFY/ETRN during connection to
  4. 海康开放平台音视频方案对比(rtsp、http-flv、hls、rtmp)
  5. 毕业设计今日总结(一)
  6. 如何用 Redis 做实时订阅推送的?
  7. 动态数据的国际化、几种主要的国际化标签
  8. 实现自己的promise
  9. webots python e-puck 集群通信案例
  10. 无法启动调试--未安装 Silverlight Developer 运行时。请安装一个匹配版本
  11. BreakPoint Hex Workshop 6.7.2.5284 portable
  12. python数据挖掘电影评分分析_豆瓣电影数据与票房数据分析
  13. CSS3实现折角效果
  14. win10小娜不能使用解决方案
  15. android 照片特效,Android使用Gallery实现照片拖动的特效
  16. [574]tf.nn.xw_plus_b
  17. Css3中align-content,深入理解css中的align-content属性
  18. 黑色素瘤的最新研究进展(2021年5月)
  19. Nginx+Keepalived+LVS集群实战
  20. 计算机毕业设计 SSM+Vue网上招投标管理系统 电子投标系统 项目投标管理系统Java Vue MySQL数据库 远程调试 代码讲解

热门文章

  1. 【CSS3学习笔记】16:边框图片效果
  2. 万网绑定二级域名_万网主机绑定二级域名子目录
  3. iOS 强制横屏的方法
  4. 小米编程真题:风口的猪-中国牛市
  5. 固态移动硬盘安装linux,如何在移动硬盘上安装Ubuntu系统(1)
  6. 表白生成器PHP源码,带自动生成的php表白程序 v1.0
  7. sql拼接同一字段_sql多个字段拼接
  8. iPhone密码管理
  9. php实现加密解密,PHP实现的加密解密处理类
  10. java吸_结对编程(java)