我相信,当 EnterpriseLibrary 尝试解密 RijndaelManaged 加密字符串时,它希望将初始化 vector 添加到加密文本之前。目前使用下面的代码。我可以毫无异常(exception)地解密消息,但我收到奇怪的字符,例如:
�猀漀椀搀㴀眀最爀甀戀攀☀甀琀挀㴀㈀ ⴀ ⴀ㈀吀㌀㨀㔀㈀㨀㌀
我需要做什么才能使这项工作成功?任何帮助是极大的赞赏。这是我的一些代码...
我有一个使用 EnterpriseLibrary 4.1 解密数据的 C# 应用程序(加密: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;
}
}
最佳答案
我找到了答案。上面代码中的问题:
StringUtils.getBytesUtf16(auth);
相反,企业库使用 Little Endian 字节顺序。我使用的功能没有。相反,我应该使用:
StringUtils.getBytesUtf16Le(auth);
这解决了我的问题。感谢任何在此掠夺的人。我很感激!
关于c# - Rijndael 使用 Java 加密,然后使用 C# 和 EnterpriseLibrary 4.1 解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4004836/