Msa cryptage non reversible par matrice semi-aléatoire

Contenu du snippet

Petit algo de cryptage par cryptage semi-aléatoire
je l'ai tester avec plusieur chaine tres ressenblante , et je n'ai pas trouvé de collision, ni de bug

exemple :
Texte original : aa
Resultat : H-D9-E/CD/D/9C/E

Texte original : bb
Resultat : I+E=+F*DE*E*=D*F

Texte original : ab
Resultat : H+D=-F/DD*D*9D/F

Texte original : ba
Resultat : I-E9+E*CE/E/=C*E

Texte original : poiuytreza
Resultat : Yb29fYtV2/SjrVmI

Texte original : pzertyuiop
Resultat : 1fRibVo0RkSum0pM

Source / Exemple :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YohanFrameWork.Security
{
    public static class YFW_MSA
    {

        private static Char GetChatAtPos(int pos)
        {
            String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789=/*-+.";
            return s[pos % s.Length];
        }

        /// <summary>
        /// Creation d'une matrice de 256 * 256
        /// </summary>
        /// <param name="target">Chaine a crypter</param>
        /// <returns></returns>
        private static Int32[,] Matrice(String target)
        {
            Int32[,] Matrice = new Int32[256, 256];
            int posInTarget = 0;
            for (int i = 0; i < 256; i++)
            {
                for (int j = 0; j < 256; j++)
                {
                    // A chaque élément de la matrice , on ajoute la valeur de i * j
                    Matrice[i, j] = target[posInTarget] + (i * j);

                    if (++posInTarget >= target.Length)
                        posInTarget = 0;
                }
            }
            return Matrice;
        }

        /// <summary>
        /// Divise la matrice par deux et opére un xor
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        private static Int32[,] ComputeMatrice(Int32[,] target)
        {
            if (target.GetLength(0) < 8)
                return target;

            // Creation d'une nouvelle matrice de taille inferieur de 4 par rapport a target
            Int32[,] matrice = new Int32[target.GetLength(0) / 2, target.GetLength(1) / 2];

            for (int i = 0; i < matrice.GetLength(0); i++)
            {
                for (int j = 0; j < matrice.GetLength(1); j++)
                {
                    matrice[(matrice.GetLength(0) - 1) - i, (matrice.GetLength(1) - 1) - j] ^= target[(target.GetLength(0) / 2) + i, (target.GetLength(1) / 2) + j];
                }
            }

            return matrice;
        }

        /// <summary>
        /// Crypte la chaine sur un algo a  matice semi-aléatoire ( ce n'est pas reversible )
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public static String MSA_Crypte(this String target)
        {
            Int32[,] matrice = Matrice(target);

            // Reduction de la matrice a 4 * 4
            while (matrice.GetLength(0) > 4 && matrice.GetLength(1) > 4)
            {
                matrice = ComputeMatrice(matrice);
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    sb.Append(GetChatAtPos(matrice[i, j]));
                }
            }
            return sb.ToString();
        }
    }
}

Conclusion :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YohanFrameWork.Security;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
string s = null;
s = @"aa";
Console.WriteLine("Texte original : {0}\nResultat : {1}\n", s, s.MSA_Crypte());

s = @"bb";
Console.WriteLine("Texte original : {0}\nResultat : {1}\n", s, s.MSA_Crypte());

s = @"ab";
Console.WriteLine("Texte original : {0}\nResultat : {1}\n", s, s.MSA_Crypte());

s = @"ba";
Console.WriteLine("Texte original : {0}\nResultat : {1}\n", s, s.MSA_Crypte());

s = @"poiuytreza";
Console.WriteLine("Texte original : {0}\nResultat : {1}\n", s, s.MSA_Crypte());

s = @"pzertyuiop";
Console.WriteLine("Texte original : {0}\nResultat : {1}\n", s, s.MSA_Crypte());

}
catch (ArgumentException ex )
{

Console.WriteLine(ex.Message);
}
Console.Read();
}
}
}

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.