代码之家  ›  专栏  ›  技术社区  ›  sgmeyer

用Java进行RijndaelEncryption,然后用C#和EnterpriseLibrary 4.1进行解密

  •  1
  • sgmeyer  · 技术社区  · 16 年前

    ¤¨¨

    我需要做什么才能让这工作?任何帮助都非常感谢。这是我的一些代码。。。

    我有一个C#应用程序,它使用EnterpriseLibrary 4.1(加密:RijndaelManaged)解密数据。

    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;
              }
    
           }
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   sgmeyer    16 年前

    我找到了答案。上述代码中的问题:

    StringUtils.getBytesUtf16(auth);
    

    StringUtils.getBytesUtf16Le(auth);
    

    这解决了我的问题。感谢所有在这里抢劫的人。我很感激!