【问题标题】:decrpyt .Net Encrypted string in iOS在 iOS 中解密 .Net 加密字符串
【发布时间】:2014-06-04 21:58:12
【问题描述】:

我在c# 中进行了一些 AES 加密,效果非常好。代码在这里:

public string EncryptStringAES(string plainText, string sharedSecret)
{
    if (string.IsNullOrEmpty(plainText))
        throw new ArgumentNullException("plainText");
    if (string.IsNullOrEmpty(sharedSecret))
        throw new ArgumentNullException("sharedSecret");

    string outStr = null;                       // Encrypted string to return
    RijndaelManaged aesAlg = null;              // RijndaelManaged object used to encrypt the data.

    try
    {
        _pkey = sharedSecret;
        // generate the key from the shared secret and the salt
        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);
        //_key = key.ToString();

        // Create a RijndaelManaged object
        aesAlg = new RijndaelManaged();
        aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
        _key = Encoding.ASCII.GetString(aesAlg.Key);
        // Create a decryptor to perform the stream transform.
        ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

        // Create the streams used for encryption.
        using (MemoryStream msEncrypt = new MemoryStream())
        {
            // prepend the IV
            msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
            msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);
            using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                {
                    //Write all data to the stream.
                    swEncrypt.Write(plainText);
                }
            }
            outStr = Convert.ToBase64String(msEncrypt.ToArray());
        }
    }
    finally
    {
        // Clear the RijndaelManaged object.
        if (aesAlg != null)
            aesAlg.Clear();
    }

    // Return the encrypted bytes from the memory stream.
    return outStr;
}

/// <summary>
/// Decrypt the given string.  Assumes the string was encrypted using 
/// EncryptStringAES(), using an identical sharedSecret.
/// </summary>
/// <param name="cipherText">The text to decrypt.</param>
/// <param name="sharedSecret">A password used to generate a key for decryption.</param>
public string DecryptStringAES(string cipherText, string sharedSecret)
{
    if (string.IsNullOrEmpty(cipherText))
        throw new ArgumentNullException("cipherText");
    if (string.IsNullOrEmpty(sharedSecret))
        throw new ArgumentNullException("sharedSecret");

    // Declare the RijndaelManaged object
    // used to decrypt the data.
    RijndaelManaged aesAlg = null;

    // Declare the string used to hold
    // the decrypted text.
    string plaintext = null;

    try
    {
        _pkey = sharedSecret;
        // generate the key from the shared secret and the salt
        Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);
        //_key = key.ToString();

        // Create the streams used for decryption.                
        byte[] bytes = Convert.FromBase64String(cipherText);
        using (MemoryStream msDecrypt = new MemoryStream(bytes))
        {
            // Create a RijndaelManaged object
            // with the specified key and IV.
            aesAlg = new RijndaelManaged();
            aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
            _key = Encoding.ASCII.GetString(aesAlg.Key);
            // Get the initialization vector from the encrypted stream
            aesAlg.IV = ReadByteArray(msDecrypt);
            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            {
                using (StreamReader srDecrypt = new StreamReader(csDecrypt))

                    // Read the decrypted bytes from the decrypting stream
                    // and place them in a string.
                    plaintext = srDecrypt.ReadToEnd();
            }
        }
    }
    finally
    {
        // Clear the RijndaelManaged object.
        if (aesAlg != null)
            aesAlg.Clear();
    }

    return plaintext;
}

注意: 我在这里加密了大量数据(大 json 字符串)

现在下一步是用 iOS 创造同样的魔力,这里的问题是我是 IOS 的新手,我希望有人能指出我正确的方向。

问题:

  1. 到目前为止,我还没有在 iOS 上找到如何使用密钥和盐密钥制作 Rfc289 密钥的示例
  2. 我试过this example,这里的诀窍是c# 代码不适用于大字符串,它只适用于短字符串。

基本上我想要一些建议或其他一些方法来在rest api Web服务应用程序和iOS之间进行安全通信。

感谢您的帮助。

【问题讨论】:

    标签: c# ios objective-c aes


    【解决方案1】:

    将 SSL 证书添加到您的 WebAPI 端点 - 然后使用完全安全的通信端点到端点,除了将 http:// 更改为 https://

    之外,您不应编写任何代码

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 2018-08-26
      相关资源
      最近更新 更多