gpt4 book ai didi

c# - 可移植类库 (PCL) 贡献 - 密码学

转载 作者:太空狗 更新时间:2023-10-29 23:09:15 28 4
gpt4 key购买 nike

我想在 Portable Class Library Contrib project on codeplex 中使用密码学但没有找到任何关于如何使用它的文档。

我想创建一个包含 EncryptDecrypt 方法的包装类,我希望这个包装类存在于可移植类库中。我在这个项目中引用了 Portable.RuntimePortable.Security.Cryptography。这是正确的吗?

然后我想在 .NET、Windows Phone 和 Metro 项目中使用我的包装器。在这些项目中,我引用了我的包装器项目 Portable.RuntimePortable.Security.Cryptography 和相应的 Portable 项目,即 Portable.DesktopPortable.PhonePortable.WindowsStore。这是正确的吗?

但是,当我尝试使用我的包装器类时,出现命名空间冲突错误。这是错误和我的包装器类:

The type System.Security.Cryptography.AesManaged exists in both C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Core.dll and 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);
}
}

最佳答案

文档有点欠缺,但我在 FAQ 中指出了这一点:

Can I share the types from PclContrib with my platform-specific projects? No, not currently. While the types in PclContrib look and feel like their platform-specific counterparts, the runtime and compiler will see them as completely different types. While we have some ideas on how to make this work, this is a feature that we won't be looking at for the short term.

关于c# - 可移植类库 (PCL) 贡献 - 密码学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12688296/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com