Bibliothèque de classes portable (PCL) Consortingb – Cryptography

Je souhaite utiliser la cryptographie dans le projet Consortingb de la bibliothèque de classes portable sur codeplex, mais je n’ai trouvé aucune documentation sur la manière de l’utiliser.

Je souhaite créer une classe wrapper contenant des méthodes Encrypt et Decrypt et je souhaite que cette classe wrapper existe dans une bibliothèque de classes portable. J’ai référencé Portable.Runtime et Portable.Security.Cryptography dans ce projet. Est-ce correct?

Je souhaite ensuite utiliser mon wrapper dans un projet .NET, Windows Phone et Metro. Dans ces projets, je fais référence à mon projet d’emballage, Portable.Runtime , Portable.Security.Cryptography et au projet Portable correspondant, à savoir Portable.Desktop , Portable.Phone ou Portable.WindowsStore . Est-ce correct?

Je reçois cependant des erreurs d’espace de noms en conflit lorsque j’essaie d’utiliser ma classe wrapper. C’est l’erreur et ma classe wrapper:

Le type System.Security.Cryptography.AesManaged existe à la fois dans C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Core.dll et C:\Downloads\PclConsortingb\bin\Debug\Portable.Security.Cryptography.dll

 public sealed class SymmesortingcCryptography where T : SymmesortingcAlgorithm, 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 SymmesortingcCryptography() { this.key = this.provider.Key; this.iv = this.provider.IV; } public SymmesortingcCryptography(byte[] key, byte[] iv) { this.key = key; this.iv = iv; } public SymmesortingcCryptography(ssortingng password, ssortingng salt) { Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes(password, this.utf8.GetBytes(salt)); this.key = deriveBytes.GetBytes(this.provider.KeySize >> 3); this.iv = deriveBytes.GetBytes(16); } public SymmesortingcCryptography(ssortingng password, ssortingng 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 ssortingng Encrypt(ssortingng text) { return this.Encrypt(text, this.key, this.iv); } public ssortingng Encrypt(ssortingng text, byte[] key, byte[] iv) { byte[] output = this.Transform( this.utf8.GetBytes(text), this.provider.CreateEncryptor(key, iv)); return Convert.ToBase64Ssortingng(output); } public ssortingng Decrypt(ssortingng text) { return this.Decrypt(text, this.key, this.iv); } public ssortingng Decrypt(ssortingng text, byte[] key, byte[] iv) { byte[] output = this.Transform( Convert.FromBase64Ssortingng(text), this.provider.CreateDecryptor(key, iv)); return this.utf8.GetSsortingng(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); } } 

La documentation manque un peu, mais je l’appelle dans la FAQ :

Puis-je partager les types de PclConsortingb avec mes projets spécifiques à la plate-forme? Non, pas actuellement. Bien que les types dans PclConsortingb ressemblent à leurs homologues spécifiques à la plate-forme, le runtime et le compilateur les verront comme des types complètement différents. Bien que nous ayons quelques idées sur la façon de faire ce travail, c’est une fonctionnalité que nous ne regarderons pas à court terme.

Le code .net suivant fonctionne avec l’implémentation de Desktop. Ajoutez d’abord des références à Portable.Desktop et Portable.Security.Cryptography.ProtectedData

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

Il se trouve que mon wrapper générique pour les algorithmes de cryptographie posait problème. PCL Consortingb contient une classe appelée SymmesortingcAlgorithm qui est elle-même un wrapper pour le véritable SymmesortingcAlgorithm. Si je rends ma classe wrapper non générique, cela fonctionne comme ceci:

 public sealed class AesManagedSymmesortingcCryptography : SymmesortingcCryptography { #region Constructors public AesManagedSymmesortingcCryptography() { } public AesManagedSymmesortingcCryptography(byte[] key, byte[] iv) : base(key, iv) { } public AesManagedSymmesortingcCryptography(ssortingng password, ssortingng salt) : base(password, salt) { } public AesManagedSymmesortingcCryptography(ssortingng password, ssortingng salt, int iterations) : base(password, salt, iterations) { } #endregion }