6

我正在构建一个使用 c# web 服务的 iPhone 应用程序。我的 c# Web 服务获取用户详细信息并针对我的数据库进行验证并返回 xml 文件。

所以现在的问题是如何在目标 c 中加密用户详细信息(用户名和密码各 10 个字符)并在 C# 中解密。

我对密码学很陌生,哪种方法最好。是否可以在 Objective c 中加密并在 C# 中解密。

谢谢..

4

5 回答 5

7

感谢您的快速回复。我感谢您的帮助。我找到了一个解释我的问题的博客。这是它的链接。

http://dotmac.rationalmind.net/2009/02/aes-interoperability-between-net-and-iphone/

我现在正在实施。我会尽快让你知道状态。

非常感谢..
快乐编码..

于 2009-05-14T18:22:29.297 回答
5

假设您正在加密此信息以通过网络保护它,最好的解决方案是通过 SSL 连接。这将解决问题,而不会在代码中产生新的复杂性。SSL 处理通常在 .NET 和 Cocoa 中都可用。

您尝试加密此数据是否还有其他原因?

于 2009-05-14T18:00:10.487 回答
1

以下内容来自The CSharp Cookbook。它几乎是现有的一个简单的例子。您当然必须将加密部分移植到 Objective C,但只要您使用相同的密钥,它应该会生成相同的结果。

您需要使用在 ObjC 兼容代码中可用的 Rijndael 密码

 public static void EncDecString()
        {
            string encryptedString = CryptoString.Encrypt("MyPassword");
            Console.WriteLine("encryptedString: " + encryptedString);
            // get the key and IV used so you can decrypt it later
            byte [] key = CryptoString.Key;
            byte [] IV = CryptoString.IV;

            CryptoString.Key = key;
            CryptoString.IV = IV;
            string decryptedString = CryptoString.Decrypt(encryptedString);
            Console.WriteLine("decryptedString: " + decryptedString);

        }

        public sealed class CryptoString
        {
            private CryptoString() {}

            private static byte[] savedKey = null;
            private static byte[] savedIV = null;

            public static byte[] Key
            {
                get { return savedKey; }
                set { savedKey = value; }
            }

            public static byte[] IV
            {
                get { return savedIV; }
                set { savedIV = value; }
            }

            private static void RdGenerateSecretKey(RijndaelManaged rdProvider)
            {
                if (savedKey == null)
                {
                    rdProvider.KeySize = 256;
                    rdProvider.GenerateKey();
                    savedKey = rdProvider.Key;
                }
            }

            private static void RdGenerateSecretInitVector(RijndaelManaged rdProvider)
            {
                if (savedIV == null)
                {
                    rdProvider.GenerateIV();
                    savedIV = rdProvider.IV;
                }
            }

            public static string Encrypt(string originalStr)
            {
                // Encode data string to be stored in memory
                byte[] originalStrAsBytes = Encoding.ASCII.GetBytes(originalStr);
                byte[] originalBytes = {};

                // Create MemoryStream to contain output
                MemoryStream memStream = new MemoryStream(originalStrAsBytes.Length);

                RijndaelManaged rijndael = new RijndaelManaged();

                // Generate and save secret key and init vector
                RdGenerateSecretKey(rijndael);
                RdGenerateSecretInitVector(rijndael);

                if (savedKey == null || savedIV == null)
                {
                    throw (new NullReferenceException(
                        "savedKey and savedIV must be non-null."));
                }

                // Create encryptor, and stream objects
                ICryptoTransform rdTransform = 
                    rijndael.CreateEncryptor((byte[])savedKey.Clone(),
                                            (byte[])savedIV.Clone());
                CryptoStream cryptoStream = new CryptoStream(memStream, rdTransform, 
                    CryptoStreamMode.Write);

                // Write encrypted data to the MemoryStream
                cryptoStream.Write(originalStrAsBytes, 0, originalStrAsBytes.Length);
                cryptoStream.FlushFinalBlock();
                originalBytes = memStream.ToArray();

                // Release all resources
                memStream.Close();
                cryptoStream.Close();
                rdTransform.Dispose();
                rijndael.Clear();

                // Convert encrypted string
                string encryptedStr = Convert.ToBase64String(originalBytes);
                return (encryptedStr);
            }

            public static string Decrypt(string encryptedStr)
            {
                // Unconvert encrypted string
                byte[] encryptedStrAsBytes = Convert.FromBase64String(encryptedStr);
                byte[] initialText = new Byte[encryptedStrAsBytes.Length];

                RijndaelManaged rijndael = new RijndaelManaged();
                MemoryStream memStream = new MemoryStream(encryptedStrAsBytes);

                if (savedKey == null || savedIV == null)
                {
                    throw (new NullReferenceException(
                        "savedKey and savedIV must be non-null."));
                }

                // Create decryptor, and stream objects
                ICryptoTransform rdTransform = 
                    rijndael.CreateDecryptor((byte[])savedKey.Clone(), 
                                            (byte[])savedIV.Clone());
                CryptoStream cryptoStream = new CryptoStream(memStream, rdTransform, 
                    CryptoStreamMode.Read);

                // Read in decrypted string as a byte[]
                cryptoStream.Read(initialText, 0, initialText.Length);

                // Release all resources
                memStream.Close();
                cryptoStream.Close();
                rdTransform.Dispose();
                rijndael.Clear();

                // Convert byte[] to string
                string decryptedStr = Encoding.ASCII.GetString(initialText);
                return (decryptedStr);
            }
        }

我不能谈论 Rijndael 密码的 Objective C 实现,但我已经使用这个(C#)代码作为一些生产工作的基础,并且效果很好。

于 2009-05-14T18:00:55.870 回答
0

你的问题很模糊,但简而言之;是的,有可能。你需要弄清楚你的密码学的期望是什么(高安全性还是高速?),然后权衡各种算法的好处以及它们在 Objective-C 和 C# 中的实现困难。

于 2009-05-14T17:59:33.237 回答
0

我不知道 Objective C,但对于 C# 解密,请查看 System.Security.Cryptography 中的各种 CryptoServiceProviders。

这是我使用 TripleDES 编写的一个示例:

public class TripleDES
{
    private byte[] mbKey;
    private byte[] mbIV;
    private TripleDESCryptoServiceProvider tdProvider = new TripleDESCryptoServiceProvider();
    private UTF8Encoding UTEncode = new UTF8Encoding();

    // Key:  **YOUR KEY**
    // Project IV:  **YOUR IV**
    public TripleDES(string strKey, string strIV)
    {
        mbKey = UTEncode.GetBytes(strKey);
        mbIV = UTEncode.GetBytes(strIV);        
    }

    public TripleDES()
    {
        //
        // TODO: Add constructor logic here
        //
    }


    public string EncryptToString(string strInput)
    {           
        return Convert.ToBase64String(this.EncryptToBytes(strInput));           
    }

    public byte[] EncryptToBytes(string strInput)
    {
        byte[] bInput = UTEncode.GetBytes(strInput);
        byte[] bOutput = ProcessInput(bInput, tdProvider.CreateEncryptor(mbKey, mbIV));
        return bOutput;
    }

    public string DecryptToString(string strInput)
    {
        return UTEncode.GetString(DecryptToBytes(strInput));
    }

    public byte[] DecryptToBytes(string strInput)
    {
        byte[] bInput = Convert.FromBase64String(strInput);
        byte[] bOutput = ProcessInput(bInput, tdProvider.CreateDecryptor(mbKey, mbIV));
        return bOutput;
    }

    private byte[] ProcessInput(byte[] input, ICryptoTransform ctProcessor)
    {
        MemoryStream memStream = new MemoryStream();
        CryptoStream crpStream = new CryptoStream(memStream, ctProcessor, CryptoStreamMode.Write);

        crpStream.Write(input, 0, input.Length);
        crpStream.FlushFinalBlock();

        memStream.Position = 0;

        byte[] output;
        output = new byte[memStream.Length];

        memStream.Read(output, 0, output.Length);

        memStream.Close();
        crpStream.Close();

        return output;
    }
}

}

于 2009-05-14T18:02:14.273 回答