1.我有加密xml文件并返回加密字符串的java函数。
/// Java Class
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class Crypt {
public static String key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
public static byte[] key_Array = Base64.decodeBase64(key);
public static String encrypt(String strToEncrypt)
{
try
{
//Cipher _Cipher = Cipher.getInstance("AES");
//Cipher _Cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
//Cipher _Cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
Key SecretKey = new SecretKeySpec(key_Array, "AES");
Cipher _Cipher = Cipher.getInstance("AES");
_Cipher.init(Cipher.ENCRYPT_MODE, SecretKey);
return Base64.encodeBase64String(_Cipher.doFinal(strToEncrypt.getBytes()));
}
catch (Exception e)
{
System.out.println("[Exception]:"+e.getMessage());
}
return null;
}
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("xml file string ...");
String EncryptedString = encrypt(sb.toString());
System.out.println("[EncryptedString]:"+EncryptedString);
}
}
2.我有c#函数来解密由java函数加密的消息。
/// C# Function
private static string Decrypt(string encryptedText)
{
RijndaelManaged aesEncryption = new RijndaelManaged();
aesEncryption.BlockSize = 256;
//aesEncryption.KeySize = 256;
//aesEncryption.Mode = CipherMode.CBC;
//aesEncryption.Padding = PaddingMode.PKCS7;
string keyStr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
//string ivStr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
byte[] keyArr = Convert.FromBase64String(keyStr);
//byte[] ivArr = Convert.FromBase64String(ivStr);
aesEncryption.Key = keyArr;
//aesEncryption.IV = ivArr;
ICryptoTransform decrypto = aesEncryption.CreateDecryptor();
byte[] encryptedBytes = Convert.FromBase64CharArray(encryptedText.ToCharArray(), 0, encryptedText.Length);
byte[] decryptedData = decrypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); /// CryptographicException: Length of the data to decrypt is invalid.
return ASCIIEncoding.UTF8.GetString(decryptedData);
}
Java 加密功能运行良好。但问题是 C# 函数,
当我解密时,我收到以下错误消息
CryptographicException: Length of the data to decrypt is invalid.
我使用以下参考搜索了解决方案
但我仍然面临同样的错误。请问有人能给我建议吗?
更新
我只是更改了我的 C# 加密函数。以下是我的更改列表
- 块大小为 128
- 密钥大小为 256
- IV 尺寸至 16
- 密钥大小为 32
/// Updated decrypt function
private static string Decrypt(string encryptedText)
{
RijndaelManaged aesEncryption = new RijndaelManaged();
aesEncryption.BlockSize = 128;
aesEncryption.KeySize = 256;
//aesEncryption.Mode = CipherMode.CBC;
aesEncryption.Padding = PaddingMode.None;
string keyStr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
string ivStr = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
byte[] ivArr = Convert.FromBase64String(ivStr);
byte[] IVkey16BytesValue = new byte[16];
Array.Copy(ivArr, IVkey16BytesValue, 16);
byte[] keyArr = Convert.FromBase64String(keyStr);
byte[] KeyArr32BytesValue = new byte[32];
Array.Copy(keyArr, KeyArr32BytesValue, 32);
aesEncryption.IV = IVkey16BytesValue;
aesEncryption.Key = KeyArr32BytesValue;
ICryptoTransform decrypto = aesEncryption.CreateDecryptor();
byte[] encryptedBytes = Convert.FromBase64CharArray(encryptedText.ToCharArray(), 0, encryptedText.Length);
byte[] decryptedData = decrypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
return ASCIIEncoding.UTF8.GetString(decryptedData);
}
在此期间,没有错误发生。但是我收到了我无法阅读的解密消息。
g:�\0�\td��Y\\符O����\rL��W�wHm�>f�\au����%��0��\ ..........
请让我再次得到你的建议。
原文由 Frank Myat Thu 发布,翻译遵循 CC BY-SA 4.0 许可协议
我相信 blockSize 应该是 128,keysize 应该是 256。keyStr 应该是 32 个字符长,IVstr 应该是 16 个字符长。这可能会有所帮助,因为它描述了为什么必须将 128 位用于块大小以及密钥大小可以是多少。 csrc.nist.gov/publications/fips/fips197/fips-197.pdf
你在解密方法中有这个。
我相信你也需要把它放在加密方法中。
另外为什么不对密钥和 IV 使用此方法。