View Full Version : [c]Convertire base64 in ascii
Ho bisogno di convertire del testo scritto in base64 in ascii,
i dati che devo convertire mi vengono da un socket tcp.
il problema fondamentale e come prendere correttamente 6 bit alla volta (della codifica base64) invece di 8 bit dimenzione minima dei tipi nel c
ilsensine
17-03-2005, 09:53
http://www.koders.com/c/fidFDC554B9F9F6340C894141C53BE08C5299D93B5F.aspx
Ho visto il link ed per fare una cosa a mio parere semplice usa metri e metri di codice.
Sto cercando di implementare diretamente io il codice di conversione e ditemi se io ho:
struct base64{int val:6;};
Ed eseguo poi le segunati operazioni:
struct base64 *b64=malloc(len*8/6*sizeof(struct base64));
memcpy(b64,valori,len);
dove valori è l'area di memoria dove ho i dati e len e la lunghezza dell'area di memoria contenente i dati.
potrei prendere il numero del carattere in base64 facendo semplicemente:
x=b64.val;
ma molto probabilmente questo metodo non funziona perche il record a una dimenzione minore di 8 bit, ma l'area di memoria allocata viene allocada come blocco di 8 bit per record..
Chiedo conferma..
Questo l'ho scritto io in C++, ma c'è talmente poco C++ che non dovresti avere problemi (anche se con la string risolvo i problemi di allocazione)...
#include <iostream>
#include <string>
using namespace std;
string Base64Encode(const string &data)
{
const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned int dataLen = data.length();
string res;
for(int i=0; i<dataLen; i+=3)
{
unsigned int triplet;
char tmp[5];
triplet = (data[i] << 16)
+ (((dataLen > (i+1))?data[i+1]:0) << 8)
+ ((dataLen > (i+2))?data[i+2]:0);
tmp[3] = base64[triplet & 0x3F];
tmp[2] = base64[(triplet >> 6) & 0x3F];
tmp[1] = base64[(triplet >> 12) & 0x3F];
tmp[0] = base64[(triplet >> 18) & 0x3F];
tmp[4] = '\0';
res.append(tmp);
}
switch(dataLen % 3)
{
case 1:
res[res.length()-2] = '=';
case 2:
res[res.length()-1] = '=';
}
return res;
}
inline int getBase64Pos(const char c)
{
switch(c)
{
case '+':
return 62;
case '/':
return 63;
default:
if(c >= 'A' && c <= 'Z')
return c - 'A';
else if(c >= 'a' && c <= 'z')
return 26 + c - 'a';
else if(c >= '0' && c <= '9')
return 52 + c - '0';
else
return 0;
}
return -1;
}
string Base64Decode(const string &data)
{
unsigned int dataLen = data.length();
string res("");
if(res[dataLen-1] == '=')
--dataLen;
if(res[dataLen-1] == '=')
--dataLen;
unsigned int buf;
char tmp[3];
for(int i=0; i<dataLen; i+=4)
{
int t1 = 0, t2 = 0, t3 = 0, t4 = 0;
buf = 0;
t1 = getBase64Pos(data[i]);
t2 = getBase64Pos((dataLen > (i+1)) ? data[i+1]:'A');
t3 = getBase64Pos((dataLen > (i+2)) ? data[i+2]:'A');
t4 = getBase64Pos((dataLen > (i+3)) ? data[i+3]:'A');
if(t1 < 0 || t2 < 0 || t3 < 0 || t4 < 0)
return res;
tmp[0] = (t1 << 2) + (t2 >> 4);
tmp[1] = (t2 << 4) + (t3 >> 2);
tmp[2] = (t3 << 6) + t4;
tmp[3] = '\0';
res.append(tmp);
}
return res;
}
Bello e fatto bene mi piace domani lo provo, mi serve per il mio progetto di un web server per interpretare le autorizzazioni inviate con
[code]
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
[code]
in relazione con questo velevo sapere se avevate anche delle informazioni a riguardo questa funzione, sto leggendo rfc 2068
dove c'e scritto che dentro al codice in base64 che dorebbe essere quello che inizia con la Q c'e username e password separate da ":" ma il carattere ":" non esiste in base64 come funziona..
Grazie e ciao..
Ma il ":" è nella stringa in chiaro...e dopo viene codificato...
questo è quello che sta scritto sulla specifica rfc2068
Basic Authentication Scheme
The "basic" authentication scheme is based on the model that the user
agent must authenticate itself with a user-ID and a password for each
realm. The realm value should be considered an opaque string which
can only be compared for equality with other realms on that server.
The server will service the request only if it can validate the
user-ID and password for the protection space of the Request-URI.
There are no optional authentication parameters.
Upon receipt of an unauthorized request for a URI within the
protection space, the server MAY respond with a challenge like the
following:
WWW-Authenticate: Basic realm="WallyWorld"
where "WallyWorld" is the string assigned by the server to identify
the protection space of the Request-URI.
To receive authorization, the client sends the userid and password,
separated by a single colon (":") character, within a base64 encoded
string in the credentials.
basic-credentials = "Basic" SP basic-cookie
basic-cookie = <base64 [7] encoding of user-pass,
except not limited to 76 char/line>
user-pass = userid ":" password
userid = *<TEXT excluding ":">
password = *TEXT
Userids might be case sensitive.
If the user agent wishes to send the userid "Aladdin" and password
"open sesame", it would use the following header field:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Io no capisco bene l'inglese ma da quello che ho capito sembra che la stringa che contiente username e password contiene anche un carattere ":" in formato ascii che separa i due valori..
Ditemi se ho capito bene ho sbagliato qualcosa..
Ciao
Come ti dicevo metti in chiaro userid:password e poi lo codifichi in base64...
No scusa non mi sono spiegato bene, sto creando un web server e quindi la stringa la ricevo dal client e io devo capirla (e io non capisc niente =-) ) poi scusa il carattere ":" non esiste nella codifica base64
Grazie ancora ciao.
Il carattere : non esiste nella stringa codificata, ma nella stringa decodificata... Ricevi la stringa di autorizzazione, la decodifichi e dopo cerchi i : a partire dall'inizio della stringa...
non ci ho capito niente ma il tu codice ha funzinato vittoria
ho scritto ciao come user e ciao come pass e....
questo e lo schifo inviato: Y2lhbzpjaWFv
questo è il risultato della tua funzione: ciao:ciao
Grazie e mi faresti un favore se me lo spieghi in dettaglio
haaa ho beccato un errorino stupido all'inizio:
if(res[dataLen-1] == '=')
--dataLen;
if(res[dataLen-1] == '=')
--dataLen;
invece di:
if(data[dataLen-1] == '=')
--dataLen;
if(data[dataLen-1] == '=')
--dataLen;
Anche gli dei possono sbagliare
Ciao ..
Hai ragione, ma probabilmente l'errore era in una versione parziale, sono andato a recuperarla da un vecchio backup...anche perchè il server di posta per cui l'ho sviluppato funzionava egregiamente ;)
Riprovo a spiegarti il problema dei due punti...è la stringa in chiaro che deve contenere username e password separati dai due punti... E quindi la stringa codificata in base 64 la devi passare a Base64Decode che ti ritorna username e password in chiaro separati dai due punti...
Si questo è chiaro l'unica cosa che non avevo capito è come funzionava in generale la codifica in base64,
questa dorebbe funzionare nel seguente modo:
ho una serie di dati come ad esempio una stringa,leggo questi dati sei bit per volta e a seconda del valore dei sei bit inserisco il carattere corrispondente
per decodificare invece:
ho una stringa decodificata in base64, a seconda del carattere che ho mi ricavo il valore dei sei bit, e poi li riunisco nella mia codifica..
Giusto?? se è cosi adesso ho capito tutto..
Grazie di tutto e ciao..
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.