Portable Class Library (PCL) Contrib - Cryptography

前端 未结 3 741
忘掉有多难
忘掉有多难 2021-02-10 03:16

I want to use the cryptography in the Portable Class Library Contrib project on codeplex but have not found any documentation on how I can use it.

I want to create a wra

3条回答
  •  长情又很酷
    2021-02-10 04:06

    The following .net code works on Desktop implementation. First add references to Portable.Desktop and Portable.Security.Cryptography.ProtectedData

       private void button2_Click(object sender, EventArgs e)
        {
            String encrypted = PCL.CentralClass.Encrypt("yo");
            String decreypted = PCL.CentralClass.Decrypt(encrypted);
            //PCL.CentralClass.
        }
        //https://pclcontrib.codeplex.com/documentation?FocusElement=Comment
        //\Source\Portable.Security.Cryptography.ProtectedData\Security\Cryptography\ProtectedData.cs
    
        static byte[] GetBytes(string str)
        {
            byte[] bytes = new byte[str.Length * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }
    
        static string GetString(byte[] bytes)
        {
            char[] chars = new char[bytes.Length / sizeof(char)];
            System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
            return new string(chars);
        }
    
        public static String Encrypt(String strEncrypt)
        {
            byte[] userData = GetBytes(strEncrypt);
            byte[] optionalEntropy = null;
            byte[] x = System.Security.Cryptography.ProtectedData.Protect(userData, optionalEntropy);
            return GetString(x);
        }
        public static String Decrypt(String strDecrypt)
        {
            byte[] encryptedData = GetBytes(strDecrypt);
            byte[] optionalEntropy = null;
            byte[] x = System.Security.Cryptography.ProtectedData.Unprotect(encryptedData, optionalEntropy);
            return GetString(x); ;
        }
    

提交回复
热议问题