Berryblue031
10th August 2011, 15:35
I would like to use encryption from the openssl library to encrypt saved user passwords
So create a function that takes the user's password as a QString and returns the encrypted version I can save with QSettings, and then reverse it to load the password and use it to login on the server.
I have an example function full implementation details can be found at
http://tldp.org/LDP/LGNET/87/vinayak.html
int encrypt (int infd, int outfd)
{
unsigned char outbuf[OP_SIZE];
int olen, tlen, n;
char inbuff[IP_SIZE];
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init (& ctx);
EVP_EncryptInit (& ctx, EVP_bf_cbc (), key, iv);
for (;;)
{
bzero (& inbuff, IP_SIZE);
if ((n = read (infd, inbuff, IP_SIZE)) == -1)
{
perror ("read error");
break;
}
else if (n == 0)
break;
if (EVP_EncryptUpdate (& ctx, outbuf, & olen, inbuff, n) != 1)
{
printf ("error in encrypt update\n");
return 0;
}
if (EVP_EncryptFinal (& ctx, outbuf + olen, & tlen) != 1)
{
printf ("error in encrypt final\n");
return 0;
}
olen += tlen;
if ((n = write (outfd, outbuf, olen)) == -1)
perror ("write error");
}
EVP_CIPHER_CTX_cleanup (& ctx);
return 1;
}
which I am trying to make Qt compatible
QString Encrypter::encrypt(const QString& s)
{
QString result;
bool processing = true;
bool success = true;
QBuffer source;
source.setData(s.toUtf8());
QBuffer final;
unsigned char outbuff[OP_SIZE];
int olen, tlen;
unsigned char inbuff[IP_SIZE];
quint64 chunkSize = IP_SIZE;
quint64 readSize;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init (&ctx);
EVP_EncryptInit (&ctx, EVP_bf_cbc (), key, iv);
while(processing)
{
//zero out input buffer
for(int i = 0; i < IP_SIZE; i++)
{
inbuff[i] = 0;
}
//populate input buffer with first chunk
readSize = source.read((char*)inbuff, chunkSize);
if(readSize == 0)
{
processing = false;
}
if(readSize == -1)
{
qDebug() << "encrypt read error";
success = false;
processing = false;
break;
}
if(processing)
{
if (EVP_EncryptUpdate (&ctx, outbuff, &olen, inbuff, readSize) != 1)
{
printf ("error in encrypt update\n");
success = false;
processing = false;
}
else
{
if (EVP_EncryptFinal (&ctx, outbuff + olen, &tlen) != 1)
{
printf ("error in encrypt final\n");
success = false;
processing = false;
}
}
olen += tlen;
readSize = final.write((char*)outbuff, olen);
if( readSize == -1 )
{
perror ("write error");
success = false;
processing = false;
}
}
}
EVP_CIPHER_CTX_cleanup (&ctx);
if(success)
{
result = QString(final.data());
}
//The encrypted string
return result;
}
But my function is failing right away with "encrypt read error". I know my solution isn't the most elegant but I was just hoping to get it working and then clean it up. I have a feeling problems are being caused by all of the different type conversions.
Help would be appreciated
So create a function that takes the user's password as a QString and returns the encrypted version I can save with QSettings, and then reverse it to load the password and use it to login on the server.
I have an example function full implementation details can be found at
http://tldp.org/LDP/LGNET/87/vinayak.html
int encrypt (int infd, int outfd)
{
unsigned char outbuf[OP_SIZE];
int olen, tlen, n;
char inbuff[IP_SIZE];
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init (& ctx);
EVP_EncryptInit (& ctx, EVP_bf_cbc (), key, iv);
for (;;)
{
bzero (& inbuff, IP_SIZE);
if ((n = read (infd, inbuff, IP_SIZE)) == -1)
{
perror ("read error");
break;
}
else if (n == 0)
break;
if (EVP_EncryptUpdate (& ctx, outbuf, & olen, inbuff, n) != 1)
{
printf ("error in encrypt update\n");
return 0;
}
if (EVP_EncryptFinal (& ctx, outbuf + olen, & tlen) != 1)
{
printf ("error in encrypt final\n");
return 0;
}
olen += tlen;
if ((n = write (outfd, outbuf, olen)) == -1)
perror ("write error");
}
EVP_CIPHER_CTX_cleanup (& ctx);
return 1;
}
which I am trying to make Qt compatible
QString Encrypter::encrypt(const QString& s)
{
QString result;
bool processing = true;
bool success = true;
QBuffer source;
source.setData(s.toUtf8());
QBuffer final;
unsigned char outbuff[OP_SIZE];
int olen, tlen;
unsigned char inbuff[IP_SIZE];
quint64 chunkSize = IP_SIZE;
quint64 readSize;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init (&ctx);
EVP_EncryptInit (&ctx, EVP_bf_cbc (), key, iv);
while(processing)
{
//zero out input buffer
for(int i = 0; i < IP_SIZE; i++)
{
inbuff[i] = 0;
}
//populate input buffer with first chunk
readSize = source.read((char*)inbuff, chunkSize);
if(readSize == 0)
{
processing = false;
}
if(readSize == -1)
{
qDebug() << "encrypt read error";
success = false;
processing = false;
break;
}
if(processing)
{
if (EVP_EncryptUpdate (&ctx, outbuff, &olen, inbuff, readSize) != 1)
{
printf ("error in encrypt update\n");
success = false;
processing = false;
}
else
{
if (EVP_EncryptFinal (&ctx, outbuff + olen, &tlen) != 1)
{
printf ("error in encrypt final\n");
success = false;
processing = false;
}
}
olen += tlen;
readSize = final.write((char*)outbuff, olen);
if( readSize == -1 )
{
perror ("write error");
success = false;
processing = false;
}
}
}
EVP_CIPHER_CTX_cleanup (&ctx);
if(success)
{
result = QString(final.data());
}
//The encrypted string
return result;
}
But my function is failing right away with "encrypt read error". I know my solution isn't the most elegant but I was just hoping to get it working and then clean it up. I have a feeling problems are being caused by all of the different type conversions.
Help would be appreciated