C# c# RSA cryptographie

A

Anonymous

Invité
#1
Bonjour à tous,

je m’intéresse a la façon de crypter le mdp lors le la connexion.
Avant c’était le système md5 qui était utilisé où:
resultat = md5(key + md5(MotDePasse)); //de mémoire.


mais maintenant le mot de passe est crypté avec le rsa. apres m'etre renseigné aupres de wikipedia, j'en ai retenu que la cryptographie en RSA utilisé un systeme de clef.
alors première question :
Dans quelle variable nous est envoyé la clef public ? j'hésite entre deux posibilitées : salt ou key dans HelloConnectMessage.

ensuite le résultat (retourné dans la variable "credentials" dans l'IdentificationMessage ?) est retourné sous quelle forme?
resultat = rsa(key/salt + rsa(MotDePasse));
ou a t-il changé ?

troisieme question : quelle classe pour crypter faudrai-t-il utiliser en c# ?
j'ai regardé du coté de System.Security.Cryptography.RSACryptoServiceProvider.
mais comment lui renseigner la clef public ?
peut etre avec RSAParameters. mais dans quelle propriété y mettre la clef ?


Si qqn a des réponses, merci de m'éclairer =) bonne prog a tous
 
A

Anonymous

Invité
#2
j'ai trouvé sur cette page : -dll-facilitant-la-programmation-de-bot.359/]viewtopic.php?f=6&t=783

Im.credentials = new CryptPass().Crypt_Pass(Hcm.Salt + "MonPass", hcm.Key);

donc apparemment le resultat est renvoyé dans credentials et il est égal au rsa(salt + MotDePasse) et la clef public etant Key.

Qqn peut-il me confirmer ?

et une question subsiste : comment crypter? :x
 
A

Anonymous

Invité
#3
en décompressant la dll j'ai trouvé

public List<int> Crypt_Pass(string Input, List<int> Key)
{
byte[] exponent = new byte[]{1,0,1};
byte[] array = new byte[256];
for (int i = 33; i < 289; i++)
{
array[i - 33] = (byte)Key;
}

byte[] bytes = Encoding.UTF8.GetBytes(Input);
RSAParameters parameters = default(RSAParameters);
RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider();
parameters.Modulus = array;
parameters.Exponent = exponent;
rSACryptoServiceProvider.ImportParameters(parameters);
List<int> list = new List<int>();
byte[] array2 = rSACryptoServiceProvider.Encrypt(bytes, false);
for (int j = 0; j < array2.Length; j++)
{
byte item = array2[j];
list.Add((int)item);
}
return list;
}

malheureusement il y a le certificat de Ankam. Shield qui viens mettre son grain de sable :/
 
A

Anonymous

Invité
#5
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Traitement
{
    public static class CryptPass
    {
        public static List<byte> Crypt_Pass(string Input, List<byte> Key)
        {
            byte[] Exponent = { 1, 0, 1 };
            byte[] Modulus = new byte[256];
            for (int i = 33; i < 289; i++)
                Modulus[i - 33] = (byte)Key[i];
            byte[] bytes_Input = Encoding.UTF8.GetBytes(Input);
            RSAParameters RSAKeyInfo = new RSAParameters();
            RSACryptoServiceProvider Crypt = new RSACryptoServiceProvider();
            RSAKeyInfo.Modulus = Modulus;
            RSAKeyInfo.Exponent = Exponent;
            Crypt.ImportParameters(RSAKeyInfo);
            List<byte> ListeOutput = new List<byte>();
            foreach (byte Output in Crypt.Encrypt(bytes_Input, false))
                ListeOutput.Add(Output);
            return ListeOutput;
        }
    }
}

La classe original et la manière de l'Appeller
Code:
Im.credentials = CryptPass.Crypt_Pass(Hcm.salt + "MotDePasse", Hcm.key)
 
A

Anonymous

Invité
#6
merci beaucoup pour vos réponses =) je vais regarder ça =)

edition:

:/ incomprehensible, voici mon code

Form1.cs
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace RsaEncrypt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ClefPublicTextBox.Text = "";
            SaltTextBox.Text = "";
            MdpTextBox.Text = "";
            ResultTextBox.Text = "";
        }

        private byte[] toBytesArray(string p)
        {
            try
            {
                return p.Replace(" ", "")
                        .Replace("0x", "")
                        .Split(',')
                        .Select(x => byte.Parse(x, System.Globalization.NumberStyles.HexNumber))
                        .ToArray();
            }
            catch (Exception)
            { return null; }
        }



        private void EncryptButton_Click(object sender, EventArgs e)
        {
            ResultTextBox.Text = "";


            byte[] clefPublic = toBytesArray(ClefPublicTextBox.Text);
            byte[] salt = toBytesArray(SaltTextBox.Text);
            byte[] mdp = Encoding.UTF8.GetBytes(MdpTextBox.Text);

            byte[] concat = new byte[salt.Length + mdp.Length];
            salt.CopyTo(concat,0);
            mdp.CopyTo(concat,salt.Length);


            byte[] result = Crypt_Pass(concat, clefPublic);

            ResultTextBox.Text += "0x" + BitConverter.ToString(result).Replace("-", ", 0x");
        }

        public static byte[] Crypt_Pass(byte[] Input, byte[] Key)
        {
            byte[] Exponent = { 1, 0, 1 };
            byte[] Modulus = new byte[256];
            for (int i = 33; i < 289; i++)
                Modulus[i - 33] = Key[i];
            RSAParameters RSAKeyInfo = new RSAParameters();
            RSACryptoServiceProvider Crypt = new RSACryptoServiceProvider();
            RSAKeyInfo.Modulus = Modulus;
            RSAKeyInfo.Exponent = Exponent;
            Crypt.ImportParameters(RSAKeyInfo);
            return Crypt.Encrypt(Input, false);
        }
        
    }
}
Form1.Designer.cs
Code:
namespace RsaEncrypt
{
    partial class Form1
    {
        /// <summary>
        /// Variable nécessaire au concepteur.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Nettoyage des ressources utilisées.
        /// </summary>
        /// <param name="disposing">true si les ressources managées doivent être supprimées ; sinon, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Code généré par le Concepteur Windows Form

        /// <summary>
        /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
        /// le contenu de cette méthode avec l'éditeur de code.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.ClefPublicTextBox = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.SaltTextBox = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.MdpTextBox = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.EncryptButton = new System.Windows.Forms.Button();
            this.label4 = new System.Windows.Forms.Label();
            this.ResultTextBox = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(60, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "ClefPublic :";
            // 
            // ClefPublicTextBox
            // 
            this.ClefPublicTextBox.Location = new System.Drawing.Point(13, 26);
            this.ClefPublicTextBox.Multiline = true;
            this.ClefPublicTextBox.Name = "ClefPublicTextBox";
            this.ClefPublicTextBox.Size = new System.Drawing.Size(259, 65);
            this.ClefPublicTextBox.TabIndex = 1;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(12, 94);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(31, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "Salt :";
            // 
            // SaltTextBox
            // 
            this.SaltTextBox.Location = new System.Drawing.Point(15, 110);
            this.SaltTextBox.Multiline = true;
            this.SaltTextBox.Name = "SaltTextBox";
            this.SaltTextBox.Size = new System.Drawing.Size(257, 75);
            this.SaltTextBox.TabIndex = 3;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(13, 192);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(33, 13);
            this.label3.TabIndex = 4;
            this.label3.Text = "mdp :";
            // 
            // MdpTextBox
            // 
            this.MdpTextBox.Location = new System.Drawing.Point(15, 209);
            this.MdpTextBox.Name = "MdpTextBox";
            this.MdpTextBox.Size = new System.Drawing.Size(257, 20);
            this.MdpTextBox.TabIndex = 5;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(197, 235);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 6;
            this.button1.Text = "Effacer";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // EncryptButton
            // 
            this.EncryptButton.Location = new System.Drawing.Point(15, 234);
            this.EncryptButton.Name = "EncryptButton";
            this.EncryptButton.Size = new System.Drawing.Size(75, 23);
            this.EncryptButton.TabIndex = 7;
            this.EncryptButton.Text = "Encrypt";
            this.EncryptButton.UseVisualStyleBackColor = true;
            this.EncryptButton.Click += new System.EventHandler(this.EncryptButton_Click);
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(15, 264);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(65, 13);
            this.label4.TabIndex = 8;
            this.label4.Text = "Credentials :";
            // 
            // ResultTextBox
            // 
            this.ResultTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.ResultTextBox.Location = new System.Drawing.Point(15, 281);
            this.ResultTextBox.Multiline = true;
            this.ResultTextBox.Name = "ResultTextBox";
            this.ResultTextBox.Size = new System.Drawing.Size(75, 116);
            this.ResultTextBox.TabIndex = 9;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 409);
            this.Controls.Add(this.ResultTextBox);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.EncryptButton);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.MdpTextBox);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.SaltTextBox);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.ClefPublicTextBox);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox ClefPublicTextBox;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox SaltTextBox;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox MdpTextBox;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button EncryptButton;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox ResultTextBox;
    }
}
et a chaque fois que j'appuie sur crypter, il me sort une clef différente (en lui passant les mêmes parametres).
J'ai regardé en mode debug, les tableaux sont bien rempli etc :/
Je vois pas où est l'erreur
 
A

Anonymous

Invité
#7
Apres avoir fais une capture et bien séparé les tableaux, voici ce que j'obtiens en snifant

Code:
mdp : kacper13

salt:

0x68, 0x31, 
0x32, 0x68, 0x77, 0x67, 0x6b, 0x74, 0x70, 0x26, 
0x23, 0x63, 0x6c, 0x2d, 0x75, 0x54, 0x7b, 0x7d, 
0x36, 0x3c, 0x44, 0x4a, 0x4a, 0x2e, 0x25, 0x56, 
0x5e, 0x2d, 0x37, 0x50, 0x2b, 0x4d

Key:

0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 
0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 
0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 
0x00, 0x8c, 0x41, 0x2a, 0x73, 0x75, 0xac, 0xf2, 
0xde, 0x78, 0xa2, 0x9a, 0x08, 0x20, 0x88, 0x8c, 
0xe5, 0xb4, 0x89, 0x1f, 0xb5, 0x39, 0x05, 0x7a, 
0x5a, 0x93, 0xc3, 0x24, 0xcf, 0x73, 0x52, 0x5e, 
0x58, 0x4f, 0xba, 0x50, 0x02, 0x95, 0x37, 0x2f, 
0x3f, 0xc2, 0xea, 0x61, 0x60, 0x72, 0x55, 0xc6, 
0x4b, 0x96, 0x10, 0x1a, 0xb2, 0x9c, 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5, 0x03, 0x59, 
0x74, 0xb5, 0x5c, 0xea, 0x4c, 0xde, 0xda, 0x9b, 
0xee, 0xe8, 0x04, 0xed, 0x92, 0x6d, 0xb8, 0x41, 
0x77, 0x4c, 0x7a, 0xb2, 0x44, 0x19, 0x5f, 0x40, 
0xe6, 0x15, 0x91, 0x46, 0x47, 0x32, 0xdc, 0xf7, 
0xa4, 0x61, 0x88, 0xf0, 0x32, 0x4c, 0x37, 0x2c, 
0xfb, 0xeb, 0xd3, 0x1e, 0x5a, 0x1a, 0x8f, 0x4f, 
0x32, 0xb3, 0xdb, 0x4d, 0xc8, 0xee, 0x42, 0x76, 
0x6f, 0x0e, 0x87, 0xf1, 0xff, 0x74, 0x3b, 0xfa, 
0x67, 0x4f, 0x25, 0xee, 0x7c, 0xe9, 0x01, 0xa2, 
0x48, 0x03, 0xe2, 0x97, 0x2c, 0xe7, 0x61, 0x7f, 
0x3e, 0x8e, 0xbd, 0xad, 0xe2, 0x87, 0xc6, 0x39, 
0xdc, 0xee, 0x73, 0x6b, 0xba, 0x17, 0xbf, 0x5b, 
0x25, 0x66, 0x3d, 0x49
j'essaye donc de retrouver le même resultat avec mon programme... mais rien a faire :/
 
A

Anonymous

Invité
#8
up ?
 
A

Anonymous

Invité
#9
Salut,

Je n'ai pas vraiment le temps d'expliquer, je te passe les class qui me servais pour crypter tout ça.

Je pense qu'elle marche encore
 
Haut Bas