cmpp3.0 md5 加密码部份的两种方式.

public CMPP_CONNECT() throws Exception{
  this.setTotal_Length(39);
  this.setCommand_Id(0x01);//1
  this.setSequence_Id(3);
  this.Source_Addr = SP_ID;   
  String timestamp2 = new SimpleDateFormat("MMddHHmmss").format(new Date());  
  
  byte[] aspid = SP_ID.getBytes();//第一种加密方式.
  byte[] anight = new byte[9];
  byte[] aser = SHARED_SECRET.getBytes();
  byte[] atimestamp2 = timestamp2.getBytes();  
  ByteBuffer aauth=ByteBuffer.allocate(aspid.length+anight.length+aser.length+atimestamp2.length);
  aauth.put(aspid);
  aauth.put(anight);
  aauth.put(aser);
  aauth.put(atimestamp2);   
  try {
   MessageDigest md52 = MessageDigest.getInstance("MD5");
   md52.update(aauth.array());// 鉴别源地址   
   this.aubyte = md52.digest();
  } catch (Exception e) {
   System.out.println("jia mi  exception ");
  }   
  
  this.AuthenticatorSource=new MD5().getMD5ofStr(new String(aauth.array())); 
  //this.AuthenticatorSource=new MD5().getMD5ofStr(SP_ID+"000000000"+SHARED_SECRET+timestamp2);   
  aubyte1 = this.AuthenticatorSource.getBytes();
  
  this.Version = 0x30;
  this.setTimestamp(TimeStamp.getIntTimestamp()); 
  
  
  byte[] buffer = new byte[27];  
  byte[] buffer1 ;
  byte[] spid = this.Source_Addr.getBytes();
  byte[] pass = SHARED_SECRET.getBytes();
  String timestamp = new SimpleDateFormat("MMddHHmmss").format(new Date());// 时间戳明文由客户端产生
  int length = spid.length + pass.length + 19;// 19 = 9字节的0
  byte[] auth = new byte[length];
  int position = spid.length;
  System.arraycopy(spid,0,auth,0,position);
  position += 9;
  System.arraycopy(pass,0,auth,position,pass.length);
  position += pass.length;
  System.arraycopy(timestamp.getBytes(),0,auth,position,10);  
  System.out.println("auth str= "+new String(auth));
  try {
   MessageDigest md5 = MessageDigest.getInstance("MD5");
   md5.update(auth);// 鉴别源地址
   //this.aubyte = md5.digest();  // 除开注释就是第二种
   //md5.digest(this.aubyte);// 从第六位开始存储>=6 <=21
   //md5.digest(buffer1, 0, 16);// 从第六位开始存储>=6 <=21
   buffer1 = md5.digest();
  } catch (Exception e) {
   System.out.println("jia mi  exception ");
   buffer1 = new byte[10];
  } 
  // write enterprise code
  System.arraycopy(spid, 0, buffer, 0, spid.length); 
  buffer[22] = (byte)this.Version;
  System.arraycopy(Bytes2All.integerToByte(TimeStamp.getIntTimestamp()), 0, buffer,23, 4); 
 } 
 public byte[] toBytes(){
  ByteBuffer buf=ByteBuffer.allocate(39);
  buf.putInt(this.getTotal_Length());//4  4   0-3
  buf.putInt(this.getCommand_Id());//4  8  4-7
  buf.putInt(this.getSequence_Id());//4   12  8-11
  buf.put(this.getSource_Addr().getBytes());//6  18 12-17
  //buf.put(this.AuthenticatorSource.getBytes());//16  34  18-33   
  buf.put(this.aubyte);  
  buf.put(this.Version); //1  35  34-34
  buf.putInt(this.getTimestamp());//4 39  35-38
  return buf.array();
 };

 

 

 

1.

 

byte[] aspid = SP_ID.getBytes();//第一种加密方式.
  byte[] anight = new byte[9];
  byte[] aser = SHARED_SECRET.getBytes();
  byte[] atimestamp2 = timestamp2.getBytes(); 
  ByteBuffer aauth=ByteBuffer.allocate(aspid.length+anight.length+aser.length+atimestamp2.length);
  aauth.put(aspid);
  aauth.put(anight);
  aauth.put(aser);
  aauth.put(atimestamp2);  
  try {
   MessageDigest md52 = MessageDigest.getInstance("MD5");
   md52.update(aauth.array());// 鉴别源地址  
   this.aubyte = md52.digest();
  } catch (Exception e) {
   System.out.println("jia mi  exception ");
  }  

posted @ 2014-09-12 16:00  hanks  阅读(421)  评论(0)    收藏  举报