public Boolean CheckMD5(chave_aut chave) { if (_body == null) { _body = new Byte[0]; } header_aut tmp = (header_aut)this.Clone(); tmp.md5 = new Byte[0]; tmp.nop = 0; Byte[] header = tmp.ToBytes(); Byte[] md5calc = new Byte[header.Length + _body.Length]; Array.Copy(header, 0, md5calc, 0, header.Length); Array.Copy(_body, 0, md5calc, header.Length, _body.Length); Byte[] msgBuffer = new Byte[16 + 16 + md5calc.Length]; Array.Copy(chave.akey, 0, msgBuffer, 0, 16); Array.Copy(md5calc, 0, msgBuffer, 16, md5calc.Length); Array.Copy(chave.akey, 0, msgBuffer, 16 + md5calc.Length, 16); md5 md5 = new md5(msgBuffer); md5.calc(); return(BitConverter.ToString(md5.getHash()) == BitConverter.ToString(base.md5)); }
internal chave_aut gera_chave(String texto) { Byte[] tmp = Encoding.ASCII.GetBytes(password);//Senha cadastrada na entidade Byte[] md5Buffer = new Byte[tmp.Length + 2]; md5Buffer[0] = 8; Array.Copy(tmp, 0, md5Buffer, 1, tmp.Length); md5Buffer[tmp.Length + 1] = 8; md5 md5_1 = new md5(md5Buffer); md5_1.calc(); return(new chave_aut(md5_1.getHash())); }
public Byte[] akey; /* Chave de autenticacao (16 bytes) */ //Chave vinda do FW /* A funcao abaixo separa a chave basica recebida (16 bytes) em 3 chaves * distintas, uma para autenticacao e duas para criptografia */ public chave_aut(Byte[] baseKey) { Byte[] b1 = new Byte[17]; Array.Copy(baseKey, 0, b1, 0, 16); b1[16] = 81; md5 md5_1 = new md5(b1); md5_1.calc(); Byte[] tmp = md5_1.getHash(); key = new Blowfish(); key.initializeKey(tmp); Byte[] b2 = new Byte[17]; Array.Copy(baseKey, 0, b2, 0, 16); b2[16] = 34; md5 md5_2 = new md5(b2); md5_2.calc(); akey = md5_2.getHash(); }
private Byte[] _enviapacote(Byte[] dados, Int32 size, ushort op, ushort status) { header_aut header = new header_aut(); if (type == AuthType.Server) { if (++ne == 0xFFFFFFFF) { ne = 1; } } header.n = ne; header.operacao = op; header.status = status; header.tam_pad = (ushort)(8 - (size % 8)); /* Calcula tamanho do padding */ if (header.tam_pad == 8) { header.tam_pad = 0; } header.tam_pad = 0; header.tam_pack = size + header.tam_pad + 32; //32 = tamanho do header_aut; Int32 bsend = header.tam_pack; header.nop = 0; Byte[] headerBytes = header.ToBytes(); if (OnPacketSent != null) { OnPacketSent(header, dados); } //Monta o buffer decriptado p/ calculo do md5 Byte[] md5CalcBuffer = new Byte[headerBytes.Length + size]; Array.Copy(headerBytes, 0, md5CalcBuffer, 0, headerBytes.Length); if (size > 0) { Array.Copy(dados, 0, md5CalcBuffer, headerBytes.Length, dados.Length); } //Cacula md5 Byte[] msgBuffer = new Byte[16 + 16 + md5CalcBuffer.Length]; Array.Copy(chave.akey, 0, msgBuffer, 0, 16); Array.Copy(md5CalcBuffer, 0, msgBuffer, 16, md5CalcBuffer.Length); Array.Copy(chave.akey, 0, msgBuffer, 16 + md5CalcBuffer.Length, 16); md5 md5 = new md5(msgBuffer); md5.calc(); header.md5 = md5.getHash(); header.nop = BitConverter.ToUInt16(header.md5, 0); //Resgata o header atualizado headerBytes = header.ToBytes(); //Encripta header Byte[] encHeader = aut_encripta_buffer(headerBytes, headerBytes.Length, 0, chave); Byte[] encBody = new Byte[0]; if (size > 0) { encBody = aut_encripta_buffer(dados, size, htonlInt32(ne), chave); } Byte[] buffer = new Byte[encHeader.Length + encBody.Length]; Array.Copy(encHeader, 0, buffer, 0, encHeader.Length); if (size > 0) { Array.Copy(encBody, 0, buffer, encHeader.Length, encBody.Length); } return(buffer); }