Hello, i was trying to encrypt file(byte array) in UWP application with help of Rebex.Legacy.Cryptography class and decrypt byte array at the server side(web api) with help of System.Security.Cryptography. But every time when i try to decrypt array i receive an error "The input is not a complete block".
my code
AesCryptoServiceProvider aes;
// Key with 256 and IV with 16 length
private string AES_Key = "password";
singletone
private RabexEnc()
{
aes = new AesCryptoServiceProvider();
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
// generate symmetric key and initialization vector
aes.GenerateIV();
byte[] IV = aes.IV;
//The Salt will be the first 8 bytes of the IV.
byte[] theSalt = new byte[8];
Array.Copy(IV, theSalt, 8);
//A key for AES is generated by expanding the password using the following method.
Rfc2898DeriveBytes keyGen = new Rfc2898DeriveBytes(AES_Key, theSalt);
byte[] aesKey = keyGen.GetBytes(16);
aes.Key = aesKey;
}
public byte[] Encrypt_Aes(byte[] streamBytes)
{
// create an instance of memory-based stream to hold encrypted data
var ms = new MemoryStream();
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
// create an encryptor, create a CryptoStream and wrap a StreamWriter around it
using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(streamBytes, 0, streamBytes.Length);
}
return ms.ToArray();
}
public byte[] Decrypt_Aes(byte[] streamBytes)
{
// create an instance of memory-based stream to hold encrypted data
var ms = new MemoryStream();
using (MemoryStream msDecrypt = new MemoryStream(streamBytes))
{
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (MemoryStream newstream = new MemoryStream())
{
csDecrypt.CopyTo(newstream);
return newstream.ToArray();
}
}
}
}
Same code working at the server, but namespace at the server set to System.Security.Cryptography instead of Rebex.Legacy.Cryptography. Thanks for a reply.