C# 可移植类库(PCL)Contrib-加密
我想在中使用加密技术,但没有找到任何关于如何使用它的文档 我想创建一个包装类,其中包含C# 可移植类库(PCL)Contrib-加密,c#,.net,cryptography,portable-class-library,C#,.net,Cryptography,Portable Class Library,我想在中使用加密技术,但没有找到任何关于如何使用它的文档 我想创建一个包装类,其中包含Encrypt和Decrypt方法,并且我希望这个包装类存在于一个可移植类库中。我在这个项目中引用了Portable.Runtime和Portable.Security.Cryptography。这是正确的吗 然后我想在.NET、Windows Phone和Metro项目中使用我的包装器。在这些项目中,我引用了我的包装器项目,Portable.Runtime,Portable.Security.Cryptogr
Encrypt
和Decrypt
方法,并且我希望这个包装类存在于一个可移植类库中。我在这个项目中引用了Portable.Runtime
和Portable.Security.Cryptography
。这是正确的吗
然后我想在.NET、Windows Phone和Metro项目中使用我的包装器。在这些项目中,我引用了我的包装器项目,Portable.Runtime
,Portable.Security.Cryptography
,以及相应的便携项目,即Portable.Desktop
,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
公共密封类对称密码,其中T:SymmetricAlgorithm,new()
{
私有只读T提供程序=新T();
私有只读UTF8Encoding utf8=新的UTF8Encoding();
专用字节[]密钥;
专用字节[]iv;
公共字节[]密钥
{
获取{返回this.key;}
}
公共字节[]IV
{
获取{返回this.iv;}
}
公共对称密码术()
{
this.key=this.provider.key;
this.iv=this.provider.iv;
}
公共对称密码(字节[]键,字节[]iv)
{
this.key=key;
这4.iv=iv;
}
公共对称密码(字符串密码、字符串salt)
{
Rfc2898DeriveBytes-deriveBytes=新的Rfc2898DeriveBytes(密码,this.utf8.GetBytes(salt));
this.key=deriveBytes.GetBytes(this.provider.KeySize>>3);
this.iv=deriveBytes.GetBytes(16);
}
公共对称密码(字符串密码、字符串salt、int迭代)
{
Rfc2898DeriveBytes-deriveBytes=新的Rfc2898DeriveBytes(密码,this.utf8.GetBytes(salt),迭代次数);
this.key=deriveBytes.GetBytes(this.provider.KeySize>>3);
this.iv=deriveBytes.GetBytes(16);
}
公共字节[]加密(字节[]输入)
{
返回this.Encrypt(输入,this.key,this.iv);
}
公共字节[]加密(字节[]输入,字节[]密钥,字节[]iv)
{
把这个还给我,变换一下(
输入,
this.provider.CreateEncryptor(key,iv));
}
公共字节[]解密(字节[]输入)
{
返回this.Decrypt(输入,this.key,this.iv);
}
公共字节[]解密(字节[]输入,字节[]密钥,字节[]iv)
{
把这个还给我,变换一下(
输入,
this.provider.CreateDecryptor(key,iv));
}
公共字符串加密(字符串文本)
{
返回this.Encrypt(text,this.key,this.iv);
}
公共字符串加密(字符串文本,字节[]密钥,字节[]iv)
{
字节[]输出=此.Transform(
这个.utf8.GetBytes(文本),
this.provider.CreateEncryptor(key,iv));
返回Convert.tobase64字符串(输出);
}
公共字符串解密(字符串文本)
{
返回this.Decrypt(text,this.key,this.iv);
}
公共字符串解密(字符串文本,字节[]密钥,字节[]iv)
{
字节[]输出=此.Transform(
转换.fromBase64字符串(文本),
this.provider.CreateDecryptor(key,iv));
返回这个.utf8.GetString(output,0,output.Length);
}
公共void加密(流输入、流输出)
{
this.Encrypt(输入、输出、this.key、this.iv);
}
public void Encrypt(流输入、流输出、字节[]密钥、字节[]iv)
{
此.TransformStream(true、ref输入、ref输出、键、iv);
}
公共无效解密(流输入、流输出)
{
this.Decrypt(输入、输出、this.key、this.iv);
}
公共无效解密(流输入、流输出、字节[]密钥、字节[]iv)
{
此.TransformStream(false,ref输入,ref输出,key,iv);
}
专用字节[]转换(
字节[]输入,
ICryptoTransform(加密变换)
{
字节[]结果;
使用(MemoryStream MemoryStream=new MemoryStream())
{
使用(CryptoStream CryptoStream=新加密流)(
记忆流,
密码转换,
CryptoStreamMode.Write)
{
cryptStream.Write(输入,0,输入,长度);
cryptStream.FlushFinalBlock();
memoryStream.Position=0;
结果=memoryStream.ToArray();
}
}
返回结果;
}
私有void TransformStream(bool加密、ref流输入、ref流输出、字节[]密钥、字节[]iv)
{
//防御性论证检验
如果(输入==null)
{
抛出新的ArgumentNullException(“输入”);
}
if(输出==null)
{
抛出新的ArgumentNullException(“输出”);
}
如果(!input.CanRead)
{
抛出新的ArgumentException(“无法读取输入流。”,“输入”);
}
如果(!output.CanWrite)
{
抛出新ArgumentException(“无法写入输出流。”,“输出”);
}
//使缓冲区足够大,以便
//要处理的流的部分
byte[]inputBuffer=新字节[input.Length-input.Position];
//将流读入缓冲区
读取(inputBuffer,0,inputBuffer.Length);
//转换缓冲区
字节[]outputBuffer=加密?加密(inputBuffer,密钥,iv)
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);
}
}
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
}
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); ;
}