2012-10-02 16 views
5

मैं Portable Class Library Contrib project on codeplex में क्रिप्टोग्राफी का उपयोग करना चाहता हूं लेकिन मुझे इसका उपयोग करने के तरीके पर कोई दस्तावेज नहीं मिला है।पोर्टेबल क्लास लाइब्रेरी (पीसीएल) Contrib - क्रिप्टोग्राफी

मैं Encrypt और Decrypt विधियों के साथ एक रैपर वर्ग बनाना चाहता हूं और मैं चाहता हूं कि यह रैपर वर्ग पोर्टेबल क्लास लाइब्रेरी में मौजूद हो। मैंने इस परियोजना में Portable.Runtime और Portable.Security.Cryptography का संदर्भ दिया है। क्या ये सही है?

मैं फिर अपने रैपर को .NET, Windows Phone और Metro Project के अंदर उपयोग करना चाहता हूं। इन परियोजनाओं में मैं अपने रैपर प्रोजेक्ट, Portable.Runtime, Portable.Security.Cryptography और संबंधित पोर्टेबल प्रोजेक्ट i.e., Portable.Phone या Portable.WindowsStore का संदर्भ देता हूं। क्या ये सही है?

जब मैं अपने रैपर वर्ग का उपयोग करने का प्रयास करता हूं तो मुझे विरोधाभासी नेमस्पेस त्रुटियां मिलती हैं। यह त्रुटि है और मेरी आवरण वर्ग:

प्रकार System.Security.Cryptography.AesManaged दोनों C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Core.dll और C:\Downloads\PclContrib\bin\Debug\Portable.Security.Cryptography.dll

public sealed class SymmetricCryptography<T> where T : SymmetricAlgorithm, new() 
{ 
    private readonly T provider = new T(); 
    private readonly UTF8Encoding utf8 = new UTF8Encoding(); 

    private byte[] key; 
    private byte[] iv; 

    public byte[] Key 
    { 
     get { return this.key; } 
    } 

    public byte[] IV 
    { 
     get { return this.iv; } 
    } 

    public SymmetricCryptography() 
    { 
     this.key = this.provider.Key; 
     this.iv = this.provider.IV; 
    } 

    public SymmetricCryptography(byte[] key, byte[] iv) 
    { 
     this.key = key; 
     this.iv = iv; 
    } 

    public SymmetricCryptography(string password, string salt) 
    { 
     Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(password, this.utf8.GetBytes(salt)); 
     this.key = deriveBytes.GetBytes(this.provider.KeySize >> 3); 
     this.iv = deriveBytes.GetBytes(16); 
    } 

    public SymmetricCryptography(string password, string salt, int iterations) 
    { 
     Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(password, this.utf8.GetBytes(salt), iterations); 
     this.key = deriveBytes.GetBytes(this.provider.KeySize >> 3); 
     this.iv = deriveBytes.GetBytes(16); 
    } 

    public byte[] Encrypt(byte[] input) 
    { 
     return this.Encrypt(input, this.key, this.iv); 
    } 

    public byte[] Encrypt(byte[] input, byte[] key, byte[] iv) 
    { 
     return this.Transform(
      input, 
      this.provider.CreateEncryptor(key, iv)); 
    } 

    public byte[] Decrypt(byte[] input) 
    { 
     return this.Decrypt(input, this.key, this.iv); 
    } 

    public byte[] Decrypt(byte[] input, byte[] key, byte[] iv) 
    { 
     return this.Transform(
      input, 
      this.provider.CreateDecryptor(key, iv)); 
    } 

    public string Encrypt(string text) 
    { 
     return this.Encrypt(text, this.key, this.iv); 
    } 

    public string Encrypt(string text, byte[] key, byte[] iv) 
    { 
     byte[] output = this.Transform(
      this.utf8.GetBytes(text), 
      this.provider.CreateEncryptor(key, iv)); 
     return Convert.ToBase64String(output); 
    } 

    public string Decrypt(string text) 
    { 
     return this.Decrypt(text, this.key, this.iv); 
    } 

    public string Decrypt(string text, byte[] key, byte[] iv) 
    { 
     byte[] output = this.Transform(
      Convert.FromBase64String(text), 
      this.provider.CreateDecryptor(key, iv)); 
     return this.utf8.GetString(output, 0, output.Length); 
    } 

    public void Encrypt(Stream input, Stream output) 
    { 
     this.Encrypt(input, output, this.key, this.iv); 
    } 

    public void Encrypt(Stream input, Stream output, byte[] key, byte[] iv) 
    { 
     this.TransformStream(true, ref input, ref output, key, iv); 
    } 

    public void Decrypt(Stream input, Stream output) 
    { 
     this.Decrypt(input, output, this.key, this.iv); 
    } 

    public void Decrypt(Stream input, Stream output, byte[] key, byte[] iv) 
    { 
     this.TransformStream(false, ref input, ref output, key, iv); 
    } 

    private byte[] Transform(
     byte[] input, 
     ICryptoTransform cryptoTransform) 
    { 
     byte[] result; 

     using (MemoryStream memoryStream = new MemoryStream()) 
     { 
      using (CryptoStream cryptStream = new CryptoStream(
       memoryStream, 
       cryptoTransform, 
       CryptoStreamMode.Write)) 
      { 
       cryptStream.Write(input, 0, input.Length); 
       cryptStream.FlushFinalBlock(); 
       memoryStream.Position = 0; 
       result = memoryStream.ToArray(); 
      } 
     } 

     return result; 
    } 

    private void TransformStream(bool encrypt, ref Stream input, ref Stream output, byte[] key, byte[] iv) 
    { 
     // defensive argument checking 
     if (input == null) 
     { 
      throw new ArgumentNullException("input"); 
     } 

     if (output == null) 
     { 
      throw new ArgumentNullException("output"); 
     } 

     if (!input.CanRead) 
     { 
      throw new ArgumentException("Unable to read from the input Stream.", "input"); 
     } 

     if (!output.CanWrite) 
     { 
      throw new ArgumentException("Unable to write to the output Stream.", "output"); 
     } 

     // make the buffer just large enough for 
     // the portion of the stream to be processed 
     byte[] inputBuffer = new byte[input.Length - input.Position]; 
     // read the stream into the buffer 
     input.Read(inputBuffer, 0, inputBuffer.Length); 
     // transform the buffer 
     byte[] outputBuffer = encrypt ? Encrypt(inputBuffer, key, iv) 
             : Decrypt(inputBuffer, key, iv); 
     // write the transformed buffer to our output stream 
     output.Write(outputBuffer, 0, outputBuffer.Length); 
    } 
} 
+1

कि आप अपनी समस्या को हल करते हैं, तो एक जवाब के रूप समाधान पोस्ट, बजाय प्रश्न में संपादन की जोड़ें। या आपके पास कोई शेष प्रश्न है? यदि ऐसा है, तो आपको उन्हें स्पष्ट रूप से अवश्य कहना चाहिए। – CodesInChaos

+1

बीटीडब्ल्यू आपके चतुर्थ उपयोग खराब है। आपके द्वारा किए जाने वाले प्रत्येक एन्क्रिप्शन के लिए चतुर्थ अलग होना चाहिए। – CodesInChaos

उत्तर

1

ऐसा लगता है कि क्रिप्टोग्राफी एल्गोरिदम के लिए मेरी सामान्य आवरण एक समस्या पैदा कर रहा था में मौजूद है। पीसीएल कंट्रिब में सममित एल्गोरिदम नामक एक कक्षा होती है जो वास्तविक सिमेट्रिक एल्गोरिदम के लिए स्वयं एक रैपर है। अगर मैं अपने आवरण वर्ग गैर सामान्य बनाने इसे इस तरह काम करता है:

public sealed class AesManagedSymmetricCryptography : SymmetricCryptography<AesManaged> 
{ 
    #region Constructors 

    public AesManagedSymmetricCryptography() 
    { 
    } 

    public AesManagedSymmetricCryptography(byte[] key, byte[] iv) 
     : base(key, iv) 
    { 
    } 

    public AesManagedSymmetricCryptography(string password, string salt) 
     : base(password, salt) 
    { 
    } 

    public AesManagedSymmetricCryptography(string password, string salt, int iterations) 
     : base(password, salt, iterations) 
    { 
    } 

    #endregion 
} 
+0

क्या आप इस फ़ंक्शंस का उपयोग करके कुछ पंक्तियों का उदाहरण भी शामिल कर सकते हैं? –

+0

मैं विंडोज़ फोन 8 और विंडोज स्टोर ऐप दोनों की सेवा के लिए पीएलसी-कॉन्ट्रिब क्रिप्टोग्राफी का उपयोग करने की भी कोशिश कर रहा हूं। तो मैं सवाल और निकाली गई "सील" से अपने कोड को कॉपी किया था से यह 'सार्वजनिक वर्ग SymmetricCryptography जहां टी: SymmetricAlgorithm, नई() ....' तो मैं इसके बारे में एक गैर सामान्य संस्करण बना सकते हैं, जैसा आपने सुझाव दिया था। क्या आप इसका उपयोग कर कहीं समाधान पोस्ट कर सकते हैं? –

+0

आपने 'this.provider.KeySize' का उपयोग किया है लेकिन प्रदाता सममित एल्गोरिदम है, और इसमें KeySize नहीं है? –

3

डॉक्स एक छोटे से कमी कर रहे हैं, लेकिन मैं FAQ में इस कॉल:

मैं से प्रकार साझा कर सकते हैं मेरे मंच-विशिष्ट परियोजनाओं के साथ PclContrib? नहीं, वर्तमान में नहीं। जबकि PclContrib में प्रकार और उनके प्लेटफ़ॉर्म-विशिष्ट समकक्षों की तरह महसूस करते हैं, रनटाइम और कंपाइलर उन्हें पूरी तरह से अलग प्रकार के रूप में देखेगा। जबकि हमारे पास इस काम को बनाने के तरीके के बारे में कुछ विचार हैं, यह एक विशेषता है कि हम शॉर्ट टर्म की तलाश नहीं करेंगे।

2

निम्नलिखित .NET कोड डेस्कटॉप कार्यान्वयन पर काम करता है। पहले संदर्भ Portable.Desktop और 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); ; 
    } 
संबंधित मुद्दे