The Wayback Machine - https://web.archive.org/all/20060901045834/http://www.bouncycastle.org/csharpdevmailarchive/msg00018.html

dev-crypto-csharp mail archive

[dev-crypto-csharp] Re: [dev-crypto] Sombody tried to integrate C# and Java with BC??

Hi there,

String encoding in C# is done using the System.Text.Encoding class.

Java:     new String(xBytes, "UTF-8")
C#:       Encoding.UTF8.GetString(xBytes)

Java:     xString.getBytes("UTF-8")
C#:       Encoding.UTF8.GetBytes(xString)


I have attached a C# version of the Java class you gave that works as expected.


Please note that I am using the latest C# build so you may have to change the casing in some names to get it to build as you seem to be using an earlier version.

Cheers,
Pete.

P.S. Questions about porting from Java to C# probably belong on the dev-crypto-csharp mailing list.


mchmiel wrote:
I have tried to reimplement my Encryptor class from java to C# but using Java UTF-8 encoding i cannot get the C# to decode the encoded on java side message.

this is the java code
/**
* @author Mariusz Chmielewski
* @version
*/
public class Encryptor {
private BufferedBlockCipher cipher;


   private KeyParameter key;

   public static final byte TYPE_DES = 33;

   public static final byte TYPE_AES = 20;

   public static final byte TYPE_TWOFISH = 45;

   public static final byte TYPE_BLOWFISH = 99;

   public static final byte TYPE_SERPENT = 78;

   // Initialize the cryptographic engine.
   // The key array should be at least 8 bytes long.

   public Encryptor (byte[] key, byte cipherType) {

       BlockCipher theCipher = null;
       switch (cipherType) {
       case TYPE_DES:
           theCipher = new DESEngine();
           break;
       case TYPE_AES:
           theCipher = new RijndaelEngine();
           break;
       case TYPE_TWOFISH:
           theCipher = new TwofishEngine();
           break;
       case TYPE_BLOWFISH:
           theCipher = new BlowfishEngine();
           break;
       case TYPE_SERPENT:
           theCipher = new SerpentEngine();
           break;
       }
       ;

       cipher = new PaddedBlockCipher(new CBCBlockCipher(theCipher));
       // cipher = new CTSBlockCipher(new CBCBlockCipher (theCipher)) ;

       this.key = new KeyParameter(key);
   }

   // Initialize the cryptographic engine.
   // The string should be at least 8 chars long.

   public Encryptor (String key, byte cipherType) {
       this(key.getBytes(), cipherType);
   }

   // Private routine that does the gritty work.

   private byte[] callCipher(byte[] data) throws CryptoException {
       int size = cipher.getOutputSize(data.length);
       byte[] result = new byte[size];
       int olen = cipher.processBytes(data, 0, data.length, result, 0);
       olen += cipher.doFinal(result, olen);

       if (olen < size) {
           byte[] tmp = new byte[olen];
           System.arraycopy(result, 0, tmp, 0, olen);
           result = tmp;
       }

       return result;
   }

   // Encrypt arbitrary byte array, returning the
   // encrypted data in a different byte array.

public synchronized byte[] encrypt(byte[] data) throws CryptoException {
if (data == null || data.length == 0) {
return new byte[0];
}


       cipher.init(true, key);
       return callCipher(data);
   }

public synchronized String encrypt(String data) throws CryptoException {
if (data == null || data.length() == 0) {
return null;
}


String response = null;
try {
response = new String(Hex.encode(encrypt(data.getBytes("UTF-8"))));
} catch (Exception e) {
response = new String(Hex.encode(encrypt(data.getBytes())));
}


       return response;
   }

public synchronized byte[] decrypt(byte[] data) throws CryptoException {
if (data == null || data.length == 0) {
return new byte[0];
}


cipher.init(false, key);
return callCipher(data);
}
public synchronized String decrypt(String data) throws CryptoException {
if (data == null || data.length() == 0) {
return null;
}
String result = null;
try{
result = new String (decrypt (Hex.decode (data)),"UTF-8");
}catch(Exception e){
result = new String (decrypt (Hex.decode (data)));
e.printStackTrace ();
}
return result;
}


}

and C#

class Encryptor
   {
       private BufferedBlockCipher cipher;
         private KeyParameter key;

public const byte TYPE_DES = 1;
public const byte TYPE_AES = 2;
public const byte TYPE_TWOFISH = 3;
public const byte TYPE_BLOWFISH = 4;
public const byte TYPE_SERPENT = 5;
public Encryptor (string key, byte cipherType) {
BlockCipher theCipher = null;
switch (cipherType)
{
case TYPE_DES:
theCipher = new DESEngine();
break;
case TYPE_AES:
theCipher = new RijndaelEngine();
break;
case TYPE_TWOFISH:
theCipher = new TwofishEngine();
break;
case TYPE_BLOWFISH:
theCipher = new BlowfishEngine();
break;
case TYPE_SERPENT:
theCipher = new SerpentEngine();
break;
};
//cipher = new PaddedBlockCipher(new CBCBlockCipher(theCipher));
cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(theCipher));



//this.key = new KeyParameter(System.Convert.FromBase64String(key));
this.key = new KeyParameter(Hex.decode(key)) ;
//key,Base64FormattingOptions.None));
}


   // Private routine that does the gritty work.

   private byte[] callCipher(byte[] data) {
             int size = cipher.getOutputSize(data.Length);
       byte[] result = new byte[size];
       int olen = cipher.processBytes(data, 0, data.Length, result, 0);
       olen += cipher.doFinal(result, olen);

       if (olen < size) {
           byte[] tmp = new byte[olen];
           Array.Copy(data, result, data.Length);
           result = tmp;
       }

       return result;
   }

   // Encrypt arbitrary byte array, returning the
   // encrypted data in a different byte array.

   public  byte[] encrypt(byte[] data) {
       if (data == null || data.Length == 0) {
           return new byte[0];
       }

       cipher.init(true, key);
       return callCipher(data);
   }

   public  string encrypt(string data) {
       if (data == null || data.Length == 0) {
           return null;
       }

       string response = null;
       try {
           byte[] dataTable_ = System.Convert.FromBase64String(data);

           //byte[] dataTable =

           //byte[] dataTable = Hex.encode(data);
           byte[] encData = encrypt(dataTable_);

           response = System.Convert.ToBase64String(encData);
           System.Console.WriteLine("IN:" + response);
           response = System.Convert.ToBase64String(Hex.encode(encData));
           System.Console.WriteLine("IN:"+response);
          } catch (Exception e) {
           System.Console.WriteLine(e.ToString());
       }

       return response;
   }

   public byte[] decrypt(byte[] data) {
       if (data == null || data.Length == 0) {
           return new byte[0];
       }
       cipher.init(false, key);
       return callCipher(data);
   }
   public string decrypt(string data) {
       if (data == null || data.Length == 0) {
           return null;
       }
       string result = null;
               try{


byte[] temp = Hex.decode (data); //string ar = System.Convert.ToBase64String(temp); //byte[] a = System.Convert.FromBase64String(data); //System.Console.WriteLine(ar); byte[] decrypted = decrypt(temp); result = System.Convert.ToBase64String(decrypted); }catch(Exception e){ System.Console.WriteLine(e.ToString()); } return result; }

}

anybody solve it??





using System;
using System.Text;

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.Encoders;

/**
* @author Mariusz Chmielewski
* @version
*/
public class Encryptor {
 
   private BufferedBlockCipher cipher;

   private KeyParameter key;

   public const byte TYPE_DES = 33;

   public const byte TYPE_AES = 20;

   public const byte TYPE_TWOFISH = 45;

   public const byte TYPE_BLOWFISH = 99;

   public const byte TYPE_SERPENT = 78;

   // Initialize the cryptographic engine.
   // The key array should be at least 8 bytes long.

   public Encryptor (byte[] key, byte cipherType) {

       IBlockCipher theCipher = null;
       switch (cipherType) {
       case TYPE_DES:
           theCipher = new DesEngine();
           break;
       case TYPE_AES:
           theCipher = new RijndaelEngine();
           break;
       case TYPE_TWOFISH:
           theCipher = new TwofishEngine();
           break;
       case TYPE_BLOWFISH:
           theCipher = new BlowfishEngine();
           break;
       case TYPE_SERPENT:
           theCipher = new SerpentEngine();
           break;
       }
       ;

       cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(theCipher));
       // cipher = new CTSBlockCipher(new CBCBlockCipher (theCipher)) ;

       this.key = new KeyParameter(key);
   }

   // Initialize the cryptographic engine.
   // The string should be at least 8 chars long.

   public Encryptor (String key, byte cipherType)
	   : this(Encoding.Default.GetBytes(key), cipherType)
   {
   }

   // Private routine that does the gritty work.

   private byte[] callCipher(byte[] data) {
       int size = cipher.GetOutputSize(data.Length);
       byte[] result = new byte[size];
       int olen = cipher.ProcessBytes(data, 0, data.Length, result, 0);
       olen += cipher.DoFinal(result, olen);

       if (olen < size) {
           byte[] tmp = new byte[olen];
           Array.Copy(result, 0, tmp, 0, olen);
           result = tmp;
       }

       return result;
   }

   // Encrypt arbitrary byte array, returning the
   // encrypted data in a different byte array.

   public byte[] encrypt(byte[] data) {
       if (data == null || data.Length == 0) {
           return new byte[0];
       }

       cipher.Init(true, key);
       return callCipher(data);
   }

   public String encrypt(String data) {
       if (data == null || data.Length == 0) {
           return null;
       }

       String response = null;
       try {
           response = Encoding.UTF8.GetString(Hex.Encode(encrypt(Encoding.UTF8.GetBytes(data))));
       } catch (Exception e) {
           response = Encoding.Default.GetString(Hex.Encode(encrypt(Encoding.Default.GetBytes(data))));
       }

       return response;
   }

   public byte[] decrypt(byte[] data) {
       if (data == null || data.Length == 0) {
           return new byte[0];
       }

       cipher.Init(false, key);
       return callCipher(data);
   }
   public String decrypt(String data) {
       if (data == null || data.Length == 0) {
           return null;
       }
       String result = null;
		try{
			result = Encoding.UTF8.GetString(decrypt(Hex.Decode(data)));
		}catch(Exception e){
			result = Encoding.Default.GetString(decrypt(Hex.Decode(data)));
			Console.WriteLine(e.Message);
			Console.WriteLine(e.StackTrace);
		}
       return result;
   }

}