none
Criptografia. RRS feed

  • Pergunta

  •  

      Pessoal alguem tem alguma informação para me passar ai ou d ocumento em relação a criptografia no .NET

      Eu precisava salvar algumas informação na BD SQL Server 2005 Express criptografada e também ao ler como decodificar.

     Fico no aguardo

     Obrigado.

     

     

    sexta-feira, 19 de maio de 2006 20:39

Respostas

  • /* ESTE É UM BOM EZEMPLO DE CRIPTOGRAFIA...
    BASTA COMPILAR ESSE CÓDIGO
    NOS TEXTBOX VC DEVERÁ COLOCAR O CAMINHO
    CORRETO DOS ARQUIVOS:
    EX: C:\PASTA\TEXTO.TXT*/
     
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Security;
    using System.Security.Cryptography;
    using System.Text;
    using System.IO;
    namespace Encripta_Decripta
    {
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.ComponentModel.Container components = null;
    DESCryptoServiceProvider DESCryptoProvider = new DESCryptoServiceProvider();
    public Form1()
    {
    InitializeComponent();
    }
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.label2 = new System.Windows.Forms.Label();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.label3 = new System.Windows.Forms.Label();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // label1
    //
    this.label1.Location = new System.Drawing.Point(8, 8);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(248, 23);
    this.label1.TabIndex = 0;
    this.label1.Text = "Digite o arquivo a ser encriptado / decriptado";
    //
    // textBox1
    //
    this.textBox1.Location = new System.Drawing.Point(8, 32);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(408, 20);
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "";
    //
    // label2
    //
    this.label2.Location = new System.Drawing.Point(8, 64);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(224, 23);
    this.label2.TabIndex = 2;
    this.label2.Text = "Digite o nome do arquivo de destino";
    //
    // textBox2
    //
    this.textBox2.Location = new System.Drawing.Point(8, 88);
    this.textBox2.Name = "textBox2";
    this.textBox2.Size = new System.Drawing.Size(408, 20);
    this.textBox2.TabIndex = 3;
    this.textBox2.Text = "";
    //
    // label3
    //
    this.label3.Location = new System.Drawing.Point(8, 120);
    this.label3.Name = "label3";
    this.label3.TabIndex = 4;
    this.label3.Text = "Digite a senha";
    //
    // textBox3
    //
    this.textBox3.Location = new System.Drawing.Point(8, 144);
    this.textBox3.Name = "textBox3";
    this.textBox3.Size = new System.Drawing.Size(408, 20);
    this.textBox3.TabIndex = 5;
    this.textBox3.Text = "";
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(264, 176);
    this.button1.Name = "button1";
    this.button1.TabIndex = 6;
    this.button1.Text = "Encripta";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // button2
    //
    this.button2.Location = new System.Drawing.Point(344, 176);
    this.button2.Name = "button2";
    this.button2.TabIndex = 7;
    this.button2.Text = "Decripta";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(432, 213);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox3);
    this.Controls.Add(this.label3);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.label1);
    this.Name = "Form1";
    this.Text = "Encriptação / Decriptação de arquivos";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false);
    }
    #endregion
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }
    private void EncriptaTexto(string ReadFile,string WriteFile,string Pass)
    {
    FileStream ArqEnt = new FileStream(ReadFile, FileMode.Open, FileAccess.ReadWrite);
    FileStream ArqSai = new FileStream(WriteFile, FileMode.Create, FileAccess.Write);
     
    Pass = VerificaTamChave(Pass);
     
    DESCryptoProvider.Key = ASCIIEncoding.ASCII.GetBytes(Pass);
    DESCryptoProvider.IV = ASCIIEncoding.ASCII.GetBytes(Pass);
    ICryptoTransform DESEncrypt = DESCryptoProvider.CreateEncryptor();
    CryptoStream cryptoStream = new CryptoStream(ArqSai, DESEncrypt, CryptoStreamMode.Write);
    byte[] arrayEnt = new byte[ArqEnt.Length];
    ArqEnt.Read(arrayEnt, 0,arrayEnt.Length);
    cryptoStream.Write(arrayEnt, 0, arrayEnt.Length);
    cryptoStream.Close();
    ArqEnt.Close();
    ArqSai.Close();
    }
    private void DecryptaTexto(string ReadFile, string WriteFile, string Pass)
    {
    FileStream DecEnt = new FileStream(ReadFile, FileMode.Open, FileAccess.Read);
    StreamWriter DecSai = new StreamWriter(WriteFile);
    Pass = VerificaTamChave(Pass);
    DESCryptoProvider.Key = ASCIIEncoding.ASCII.GetBytes(Pass);
    DESCryptoProvider.IV = ASCIIEncoding.ASCII.GetBytes(Pass);
    ICryptoTransform DESDecrypta = DESCryptoProvider.CreateDecryptor();
    CryptoStream cryptoStreamDec = new CryptoStream(DecEnt, DESDecrypta, CryptoStreamMode.Read);
    DecSai.Write(new StreamReader(cryptoStreamDec).ReadToEnd());
    DecSai.Flush();
    DecSai.Close();
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
    EncriptaTexto(textBox1.Text, textBox2.Text, textBox3.Text);
    }
    private string VerificaTamChave(string Pass)
    {
    if (Pass.Length < 8)
    {
    int n = (8 - Pass.Length);
    Pass = Pass.PadRight((Pass.Length + n),Convert.ToChar("t"));
    }
    else
    {
    int n = (Pass.Length - 8);
    Pass = Pass.Remove(8, n);
    }
    return Pass;
    }
    private void button2_Click(object sender, System.EventArgs e)
    {
    DecryptaTexto(textBox1.Text, textBox2.Text, textBox3.Text);
    }
    private void Form1_Load(object sender, System.EventArgs e)
    {
     
    }
    }
    }
    domingo, 21 de maio de 2006 04:44

Todas as Respostas

  • /* ESTE É UM BOM EZEMPLO DE CRIPTOGRAFIA...
    BASTA COMPILAR ESSE CÓDIGO
    NOS TEXTBOX VC DEVERÁ COLOCAR O CAMINHO
    CORRETO DOS ARQUIVOS:
    EX: C:\PASTA\TEXTO.TXT*/
     
     
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Security;
    using System.Security.Cryptography;
    using System.Text;
    using System.IO;
    namespace Encripta_Decripta
    {
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.TextBox textBox3;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.ComponentModel.Container components = null;
    DESCryptoServiceProvider DESCryptoProvider = new DESCryptoServiceProvider();
    public Form1()
    {
    InitializeComponent();
    }
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.label2 = new System.Windows.Forms.Label();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.label3 = new System.Windows.Forms.Label();
    this.textBox3 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // label1
    //
    this.label1.Location = new System.Drawing.Point(8, 8);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(248, 23);
    this.label1.TabIndex = 0;
    this.label1.Text = "Digite o arquivo a ser encriptado / decriptado";
    //
    // textBox1
    //
    this.textBox1.Location = new System.Drawing.Point(8, 32);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(408, 20);
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "";
    //
    // label2
    //
    this.label2.Location = new System.Drawing.Point(8, 64);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(224, 23);
    this.label2.TabIndex = 2;
    this.label2.Text = "Digite o nome do arquivo de destino";
    //
    // textBox2
    //
    this.textBox2.Location = new System.Drawing.Point(8, 88);
    this.textBox2.Name = "textBox2";
    this.textBox2.Size = new System.Drawing.Size(408, 20);
    this.textBox2.TabIndex = 3;
    this.textBox2.Text = "";
    //
    // label3
    //
    this.label3.Location = new System.Drawing.Point(8, 120);
    this.label3.Name = "label3";
    this.label3.TabIndex = 4;
    this.label3.Text = "Digite a senha";
    //
    // textBox3
    //
    this.textBox3.Location = new System.Drawing.Point(8, 144);
    this.textBox3.Name = "textBox3";
    this.textBox3.Size = new System.Drawing.Size(408, 20);
    this.textBox3.TabIndex = 5;
    this.textBox3.Text = "";
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(264, 176);
    this.button1.Name = "button1";
    this.button1.TabIndex = 6;
    this.button1.Text = "Encripta";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // button2
    //
    this.button2.Location = new System.Drawing.Point(344, 176);
    this.button2.Name = "button2";
    this.button2.TabIndex = 7;
    this.button2.Text = "Decripta";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(432, 213);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox3);
    this.Controls.Add(this.label3);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.label1);
    this.Name = "Form1";
    this.Text = "Encriptação / Decriptação de arquivos";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false);
    }
    #endregion
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }
    private void EncriptaTexto(string ReadFile,string WriteFile,string Pass)
    {
    FileStream ArqEnt = new FileStream(ReadFile, FileMode.Open, FileAccess.ReadWrite);
    FileStream ArqSai = new FileStream(WriteFile, FileMode.Create, FileAccess.Write);
     
    Pass = VerificaTamChave(Pass);
     
    DESCryptoProvider.Key = ASCIIEncoding.ASCII.GetBytes(Pass);
    DESCryptoProvider.IV = ASCIIEncoding.ASCII.GetBytes(Pass);
    ICryptoTransform DESEncrypt = DESCryptoProvider.CreateEncryptor();
    CryptoStream cryptoStream = new CryptoStream(ArqSai, DESEncrypt, CryptoStreamMode.Write);
    byte[] arrayEnt = new byte[ArqEnt.Length];
    ArqEnt.Read(arrayEnt, 0,arrayEnt.Length);
    cryptoStream.Write(arrayEnt, 0, arrayEnt.Length);
    cryptoStream.Close();
    ArqEnt.Close();
    ArqSai.Close();
    }
    private void DecryptaTexto(string ReadFile, string WriteFile, string Pass)
    {
    FileStream DecEnt = new FileStream(ReadFile, FileMode.Open, FileAccess.Read);
    StreamWriter DecSai = new StreamWriter(WriteFile);
    Pass = VerificaTamChave(Pass);
    DESCryptoProvider.Key = ASCIIEncoding.ASCII.GetBytes(Pass);
    DESCryptoProvider.IV = ASCIIEncoding.ASCII.GetBytes(Pass);
    ICryptoTransform DESDecrypta = DESCryptoProvider.CreateDecryptor();
    CryptoStream cryptoStreamDec = new CryptoStream(DecEnt, DESDecrypta, CryptoStreamMode.Read);
    DecSai.Write(new StreamReader(cryptoStreamDec).ReadToEnd());
    DecSai.Flush();
    DecSai.Close();
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
    EncriptaTexto(textBox1.Text, textBox2.Text, textBox3.Text);
    }
    private string VerificaTamChave(string Pass)
    {
    if (Pass.Length < 8)
    {
    int n = (8 - Pass.Length);
    Pass = Pass.PadRight((Pass.Length + n),Convert.ToChar("t"));
    }
    else
    {
    int n = (Pass.Length - 8);
    Pass = Pass.Remove(8, n);
    }
    return Pass;
    }
    private void button2_Click(object sender, System.EventArgs e)
    {
    DecryptaTexto(textBox1.Text, textBox2.Text, textBox3.Text);
    }
    private void Form1_Load(object sender, System.EventArgs e)
    {
     
    }
    }
    }
    domingo, 21 de maio de 2006 04:44
  • Bigas.

    Utilizei o teu exemplo, mas alterei estas linhas

    DESCryptoProvider.Key = ASCIIEncoding.ASCII.GetBytes(Pass);
    DESCryptoProvider.IV = ASCIIEncoding.ASCII.GetBytes(Pass);
     
    para:
     
    DESCryptoProvider.Key = ASCIIEncoding.Unicode.GetBytes(Pass);
    DESCryptoProvider.IV = ASCIIEncoding.Unicode.GetBytes(Pass);
     
    E da erro no tamanho da chave.

    Qual seria o tamanho da chave para o unicode???
     
    quinta-feira, 18 de janeiro de 2007 19:12