[c++] & sfml cryptographie

Contenu du snippet

Petit code qui "brouille un texte" dans une image...
Oui, qui brouille, au départ j'ai tenté de caché totalement le texte dans l'image en l'éparpillant, mais je cherche

encore un moyen de mélanger les pixels correctement.
Dans ce programme, j'ai juste fait des inversion 2 par 2 de lignes et de colones de pixels.

Voila le principe :

On écrit dans paint un texte sur un fond vert (la couleur de fond doit donc être 0,255,0 dans Couleurs/Modifier les

couleurs... dans paint). Cette image doit être nommée texte.bmp.

Ensuite on prend une autre image, n'importe quel format.

On met les deux à coté de l'executable.

On ouvre le programme, là il demande le nom de l'image (la derniere évoquée),
puis le nom d'enregistrement (n'importe quoi).

On valide, puis on se met sur la fenetre graphique.
On peut voir l'image de départ. On appuie sur une touche, et les pixels sont mélangés, et le texte qui est extrait

de l'image texte.bmp se colle sur ce mélange.
On rappuie est on peut voir l'image remise en place, et le texte brouillé.
En quittant cette image est enregistrée sous le nom précédement rentré.

Pour "débrouiller" le texte, on réouvre le programme, on choisit "décrypter", est les étapes sont quasi-similaire.

Le programme, les dlls et un exemple sont disponibles à cette adresse : http://dl.free.fr/getfile.pl?file=/EyfTgLKj

Source / Exemple :


/// RY-CRYPTEUR
// disponible sur http://dl.free.fr/getfile.pl?file=/EyfTgLKj

#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>

int main()
{

    std::cout << " Bienvenue\n\n";
    std::cout << "Cryptage (1)\nDecryptage (2)\n\nVotre choix : ";
    int choix;
    std::cin >> choix;

    std::string chemin;
    std::cout << "\n\n\nNom de l'image : \n";
    std::cin >> chemin;
    std::string NomFin;
    if (choix != 1 && choix != 2)
    {
        EXIT_FAILURE;
    }

    std::cout << "\n\nNom d'enregistrement : \n";
    std::cin >> NomFin;

    system ("PAUSE");

    sf::RenderWindow App(sf::VideoMode(1024, 768), "SFML Views");

    sf::Image Image;
    if (!Image.LoadFromFile(chemin))
        return EXIT_FAILURE;
    sf::Sprite Background(Image);

    Background.Resize(1024, 768);

    App.Clear (sf::Color (255,255,255));
    App.Draw(Background);
    App.Display();

//////////////////// VARIABLES //////////////////////

    /*int mtaillex = Image.GetWidth();
    int mtailley = Image.GetHeight();
    int TAILLEX = (mtaillex / 400) - 1;
    int TAILLEY = (mtailley / 400) - 1 ;*/
    int TAILLEX = 1;
    int TAILLEY = 1;

    std::cout << "\n Taille x: " << TAILLEX << "\n\n";
    std::cout << "\n Taille y: " << TAILLEY << "\n\n";

    sf::Color map [TAILLEX] [TAILLEY];
    sf::Color temp [TAILLEX] [TAILLEY];

    bool next = false;

///////////////////// ATTENTE /////////////////////////

    std::cout << "\n\nVeuillez appuyez sur une touche pour continuer.\n\n";

    next = false;
    while (!next)
    {

        sf::Event Event;
        while (App.GetEvent(Event))
        {

            if ((Event.Type == sf::Event::KeyPressed))
                next = true;
            if (Event.Type == sf::Event::Closed)
                App.Close();

            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
        }

        App.Clear();
        App.Draw(Background);
        App.Display();
    }

/////////////////// TRAITEMENT //////////////////////

/// En x

    for (int j = 0; j < Image.GetHeight(); j+= (TAILLEY))
    {
        if ((j + TAILLEY) < Image.GetHeight())
        {
            for (int i = 0; i < Image.GetWidth(); i+= (TAILLEX*2))
            {
                if ((i + 2* TAILLEX) < Image.GetWidth())
                {
                    for (int y = 0; y < TAILLEY; y++)
                    {
                        for (int x = 0; x < TAILLEX; x++)
                        {
                            map [x] [y] = Image.GetPixel((i+x),(j+y));
                            temp [x][y] = Image.GetPixel ((i+TAILLEX+x), (j+y));
                        }
                    }
                    for (int y = 0; y < TAILLEY; y++)
                    {
                        for (int x = 0; x < TAILLEX; x++)
                        {
                            Image.SetPixel ((i+x), (j+y), temp [x] [y]);
                            Image.SetPixel ((i+x+TAILLEX),(j+y), map [x][y]);
                        }
                    }

                }
            }
        }
    }

/// En y
    for (int i = 0; i < Image.GetWidth(); i+= (TAILLEX))
    {
        if ((i + 2* TAILLEX) < Image.GetWidth())
        {
            for (int j = 0; j < Image.GetHeight(); j+= (TAILLEY * 2))
            {
                if ((j + TAILLEY) < Image.GetHeight())
                {

                    for (int y = 0; y < TAILLEY; y++)
                    {
                        for (int x = 0; x < TAILLEX; x++)
                        {
                            map [x] [y] = Image.GetPixel((i+x),(j+y));
                            temp [x][y] = Image.GetPixel ((i+x), (TAILLEY+j+y));
                        }
                    }
                    for (int y = 0; y < TAILLEY; y++)
                    {
                        for (int x = 0; x < TAILLEX; x++)
                        {
                            Image.SetPixel ((i+x), (j+y), temp [x] [y]);
                            Image.SetPixel ((i+x),(TAILLEY +j+y), map [x][y]);
                        }
                    }

                }
            }
        }
    }

//////// MISE EN PLACE DE L'OBJET
    if (choix == 1)
    {

        sf::Image imgTexte;

        if (!imgTexte.LoadFromFile("texte.bmp"))
            return EXIT_FAILURE;

        for (int i = 0; i < imgTexte.GetWidth(); i++)
        {
            for (int j = 0; j < imgTexte.GetHeight(); j++)
            {
                if (imgTexte.GetPixel(i,j) != sf::Color (0,255,0))
                {
                    Image.SetPixel (i, j, imgTexte.GetPixel(i,j));
                }
            }
        }

    }

///////////////////// ATTENTE /////////////////////////

    std::cout << "\n\nVeuillez appuyez sur une touche pour continuer.\n\n";

    next = false;
    while (!next)
    {

        sf::Event Event;
        while (App.GetEvent(Event))
        {

            if ((Event.Type == sf::Event::KeyPressed))
                next = true;
            if (Event.Type == sf::Event::Closed)
                App.Close();

            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
        }

        App.Clear();
        App.Draw(Background);
        App.Display();
    }

////////////////////// Enregistrement //////////////////

    if (choix == 2)
    {
        Image.SaveToFile (NomFin);
        system ("cls");
        std::cout << "Vous pouvez fermer la fenetre.\n\n";
    }

/////////////////// REMISE EN PLACE //////////////////////

/// En y
    for (int i = 0; i < Image.GetWidth(); i+= (TAILLEX))
    {
        if ((i + 2* TAILLEX) < Image.GetWidth())
        {
            for (int j = 0; j < Image.GetHeight(); j+= (TAILLEY * 2))
            {
                if ((j + TAILLEY) < Image.GetHeight())
                {

                    for (int y = 0; y < TAILLEY; y++)
                    {
                        for (int x = 0; x < TAILLEX; x++)
                        {
                            map [x] [y] = Image.GetPixel((i+x),(j+y));
                            temp [x][y] = Image.GetPixel ((i+x), (TAILLEY +j+y));
                        }
                    }
                    for (int y = 0; y < TAILLEY; y++)
                    {
                        for (int x = 0; x < TAILLEX; x++)
                        {
                            Image.SetPixel ((i+x), (j+y), temp [x] [y]);
                            Image.SetPixel ((i+x),(TAILLEY + j+y), map [x][y]);
                        }
                    }

                }
            }
        }
    }

/// En x

    for (int j = 0; j < Image.GetHeight(); j+= (TAILLEY))
    {
        if ((j + TAILLEY) < Image.GetHeight())
        {
            for (int i = 0; i < Image.GetWidth(); i+= (TAILLEX*2))
            {
                if ((i + 2* TAILLEX) < Image.GetWidth())
                {
                    for (int y = 0; y < TAILLEY; y++)
                    {
                        for (int x = 0; x < TAILLEX; x++)
                        {
                            map [x] [y] = Image.GetPixel((i+x),(j+y));
                            temp [x][y] = Image.GetPixel ((i+TAILLEX+x), (j+y));
                        }
                    }
                    for (int y = 0; y < TAILLEY; y++)
                    {
                        for (int x = 0; x < TAILLEX; x++)
                        {
                            Image.SetPixel ((i+x), (j+y), temp [x] [y]);
                            Image.SetPixel ((i+x+TAILLEX),(j+y), map [x][y]);
                        }
                    }

                }
            }
        }
    }

////////////////////// Enregistrement //////////////////

    if (choix == 1)
    {
        Image.SaveToFile (NomFin);
        system ("cls");
        std::cout << "Vous pouvez fermer la fenetre.\n\n";
    }

///////////////////// ATTENTE /////////////////////////

    App.Clear (sf::Color (255,255,255));
    App.Draw(Background);
    App.Display();

    while (App.IsOpened())
    {

        sf::Event Event;
        while (App.GetEvent(Event))
        {

            if (Event.Type == sf::Event::Closed)
                App.Close();

            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
        }

        App.Clear();
        App.Draw(Background);
        App.Display();
    }

    return EXIT_SUCCESS;
}

Conclusion :


Bon, j'avoue que le code à beau être en C++, il n'est pas très orienté objet.
Mais bon, c'était un petit défi que je m'étais lancé.

Je sais que ce programme est loin (très loin) d'être parfait, et je ne rejetterais pas vos commentaires ainsi vos idées :)

D'ailleurs si quelqu'un connait une façon de vraiment mélanger les pixels de façon mathématique...

M'enfin voila, http://dl.free.fr/getfile.pl?file=/EyfTgLKj

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.