.NET / Java Encryption Interoperability

Question:

J’ai une petite question concernant Chilkat Crypt .NET. En début d’année nous avons acheté ce composant et pour l’instant nous en sommes très satisfait en ce qui concerne l’utilisation sous .NET.

Pour des raisons diverses nous devons maintenant développer certaines de nos applications en Java. J’aimerai en Java, en utilisant la classe javax.crypto, pouvoir décrypter les différentes choses cryptées sous .NET avec votre composant. Pour cela j’ai effectué un petit test. J’ai crypté la même chaînes une fois avec votre composant et l’autre fois avec la classe javax.crypto. Je n’arrive jamais au même résultat et cela m’ennuie beaucoup.

Avez-vous des informations à me donner ou éventuellement une classe Java qui permet de décrypter ce qui a été cryptée en .NET ???

Voilà le code utiliser pour crypter sous .NET :

public string CryptData(string data)
{
            Chilkat.Crypt crypt = new Chilkat.Crypt() ;

            crypt.UnlockComponent("zzzzzz") ;

            crypt.SetAlgorithmBlowfish() ;

            crypt.KeyLength = 128 ;

            crypt.SetSecretKeyViaPassPhrase("0123456789abcdef") ;

            string encryptedDataString = "" ;

            byte [] encryptedDataByte ;

            encryptedDataByte = crypt.Encrypt(Encoding.Unicode.GetBytes(data)) ;

            encryptedDataString = crypt.EncodeBase64(encryptedDataByte) ;

            return encryptedDataString ;
            }
 
Voici ma classe en Java :

 
public class TestBlowFish 
{
            private final String key="0123456789abcdef";

public TestBlowFish() 

{
    byte[] encrypted = null;
    SecretKeySpec skeySpec=null;

    String cryptedB64="";
 
    Try
        {
        System.out.println("key b64="+Base64.encodeBytes(key.getBytes()));
        skeySpec = new SecretKeySpec(key.getBytes(), "Blowfish");
        Cipher cipher = Cipher.getInstance("Blowfish");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

         final String tocrypt =  "Test de Cryptage Blowfish";
        encrypted = cipher.doFinal(tocrypt.getBytes());

         cryptedB64=Base64.encodeBytes(encrypted);

        System.out.println("encrypted b64:"+cryptedB64);
 
        byte[] decrypted= null;

        cipher.init(Cipher.DECRYPT_MODE, skeySpec);

        if(encrypted!=null)
            {
            decrypted =cipher.doFinal(Base64.decode(cryptedB64));
             }

        System.out.println("decoded with javax.crypto:"+new String(decrypted));

        //decode(encrypted);

        }
catch(Exception e)
{
         e.printStackTrace();
}

}

Answer:

Try using this code instead.

The problem is that the binary secret keys must match exactly. The SetSecretKeyViaPassPhrase method sets a binary secret key that is a 128-bit hash of the password string, so it is not equal to "0123456789abcdef".


                        string data = "ABCDEFG";

                        Chilkat.Crypt2 crypt2 = new Chilkat.Crypt2();

                        crypt2.UnlockComponent("zzzzzz") ;

                        crypt2.CryptAlgorithm = "blowfish";
                        crypt2.Charset = "iso-8859-1";
                        crypt2.KeyLength = 128 ;
                        
                        // Key is 128 bits (16 bytes)
                        // Set the secret key to "0123456789abcdef"
                        byte [] secretKey = new byte[16]; 
                        byte x = (byte)'0';
                        int i;
                        for (i=0; i<16; i++) secretKey[i] = x++;
                        crypt2.SecretKey = secretKey;

                        string encryptedDataString;

                        crypt2.EncodingMode = "base64";
                        encryptedDataString = crypt2.EncryptStringENC(data);
 
                        if (encryptedDataString == null)
                        {
                                MessageBox.Show(crypt2.LastErrorText);
                        }
                        else
                        {
                                MessageBox.Show(encryptedDataString);
                        }