Re: DES3 encryption with padding

2012-10-18 Thread Brent Evans
Even with a null terminator appended to the char I'm seeing the same
problems.

On 17 October 2012 09:58, Ben Laurie  wrote:

> On Wed, Oct 17, 2012 at 9:52 AM, Brent Evans 
> wrote:
> > Hi,
> >
> > I'm currently trying to use the openSSL library to perform DES3
> encryption
> > on a string. The result from this encryption then has a base64 operation
> > performed on it, before this is passed to a Java application to decode
> the
> > base64 and unencrypt it.
> >
> > In the implementation below PKCS#5 padding is applied to the string
> before
> > it is encrypted.
> >
> > The strange thing at the moment as that the DES3 encryption is returning
> > extra, 'unexpected' data. This results in the base64 returning an
> incorrect
> > result, however if I only base64 up until the first line termination (\n)
> > then the base64 operation will return the correct string, with the Java
> > application then successfully unbasing and decoding it.
> >
> > Any ideas?
>
> Yeah. You pass outtext to the string constructor. Where do you think
> the NUL terminator will be?
>
> Also - why are you using ECB mode? Not generally a good idea.
>
> >
> >
> >
> >
> > std::string encrypt(const std::string& plainText)
> >
> > {
> >
> > int dif = 8 - (plainText.length() % 8);
> >
> > int length = (plainText.length() + dif);
> >
> >
> >
> > char *outtext = new char[length];
> >
> >
> >
> >   // pad the string so that it conforms to PKCS#5
> >
> > char padChar = static_cast(dif);
> >
> > std::string padStr;
> >
> > padStr.assign(dif, padChar);
> >
> >
> >
> > std::string unencryptedStr = plainText;
> >
> > unencryptedStr += padStr;
> >
> >
> >
> > DES_key_schedule ksched1;
> >
> > DES_key_schedule ksched2;
> >
> > DES_key_schedule ksched3;
> >
> >
> >
> > DES_set_key((DES_cblock *)"abcdefgh", &ksched1);
> >
> > DES_set_key((DES_cblock *)"ijklmnop", &ksched2);
> >
> > DES_set_key((DES_cblock *)"qrstuvwx", &ksched3);
> >
> >
> >
> > for (int i = 0; i < length; i += 8)
> >
> > {
> >
> > DES_ecb3_encrypt((DES_cblock *)(unencryptedStr.c_str() + i),
> >
> > (DES_cblock *)(outtext +
> i),
> >
> > &ksched1, &ksched2,
> > &ksched3, DES_ENCRYPT);
> >
> > }
> >
> >
> >
> > std::string result = std::string(outtext);
> >
> > return result;
> >
> > }
> >
> >
> > Thanks,
> >
> >
> > Brent
> __
> OpenSSL Project http://www.openssl.org
> User Support Mailing Listopenssl-users@openssl.org
> Automated List Manager   majord...@openssl.org
>


Re: DES3 encryption with padding

2012-10-17 Thread Ben Laurie
On Wed, Oct 17, 2012 at 9:52 AM, Brent Evans  wrote:
> Hi,
>
> I'm currently trying to use the openSSL library to perform DES3 encryption
> on a string. The result from this encryption then has a base64 operation
> performed on it, before this is passed to a Java application to decode the
> base64 and unencrypt it.
>
> In the implementation below PKCS#5 padding is applied to the string before
> it is encrypted.
>
> The strange thing at the moment as that the DES3 encryption is returning
> extra, 'unexpected' data. This results in the base64 returning an incorrect
> result, however if I only base64 up until the first line termination (\n)
> then the base64 operation will return the correct string, with the Java
> application then successfully unbasing and decoding it.
>
> Any ideas?

Yeah. You pass outtext to the string constructor. Where do you think
the NUL terminator will be?

Also - why are you using ECB mode? Not generally a good idea.

>
>
>
>
> std::string encrypt(const std::string& plainText)
>
> {
>
> int dif = 8 - (plainText.length() % 8);
>
> int length = (plainText.length() + dif);
>
>
>
> char *outtext = new char[length];
>
>
>
>   // pad the string so that it conforms to PKCS#5
>
> char padChar = static_cast(dif);
>
> std::string padStr;
>
> padStr.assign(dif, padChar);
>
>
>
> std::string unencryptedStr = plainText;
>
> unencryptedStr += padStr;
>
>
>
> DES_key_schedule ksched1;
>
> DES_key_schedule ksched2;
>
> DES_key_schedule ksched3;
>
>
>
> DES_set_key((DES_cblock *)"abcdefgh", &ksched1);
>
> DES_set_key((DES_cblock *)"ijklmnop", &ksched2);
>
> DES_set_key((DES_cblock *)"qrstuvwx", &ksched3);
>
>
>
> for (int i = 0; i < length; i += 8)
>
> {
>
> DES_ecb3_encrypt((DES_cblock *)(unencryptedStr.c_str() + i),
>
> (DES_cblock *)(outtext + i),
>
> &ksched1, &ksched2,
> &ksched3, DES_ENCRYPT);
>
> }
>
>
>
> std::string result = std::string(outtext);
>
> return result;
>
> }
>
>
> Thanks,
>
>
> Brent
__
OpenSSL Project http://www.openssl.org
User Support Mailing Listopenssl-users@openssl.org
Automated List Manager   majord...@openssl.org