public string verifica_passwd(string hash, string cnpj) { banco = new dados(); string retorno = null; try { banco.abreConexao(); banco.iniciarTransacao(); banco.singleTransaction("select AES_DECRYPT(`licenca_adm`.`password`,?passwd) from `conector`.`licenca_adm` where cnpj=?str"); banco.addParametro("?passwd", hash); banco.addParametro("?str", cnpj); banco.procedimentoRead(); if (banco.retornaRead().Read() == true) { retorno = Convert.ToString(banco.retornaRead().GetString(0)); } else { retorno = null; } banco.fechaRead(); banco.commit(); } catch (Exception erro) { banco.rollback(); retorno = null; } finally { banco.fechaConexao(); } return(retorno); }
public Boolean descryptFileLiberacao(string path) //Flavio { Boolean valida = false; cspp.KeyContainerName = keyName; rsa = new RSACryptoServiceProvider(cspp); rsa.PersistKeyInCsp = true; // Create instance of Rijndael for // symetric decryption of the data. RijndaelManaged rjndl = new RijndaelManaged(); rjndl.KeySize = 256; rjndl.BlockSize = 256; rjndl.Mode = CipherMode.CBC; //Obtém ou define o modo de operação do algoritmo simétrico. // Create byte arrays to get the length of // the encrypted key and IV. // These values were stored as 4 bytes each // at the beginning of the encrypted package. byte[] LenK = new byte[4]; byte[] LenIV = new byte[4]; // Consruct the file name for the decrypted file. string outFile = path.Substring(0, path.LastIndexOf(".")) + ".txt"; // Use FileStream objects to read the encrypted // file (inFs) and save the decrypted file (outFs). using (FileStream inFs = new FileStream(path.Substring(0, path.LastIndexOf(".")) + ".enc", FileMode.Open)) { inFs.Seek(0, SeekOrigin.Begin); inFs.Seek(0, SeekOrigin.Begin); inFs.Read(LenK, 0, 3); inFs.Seek(4, SeekOrigin.Begin); inFs.Read(LenIV, 0, 3); // Convert the lengths to integer values. int lenK = BitConverter.ToInt32(LenK, 0); int lenIV = BitConverter.ToInt32(LenIV, 0); // Determine the start postition of // the ciphter text (startC) // and its length(lenC). int startC = lenK + lenIV + 8; int lenC = (int)inFs.Length - startC; // Create the byte arrays for // the encrypted Rijndael key, // the IV, and the cipher text. byte[] KeyEncrypted = new byte[lenK]; byte[] IV = new byte[lenIV]; // Extract the key and IV // starting from index 8 // after the length values. inFs.Seek(8, SeekOrigin.Begin); inFs.Read(KeyEncrypted, 0, lenK); inFs.Seek(8 + lenK, SeekOrigin.Begin); inFs.Read(IV, 0, lenIV); //Directory.CreateDirectory(path.Substring(0, path.LastIndexOf("."))); // Use RSACryptoServiceProvider // to decrypt the Rijndael key. byte[] KeyDecrypted = rsa.Decrypt(KeyEncrypted, false); // Decrypt the key. ICryptoTransform transform = rjndl.CreateDecryptor(KeyDecrypted, IV); // Decrypt the cipher text from // from the FileSteam of the encrypted // file (inFs) into the FileStream // for the decrypted file (outFs). using (FileStream outFs = new FileStream(outFile, FileMode.Create)) { int count = 0; int offset = 0; // blockSizeBytes can be any arbitrary size. int blockSizeBytes = rjndl.BlockSize / 8; byte[] data = new byte[blockSizeBytes]; // By decrypting a chunk a time, // you can save memory and // accommodate large files. // Start at the beginning // of the cipher text. inFs.Seek(startC, SeekOrigin.Begin); using (CryptoStream outStreamDecrypted = new CryptoStream(outFs, transform, CryptoStreamMode.Write)) { do { count = inFs.Read(data, 0, blockSizeBytes); offset += count; outStreamDecrypted.Write(data, 0, count); }while (count > 0); outStreamDecrypted.FlushFinalBlock(); outStreamDecrypted.Close(); } outFs.Close(); } //Open the stream and read it back. using (FileStream fs = File.OpenRead(outFile)) { string tem = ""; string aux1, aux2, aux3, aux5, aux6, aux7, aux8, retorno; int teste = 0; banco = new dados(); byte[] b = new byte[1024]; UTF8Encoding temp = new UTF8Encoding(true); while (fs.Read(b, 0, b.Length) > 0) { tem += temp.GetString(b); aux1 = tem.Substring(0, tem.IndexOf("|")); if (aux1.Length == 14) { aux2 = tem.Substring(tem.IndexOf("|") + 1, 1); aux3 = tem.Substring(tem.IndexOf("|") + 3, 8); _Periodo_Liberacao = tem.Substring(tem.IndexOf("|") + 12, 8); aux5 = tem.Substring(tem.IndexOf("|") + 21, 8); aux6 = tem.Substring(tem.IndexOf("|") + 30, 1); aux7 = tem.Substring(tem.IndexOf("|") + 32, 1); aux8 = tem.Substring(tem.IndexOf("|") + 34, 34); teste = count_passwd(aux1); if (aux1 == CNPJ) { if (teste > 0) { retorno = verifica_passwd(keyName, aux1); if (retorno != null) { if (Convert.ToDouble(retorno) < Convert.ToDouble(_Periodo_Liberacao)) { conector_passwd(aux1, aux3, _Periodo_Liberacao, aux2, _store, aux5, keyName); if (auxConsistencia == 0) { valida = true; } } } } else if ((teste = count_store(aux1)) == 1 && aux5 != "" && aux1.Length == 14)//Existe Loja ligada a senha e senha { conector_passwd(aux1, aux3, _Periodo_Liberacao, aux2, _store, aux5, keyName); if (auxConsistencia == 0) { valida = true; } } } else { valida = false; } } else { valida = false; } } } if (File.Exists(outFile)) { File.Delete(outFile); } inFs.Close(); return(valida); } }