[Botan-devel] Encryption in java and decryption in C++

Jack Lloyd lloyd at randombit.net
Wed Oct 19 11:25:01 EDT 2005


Hi David,

I believe you may have made a mistake in your Java code. It is hard for me to
be sure as the code you sent to the list does not compile for me due to missing
classes, but I whipped some quick code for both JCE and Botan which encrypts
the string "141593" using the key "9d8ff7a4e575761f" using DES/ECB/PKCS5Padding
(you say CBC in your message, but your code is using ECB). Both programs agree
that the correct value is "4aa4f4df343ef20b". I have attached both for you to
take a look at.

In your C++ code, you appear to be treating the input string "141593" as a hex
string, while the Java code seems to treat it as a decimal number and then take
the binary representation of that. This disparity may be the cause of the
problem.

Jack

On Tue, Oct 18, 2005 at 03:17:43PM +0200, delannoy wrote:
> Hello,
> 
> I'm working on a project implemented in java (1.5) and c++ (3.3). The 
> software is composed of a java interface which is running some C++ 
> binaries. The program  contains more than one part. I would like to use 
> a block cipher algorithm to filter the use of each part according to a  
> user key delivered with the software.
> For the moment, I'm generating a symmetric secret key with the Java 
> Cryptography Extension. It works well! Then I'm generating a user key in 
> java with a DES/CBC/PKCS5Padding algorithm which is working fine, it 
> means that I'm doing the encryption in java.
> 
> However I need to do the decryption in C++ and I have some problems. I 
> use the Botan 1.4.7 package and I don't succeed in decrypting the 
> message with the same algorithm.
> 
> Example :
> My java generated key is : 9d8ff7a4e575761f (See GenerateSecretKey.java 
> in attachment)
> Thanks to this key, i'm generating a symmetric user key :
> message to encrypt : 141593
> encrypted message : e410ac78755ab496
> with algorithm : DES/CBC/PKCS5Padding (see SymmetricCipher.java for 
> encryption and decryption)
> When i decrypt the message, i obtain the original message.
> 
> My C++ code is :
> 
> #include <botan/base.h>
> #include <botan/init.h>
> #include <botan/des.h>
> #include <botan/blowfish.h>
> using namespace Botan;
> #include <fstream>
> #include <iostream>
> #include <string>
> #include <vector>
> #include <cstring>
> #include <botan/look_add.h>
> #include <botan/lookup.h>
> #include <botan/filters.h>
> 
> int main() {
>   LibraryInitializer init;
>   SymmetricKey key("9d8ff7a4e575761f");
>   Pipe enc(get_cipher("DES/ECB/PKCS7", key, ENCRYPTION), new Hex_Encoder);
>   Pipe dec(new Hex_Decoder, get_cipher("DES/ECB/PKCS7", key, DECRYPTION));
>   std::string secret = 141593;
>   std::cout << "The secret message is '" << secret << "'" << std::endl;
>   enc.process_msg(secret);
>   std::string cipher = enc.read_all_as_string();
>   std::cout << "The encrypted secret message is " << cipher << std::endl;
>   dec.process_msg(cipher);
>   std::string bubu = dec.read_all_as_string();
>   std::cout << "The decrypted secret message is '"<< bubu << "'" << 
> std::endl;
> }
> 
> The encrypted message by C++ and Java is not the same, when message is 
> 141593 and using the same key.
> Do you think it's possible de encrypt in java and decrypt in C++? If 
> yes, any idea on what's wrong with my algorithm?
> 
> Note that, when I try to decrypt the java encrypted message in C++ , I 
> get the following error "decoding error : PKCS7".
> Thank you very much for your help.
> 
> David
> 
> -- 
> David Delannoy
> INRA,BIA, BP27 31326 Castanet-Tolosan, France.
> Phone: +33 5 61 28 50 71
> Fax: +33 5 61 28 53 35 
> mailto:delannoy at toulouse.inra.fr
> 




> _______________________________________________
> botan-devel mailing list
> botan-devel at randombit.net
> http://www.randombit.net/mailman/listinfo/botan-devel

-------------- next part --------------

import javax.crypto.SecretKey;
import javax.crypto.Cipher;
import java.lang.reflect.Array;

import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;

public class SymmetricCipher {
   public static byte[] encrypt(byte[] inpBytes,
                                SecretKey key,
                                String xform) throws Exception
      {
      Cipher cipher = Cipher.getInstance(xform);
      cipher.init(Cipher.ENCRYPT_MODE, key);
      return cipher.doFinal(inpBytes);
      }

   public static SecretKeySpec getSecretKey()
      {
      byte key[] = { (byte)0x9d, (byte)0x8f, (byte)0xf7, (byte)0xa4,
                     (byte)0xe5, (byte)0x75, (byte)0x76, (byte)0x1f };

      System.out.println("Key: " + toHex(key));

      SecretKeySpec secretkey = new SecretKeySpec(key, "DES");
      return secretkey;
      }

   public static String toHex(byte[] bytes)
      {
      StringBuffer s = new StringBuffer();
      for(int i = 0; i < bytes.length; i++)
         {
         if ((bytes[i] & 0xff) <= 0xf) s.append("0");
         s.append(Integer.toHexString(bytes[i] & 0xff));
         }
      return s.toString();
      }

   public static void main(String[] unused) throws Exception
      {
      byte msg[] = { 0x14, 0x15, (byte)0x93 };

      String xform = "DES/ECB/PKCS5Padding";
      SecretKeySpec key = getSecretKey();
      System.out.println("Plaintext: " + toHex(msg));
      byte[] encBytes = encrypt(msg, key, xform);
      System.out.println("Ciphertext: " + toHex(encBytes));
      }
   }
-------------- next part --------------
#include <botan/botan.h>
using namespace Botan;

#include <iostream>

int main()
   {
   try {
      LibraryInitializer init;

      SymmetricKey key("9d8ff7a4e575761f");

      std::string plain = "141593";

      Pipe pipe(new Hex_Decoder,
                get_cipher("DES/ECB/PKCS7", key, ENCRYPTION),
                new Hex_Encoder);

      pipe.process_msg(plain);

      std::cout << pipe.read_all_as_string() << std::endl;
   }
   catch(std::exception& e)
      {
      std::cout << e.what() << std::endl;
      return 1;
      }
   return 0;
   }


More information about the botan-devel mailing list