字符串加密/解密密码c#Metro Style [英] String encrypt / decrypt with password c# Metro Style

查看:199
本文介绍了字符串加密/解密密码c#Metro Style的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用密码加密和解密字符串。我使用C#和WinRT(MetroStyle)。有人为加密/解密类?

解决方案

Metro中不存在普通的.Net System.Security.Cryptography命名空间。您使用加密引擎类中的 Windows.Security.Cryptography.Core 命名空间。



如果密码仅经过验证/验证,请勿加密。相反,请使用以下内容:

 使用Windows.Security.Cryptography.Core; 
使用Windows.Security.Cryptography;
使用Windows.Storage.Streams;

...
//使用基于密码的密钥导出函数2(PBKDF2或RFC2898)
KeyDerivationAlgorithmProvider pbkdf2 =
KeyDerivationAlgorithmProvider.OpenAlgorithm(
KeyDerivationAlgorithmNames。 Pbkdf2Sha256);

//如果可以避免这些密码,请勿将密码存储在字符串中。
//密码可能会保留在内存中,直到它被垃圾回收为止。
//崩溃应用程序并查看内存转储可能
//显示它。
IBuffer passwordBuffer =
CryptographicBuffer.ConvertStringToBinary(password,
BinaryStringEncoding.Utf8);
CryptographicKey key = pbkdf2.CreateKey(passwordBuffer);

//使用随机盐和10,000次迭代。存储盐与
// derviedBytes(见下文)。
IBuffer salt = CryptographicBuffer.GenerateRandom(32);
KeyDerivationParameters参数=
KeyDerivationParameters.BuildForPbkdf2(salt,10000);

//存储返回的32个字节和盐以供以后验证
byte [] derviedBytes =
CryptographicEngine.DeriveKeyMaterial(key,parameters,32).ToArray();

当提供密码时,使用相同的盐运行相同的进程并比较derivedBytes。存储秘密,就像加密密钥一样。



如果使用密码,例如连接到其他服务:

  //使用AES,CBC模式与PKCS#7填充(良好的默认选择)
SymmetricKeyAlgorithmProvider aesCbcPkcs7 =
SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7) ;

//创建AES 128位(16字节)密钥
加密密钥=
aesCbcPkcs7.CreateSymmetricKey(CryptographicBuffer.GenerateRandom(16));

// Creata一个16字节的初始化向量
IBuffer iv = CryptographicBuffer.GenerateRandom(aesCbcPkcs7.BlockLength);

//加密数据
byte [] plainText = Encoding.UTF8.GetBytes(Hello,world!); //数据加密
byte [] cipherText = CryptographicEngine.Encrypt(
key,plainText.AsBuffer(),iv).ToArray();

//解密数据
string newPlainText = new string(
Encoding.UTF8.GetChars(CryptographicEngine.Decrypt(
key,cipherText.AsBuffer(),iv ).ToArray()));

// newPlainText包含Hello,world!

与任何加密技术一样,请确保正确保护您的密钥并遵循最佳做法。链接的文档还提供了示例。


I would like to encrypt and decrypt strings with a password. I use C# and WinRT (MetroStyle). Have somebody a class for encryption/decryption?

解决方案

The normal .Net System.Security.Cryptography namespace does not exist in Metro. You use the CryptographicEngine class in Windows.Security.Cryptography.Core namespace instead.

If the password is only being verified/authenticated, do not encrypt it. Instead, use the following:

using Windows.Security.Cryptography.Core;
using Windows.Security.Cryptography;
using Windows.Storage.Streams;

...
// Use Password Based Key Derivation Function 2 (PBKDF2 or RFC2898)
KeyDerivationAlgorithmProvider pbkdf2 = 
    KeyDerivationAlgorithmProvider.OpenAlgorithm(
        KeyDerivationAlgorithmNames.Pbkdf2Sha256);

// Do not store passwords in strings if you can avoid them. The
// password may be retained in memory until it is garbage collected.
// Crashing the application and looking at the memory dump may 
// reveal it.
IBuffer passwordBuffer = 
     CryptographicBuffer.ConvertStringToBinary("password", 
         BinaryStringEncoding.Utf8);
CryptographicKey key = pbkdf2.CreateKey(passwordBuffer);

// Use random salt and 10,000 iterations. Store the salt along with 
// the derviedBytes (see below).
IBuffer salt = CryptographicBuffer.GenerateRandom(32);
KeyDerivationParameters parameters = 
    KeyDerivationParameters.BuildForPbkdf2(salt, 10000);

// Store the returned 32 bytes along with the salt for later verification
byte[] derviedBytes = 
    CryptographicEngine.DeriveKeyMaterial(key, parameters, 32).ToArray();

When a password is supplied run through the same process using the same salt and compare derivedBytes. Store the secret as you would an encryption key.

If the password will be used, such as to connect to another service:

// Use AES, CBC mode with PKCS#7 padding (good default choice)
SymmetricKeyAlgorithmProvider aesCbcPkcs7 = 
    SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);

// Create an AES 128-bit (16 byte) key
CryptographicKey key = 
    aesCbcPkcs7.CreateSymmetricKey(CryptographicBuffer.GenerateRandom(16));

// Creata a 16 byte initialization vector
IBuffer iv = CryptographicBuffer.GenerateRandom(aesCbcPkcs7.BlockLength);

// Encrypt the data
byte[] plainText = Encoding.UTF8.GetBytes("Hello, world!"); // Data to encrypt
byte[] cipherText = CryptographicEngine.Encrypt(
    key, plainText.AsBuffer(), iv).ToArray();

// Decrypt the data
string newPlainText = new string(
    Encoding.UTF8.GetChars(CryptographicEngine.Decrypt(
        key, cipherText.AsBuffer(), iv).ToArray()));

// newPlainText contains "Hello, world!"

As with any cryptography, make sure to protect your keys appropriately and follow best practise. The linked documentation also provides examples.

这篇关于字符串加密/解密密码c#Metro Style的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆