1

EnterpriseLibrary が RijndaelManaged で暗号化された文字列を復号化しようとするとき、初期化ベクトルが暗号化されたテキストの前に追加されることを期待していると思います。現在、以下のコードで。例外なくメッセージを復号化できますが、次のような奇妙な文字が表示されます。

�猀漀椀搀㴀眀最爀甀戀攀㄀☀甀琀挀㴀㈀㄀ⴀ㄀ⴀ㈀㄀吀㄀㌀㨀㈀㨀㄀㌀

これを機能させるにはどうすればよいですか? どんな助けでも大歓迎です。これが私が持っているコードの一部です...

EnterpriseLibrary 4.1 (暗号化: RijndaelManaged) を使用してデータを復号化する C# アプリケーションがあります。

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

クライアントは、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;
          }

       }
4

1 に答える 1

1

私は答えを見つけました。上記のコードの問題:

StringUtils.getBytesUtf16(auth);

代わりに、Enterprise Library はリトル エンディアンのバイト順を使用しています。私が使用していた機能はそうではありません。代わりに、次を使用する必要がありました。

StringUtils.getBytesUtf16Le(auth);

これで私の問題は解決しました。これを略奪してくれた人に感謝します。それは有り難いです!

于 2010-10-23T16:47:03.733 に答える