Les Snippets

Connexion

Codage et décodage Base64

Niveau requis pour utiliser/comprendre cette source : 2 ( Initié )
Créé le 30/05/2006 15:53:16 et initié par turnerom [Liste]
Vue : 15242
Catégorie(s) : Réseau & Internet, Algorithme
Langages dispo pour ce code :
- C++
- Windev
- VB 2005



Langage : C++
Date ajout : 30/05/2006
Posté par turnerom [Liste]
// Base64.h

//*********************************************************************
//* C_Base64 - a simple base64 encoder and decoder.
//*
//*     Copyright (c) 1999, Bob Withers - bwit@pobox.com
//*
//* This code may be freely used for any purpose, either personal
//* or commercial, provided the authors copyright notice remains
//* intact.
//*********************************************************************
#ifndef __BASE64_H__
#define __BASE64_H__
#include <string>

const char         fillchar = '=';
                       // 00000000001111111111222222
                       // 01234567890123456789012345
static std::string cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                       // 22223333333333444444444455
                       // 67890123456789012345678901
                         "abcdefghijklmnopqrstuvwxyz"
                       // 555555556666
                       // 234567890123
                         "0123456789+/";
                         
class Base64
{
public:
    static std::string encode(std::string data);
    static std::string decode(std::string data);
};
#endif

// Base64.cpp
//*********************************************************************
//* Base64 - a simple base64 encoder and decoder.
//*
//*     Copyright (c) 1999, Bob Withers - bwit@pobox.com
//*
//* This code may be freely used for any purpose, either personal
//* or commercial, provided the authors copyright notice remains
//* intact.
//*********************************************************************
#include "base64.h"
using namespace std;
string Base64::encode(string data)
{
    auto     string::size_type  i;
    auto     char               c;
    auto     string::size_type  len = data.length();
    auto     string             ret;
    for (i = 0; i < len; ++i)
    {
        c = (data[i] >> 2) & 0x3f;
        ret.append(1, cvt[c]);
        c = (data[i] << 4) & 0x3f;
        if (++i < len)
            c |= (data[i] >> 4) & 0x0f;
        ret.append(1, cvt[c]);
        if (i < len)
        {
            c = (data[i] << 2) & 0x3f;
            if (++i < len)
                c |= (data[i] >> 6) & 0x03;
            ret.append(1, cvt[c]);
        }
        else
        {
            ++i;
            ret.append(1, fillchar);
        }
        if (i < len)
        {
            c = data[i] & 0x3f;
            ret.append(1, cvt[c]);
        }
        else
        {
            ret.append(1, fillchar);
        }
    }
    return(ret);
}
string Base64::decode(string data)
{
    auto     string::size_type  i;
    auto     char               c;
    auto     char               c1;
    auto     string::size_type  len = data.length();
    auto     string             ret;
    for (i = 0; i < len; ++i)
    {
        c = (char) cvt.find(data[i]);
        ++i;
        c1 = (char) cvt.find(data[i]);
        c = (c << 2) | ((c1 >> 4) & 0x3);
        ret.append(1, c);
        if (++i < len)
        {
            c = data[i];
            if (fillchar == c)
                break;
            c = (char) cvt.find(c);
            c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
            ret.append(1, c1);
        }
        if (++i < len)
        {
            c1 = data[i];
            if (fillchar == c1)
                break;
            c1 = (char) cvt.find(c1);
            c = ((c << 6) & 0xc0) | c1;
            ret.append(1, c);
        }
    }
    return(ret);
}


Remarque :
Ce code n'est pas de moi, voir le code source pour l'auteur!
Langage : Windev
Date ajout : 15/06/2006
Posté par Elian Lacroix [Liste]
Chaine, Chaine64 sont des chaines
// Codage de "chaine" en base 64, résultat dans "chaine64"
Chaine64 = Crypte(maChaine, "", crypteAucun, Vrai)
// L'inverse 
MaChaine = Décrypte(Chaine64, "", crypteAucun, Vrai)

Langage : VB 2005
Date ajout : 22/07/2008
Posté par gillardg [Liste]

'///******* Decrypter le texte *******\\\

Public Function GetDecryptedData(ByVal Data As String) As String
Dim dEC_data() As Byte = Convert.FromBase64String(Data)Dim dEC_Str As String = ASCIIEncoding.ASCII.GetString(dEC_data) 
GetDecryptedData = dEC_Str

End Function



'///******* crypter le texte *******\\\

Public Function GetEncryptedData(ByVal Data As String) As String
Dim shaM As New SHA1Managed 
Convert.ToBase64String(shaM.ComputeHash(Encoding.ASCII.GetBytes(Data)))
Dim eNC_data() As Byte = ASCIIEncoding.ASCII.GetBytes(Data)Dim eNC_str As String = Convert.ToBase64String(eNC_data) 
GetEncryptedData = eNC_str
End Function

'le code n'est pas de moi 

' trouvé sur vbfrance.com


Snippets en rapport avec : Base64, Codage, Décodage



Codes sources en rapport avec : Base64, Codage, Décodage

{C / C++ / C++.NET} CODES LINÉAIRES
Cette application est démonstration et un outil de codage et décodage avec les codes correcteurs lin...

{C / C++ / C++.NET} FAST BASE64 / UUENCODING ENCODAGE/DECODAGE
Classes C++ permettant de coder/décoder rapidement et simplement une string en/depuis Base64/Uuencod...

{Visual Basic, VB6, VB.NET, VB 2005} CODAGE EN BASE 64
Description _________________ Ce module a inclure dans vos applications fourni deux procedures po...

{Delphi} BASE64/BASE64URL ENCODE/DECODE
EDBase64 est une adaptation Delphi du code C/CPP de Turnerom lien de la source : http://www.cpp...

{Visual Basic, VB6, VB.NET, VB 2005} TOBASE64 IMAGE ET TEXTE
Application permettant d'encoder et de décoder une image ou un texte en base 64. En outre elle per...

{C# / C#.NET} SCRIPT ENGINE (UN DÉBUT)
J'ai recherché activement un moteur de script pour un jeu que je refais en C# XNA et je n'ai pas réu...

{Visual Basic, VB6, VB.NET, VB 2005} CRYPTAGE\DECRYPTAGE AVEC DES ALPHABETS DE CRYPTAGES MULTIPLES
En fait ce code présente trois grands rôles : >> Tout d'abord on a la fonction du cryptage, il es...

{C / C++ / C++.NET} SHAMAN LIBRAIRIE DE HASH SUPPORTANT SHA1 SHA256 SHA384 SHA512 MD5 BASE64
Shaman une DLL/SO(pour POSIX) que j'ai ecrit en C pour un autre projet en fac , qui permet de hasher...

{C / C++ / C++.NET} CODE DECODE BASE64 (WIN32)
Pour question forum. Encodage et décodage du format MIME (base64). Exemple fait fichierfichier. T...

{Visual Basic, VB6, VB.NET, VB 2005} .NET ALGO HUFFMAN RLE MTF
Il y a 1 ans j'ai essayé de me lancer dans la compression des données mais sans succès, aujourd'hui...