.cr在iOS中为decrpyt .Net加密的字符串 [英] decrpyt .Net Encrypted string in iOS

查看:86
本文介绍了.cr在iOS中为decrpyt .Net加密的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中进行了一些AES加密,并且像一个吊饰一样工作.代码在这里:

I made some AES encryption in c# and works like a charm. Code here:

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字符串)

NOTE: I am encryting a fair amout of data here(large json string)

现在,下一步将是使iOS发挥同样的魔力,这是一个问题,我在谈到IOS时是个新手,我希望有人能指出正确的方向.

Now the next step would be to make the same magic with iOS here is the problem that I am a newb when it comes to IOS and I am hoping someone can point me in the right direction.

问题:

  1. 到目前为止,我未能在iOS上找到如何使用秘密密钥和Salt密钥制作Rfc289密钥的示例
  2. 我尝试了此示例这里的技巧是c#代码不适用于大字符串,而仅适用于短字符串.
  1. I so far failed to find an example on iOS how to make Rfc289 key with secret and salt key
  2. I tryed this example the trick here is that the c# code does not work with large strings it only works with short strings.

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

Basicly I would like some suggestions or mybe some other aproach to make secure communication between rest api Web Service Application and iOS.

感谢您的帮助.

推荐答案

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

Add an SSL certificate to your WebAPI end point - then use fully secure communications endpoint to endpoint, you should not code anything except change http:// to https://

这篇关于.cr在iOS中为decrpyt .Net加密的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆