2010-10-23 7 views
1

Je crois que lorsque EnterpriseLibrary tente de décrypter une chaîne cryptée RijndaelManaged, il s'attend à ce que le vecteur d'initialisation soit ajouté au texte crypté. Actuellement avec le code ci-dessous. Je peux déchiffrer le message avec une exception, mais j'obtiens des caractères bizarres comme:RijndaelEncryption avec Java puis décryptage avec C# et EnterpriseLibrary 4.1

猀 漀 椀 搀 㴀 眀 最 爀 甀 戀 攀 ㄀ ☀ 甀 琀 挀 㴀 ㈀ ㄀ ა ㄀ ა㈀ ㄀ 吀

Que dois-je faire pour que cela fonctionne? Toute aide est grandement appréciée. Voici une partie du code que j'ai ...

J'ai une application C# qui déchiffre des données en utilisant EnterpriseLibrary 4.1 (chiffrement: RijndaelManaged).

string message = "This encrypted message comes from Java Client"; 
Cryptographer.DecryptSymmetric("RijndaelManaged", message); 

Le client crypte le message, implémenté en Java.

public String encrypt(String auth) { 
      try { 
       String cipherKey = "Key as a HEX string"; 
       byte[] rawKey = hexToBytes(cipherKey); 
       SecretKeySpec keySpec = new SecretKeySpec(rawKey, "AES"); 
       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 

       String cipherIV = "xYzF5AqA2cKLbvbfGzsMwg=="; 
       byte[] btCipherIV = Base64.decodeBase64(cipherIV.getBytes()); 

       cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec (btCipherIV)); 
       byte[] unencrypted = StringUtils.getBytesUtf16(auth); 
       byte[] encryptedData = cipher.doFinal(unencrypted); 
       String encryptedText = null; 

       byte[] entlib = new byte[btCipherIV2.length + encryptedData.length]; 
       System.arraycopy(btCipherIV, 0, entlib, 0, btCipherIV.length); 
       System.arraycopy(encryptedData, 0, entlib, btCipherIV.length, encryptedData.length); 

       encryptedText = new String(encryptedData); 
       encryptedText = Base64.encodeBase64String(encryptedData);    
       return encryptedText; 

      } catch (Exception e) { 
      } 

      return ""; 
     } 

    public static byte[] hexToBytes(String str) { 
      if (str==null) { 
      return null; 
      } else if (str.length() < 2) { 
      return null; 
      } else { 
      int len = str.length()/2; 
      byte[] buffer = new byte[len]; 
      for (int i=0; i<len; i++) { 
       buffer[i] = (byte) Integer.parseInt(
        str.substring(i*2,i*2+2),16); 
      } 
      return buffer; 
      } 

     } 

Répondre

1

J'ai trouvé la réponse. Le problème dans le code ci-dessus:

StringUtils.getBytesUtf16(auth); 

La bibliothèque d'entreprise utilise à la place l'ordre des octets de Little Endian. La fonction que j'utilisais ne fonctionne pas. Au lieu de cela j'aurais dû utiliser:

StringUtils.getBytesUtf16Le(auth); 

Cela a résolu mon problème. Merci à tous ceux qui ont pris un butin. Je vous en suis reconnaissant!

Questions connexes