我已经在这几天了.我最初(也是最终)的目标是在iOS上使用CommonCrypto来加密具有给定IV和密钥的密码,然后使用.NET成功解密它.经过大量的研究和失败,我已经缩小了我的目标,即在iOS和.NET上简单地生成相同的加密字节,然后从那里开始.
我在.NET(C#,framework 4.5)和iOS(8.1)中创建了简单的测试项目.请注意,以下代码并非旨在保证安全,而是在较大的流程中将变量放大.此外,iOS是变量.最终的.NET加密代码将由客户端部署,因此我可以将iOS加密排除在外.除非确认不可能,否则.NET代码不会更改.
相关的.NET加密代码:
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Padding = PaddingMode.PKCS7;
aesAlg.KeySize = 256;
aesAlg.BlockSize = 128;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(Key, IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) …Run Code Online (Sandbox Code Playgroud)