我已要求加密一些數據,他送我以下細節有關加密算法:AES256 JAVA加密不匹配,C#加密
- 算法:AES256
- 密鑰大小:256位
- 加密模式:CBC(16個比特的塊,其中0 PKCS5Padding)
- 輸出類型:BASE-64
- 密碼:0xA8703827AE586460105696504327B7BB0806FEAE96BD664F89E36868FBB48E3D
- IV:是一個字節[16]與0值
我用下面的代碼,但是我沒有得到與他匹配的結果:
public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
byte[] saltBytes = new byte[16] { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged aes = new RijndaelManaged())
{
aes.KeySize = 256;
aes.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
aes.Key = key.GetBytes(aes.KeySize/8);
aes.IV = key.GetBytes(aes.BlockSize/8);
aes.Padding = PaddingMode.PKCS7;
aes.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
public string EncryptText(string input, string password)
{
byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
string result = Convert.ToBase64String(bytesEncrypted);
return result;
}
所以,當我們試圖加密你好, 你好嗎?,我得到了不同的結果,我應該得到和他一樣的結果,因爲他會解密我發送的數據,並且會處理它。給出的例子應該有這樣的結果:TJTojNoVgoqnhCj4uTv1jLBiZU7r+s/0Bm234bHU+S0=
您需要提供java代碼才能找出他們不同意的原因。 –
客戶端提到[this](https://dzone.com/articles/aes-256-encryption-java-and),如果你找不到這個就夠了,請讓我知道問他更多的信息 –
正如你可以清楚地看到你自己這是你的代碼和代碼之間的巨大差異,特別是在密鑰和IV的推導中。 –