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 : 19016
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} LECTURE DE FICHIER .EML
Mes anciens Email sont stockés sous forme de fichiers textes .eml, j'ai des difficultés pour retrouv...

{Visual Basic, VB6, VB.NET, VB 2005} EXTRAIRE UNE IMAGE CODÉE EN B64 D'UNE PAGE.HTM
Bonjour Ce Classeur est une approche de la façon d' EXTRAIRE des ELEMENTS (texte, Image...) d'une P...

{Python} BASE64 ENCRYPT/DECRYPT PYTHON BY MAXOU56800
Programme permettant d'encrypter et de decrypter une chaine de caractere en Texte lisible ou en Base...

{Delphi} CODAGE EN BASE64 DE CHAÎNES UNICODE
Je cherchais depuis un certain temps comment faire pour coder des chaînes Unicode en base64 mais ne ...

{JAVA / J2EE} CODE SOURCE EDITEUR BINAIRE
CONVERSION DES NOMBRES VERS DIFFÉRENCES BASES(OCTAL, DÉCIMAL, HEXADÉCIMAL) ET DE TEXTE EN BINAIRE(CO...

{JAVA / J2EE} CODAGE EN HILLCYPHER
Ce code fait le codage d'une chaine de deux caractères mais vous pouvez augmenT la dimension de la c...

{JAVA / J2EE} COMPRESSION DE HUFFMAN
salut, j´ai commencé a programmer en java il ya 3 mois. Actuellement j´essaye de comprend...