On Wed, Oct 17, 2012 at 9:52 AM, Brent Evans <brentevan...@gmail.com> 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<unsigned char>(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 List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to