积极答复者
加密算法 Aes Sha ,c#开发metro

问题
答案
-
Hi,
Metro提供了完整的密码学支持,主要被定义在Windows.Security.Cryptography和Windows.Security.Cryptography.Core中。
下面我分别实现了Hash算法sha-1以及使用AES-CBC对称密钥算法进行加密和解密的过程:
Sha-1:
public void hash() { String str1 = "This is a texxxxxxxxxt."; this.input.Text = str1; HashAlgorithmProvider hashProvider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1); CryptographicHash hashEngine = hashProvider.CreateHash(); IBuffer buffin = CryptographicBuffer.ConvertStringToBinary(str1, BinaryStringEncoding.Utf16BE); hashEngine.Append(buffin); IBuffer buffout = hashEngine.GetValueAndReset(); this.output.Text = CryptographicBuffer.EncodeToHexString(buffout); }
AES-CBC加密及解密:
public void aes() { uint keySize=32; String str2 = "This111111111111"; this.input2.Text = str2; //encrypt SymmetricKeyAlgorithmProvider aesProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbc); CryptographicKey aesKey = aesProvider.CreateSymmetricKey(CryptographicBuffer.GenerateRandom(keySize)); IBuffer buffin=CryptographicBuffer.ConvertStringToBinary(str2,BinaryStringEncoding.Utf16BE); IBuffer iv=CryptographicBuffer.GenerateRandom((uint)aesProvider.BlockLength); IBuffer buffout = CryptographicEngine.Encrypt(aesKey, buffin, iv); this.output2.Text = CryptographicBuffer.EncodeToHexString(buffout); //decrypt IBuffer buffde = CryptographicEngine.Decrypt(aesKey, buffout, iv); this.output3.Text = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf16BE,buffde); }
在这里简单的解释一下,密码学算法实际上是对数字的运算,因此在读入字符串的时候会对字符串进行转换转换成unicode码之后可以转换成16进制数字。在完成计算之后,由于内存中存储的是16位数字,并不能直接以字符的形式显示出来。不过CryptographicBuffer类提供了一个非常好用的方法EncodeToHexString让我们可以看到结果。
在写这个实现当中也发现了一个需要注意的问题。AES加密或者解密的时候是按照块(block)来进行,因此AES就会对数据按照BlockSize进行分割,如果数据不能够正好进行分割,会进行补齐。一般来说AES的实现在算法中都会自动补齐,但是我发现在Metro这里并没有自动补齐,而是需要人为的手动补齐。AES一般使用的是128bit的BlockSize也就是16个八位组(octet)而一个字符按照上面的转换会是16bit,即2个八位组, 换句话说如果你想自己补齐的话需要保证字符的数量是8的倍数或者说数据的字节数是16的倍数。
我查找了Metro提供的示例,发现那里面也没有手动的补齐,但是却进行了判断,如果不是倍数的话会抛出错误。这上面的算法我也没有进行补齐,而是输入了16个字符,事实上做起来很简单只要在最后添加空格即可。不过如果不注意这个问题可能会遇到报错。
下面是参考文档:
RCF 3062 AESCBC标准:
http://datatracker.ietf.org/doc/rfc3602/?include_text=1
Windows.Security.Cryptography namespace:
http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.aspx
Windows.Security.Cryptography.Core namespace:
http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.aspxHash算法的示例:
http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.cryptographichash.aspx
AES-CBC算法示例:
http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.symmetrickeyalgorithmprovider.aspxHope this helps
Aaron Xue [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已编辑 Aaron XueModerator 2012年7月25日 16:18
- 已建议为答案 Jie BaoModerator 2012年7月26日 5:38
- 已标记为答案 Aaron XueModerator 2012年7月30日 7:57
全部回复
-
Hi,
Metro提供了完整的密码学支持,主要被定义在Windows.Security.Cryptography和Windows.Security.Cryptography.Core中。
下面我分别实现了Hash算法sha-1以及使用AES-CBC对称密钥算法进行加密和解密的过程:
Sha-1:
public void hash() { String str1 = "This is a texxxxxxxxxt."; this.input.Text = str1; HashAlgorithmProvider hashProvider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1); CryptographicHash hashEngine = hashProvider.CreateHash(); IBuffer buffin = CryptographicBuffer.ConvertStringToBinary(str1, BinaryStringEncoding.Utf16BE); hashEngine.Append(buffin); IBuffer buffout = hashEngine.GetValueAndReset(); this.output.Text = CryptographicBuffer.EncodeToHexString(buffout); }
AES-CBC加密及解密:
public void aes() { uint keySize=32; String str2 = "This111111111111"; this.input2.Text = str2; //encrypt SymmetricKeyAlgorithmProvider aesProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbc); CryptographicKey aesKey = aesProvider.CreateSymmetricKey(CryptographicBuffer.GenerateRandom(keySize)); IBuffer buffin=CryptographicBuffer.ConvertStringToBinary(str2,BinaryStringEncoding.Utf16BE); IBuffer iv=CryptographicBuffer.GenerateRandom((uint)aesProvider.BlockLength); IBuffer buffout = CryptographicEngine.Encrypt(aesKey, buffin, iv); this.output2.Text = CryptographicBuffer.EncodeToHexString(buffout); //decrypt IBuffer buffde = CryptographicEngine.Decrypt(aesKey, buffout, iv); this.output3.Text = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf16BE,buffde); }
在这里简单的解释一下,密码学算法实际上是对数字的运算,因此在读入字符串的时候会对字符串进行转换转换成unicode码之后可以转换成16进制数字。在完成计算之后,由于内存中存储的是16位数字,并不能直接以字符的形式显示出来。不过CryptographicBuffer类提供了一个非常好用的方法EncodeToHexString让我们可以看到结果。
在写这个实现当中也发现了一个需要注意的问题。AES加密或者解密的时候是按照块(block)来进行,因此AES就会对数据按照BlockSize进行分割,如果数据不能够正好进行分割,会进行补齐。一般来说AES的实现在算法中都会自动补齐,但是我发现在Metro这里并没有自动补齐,而是需要人为的手动补齐。AES一般使用的是128bit的BlockSize也就是16个八位组(octet)而一个字符按照上面的转换会是16bit,即2个八位组, 换句话说如果你想自己补齐的话需要保证字符的数量是8的倍数或者说数据的字节数是16的倍数。
我查找了Metro提供的示例,发现那里面也没有手动的补齐,但是却进行了判断,如果不是倍数的话会抛出错误。这上面的算法我也没有进行补齐,而是输入了16个字符,事实上做起来很简单只要在最后添加空格即可。不过如果不注意这个问题可能会遇到报错。
下面是参考文档:
RCF 3062 AESCBC标准:
http://datatracker.ietf.org/doc/rfc3602/?include_text=1
Windows.Security.Cryptography namespace:
http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.aspx
Windows.Security.Cryptography.Core namespace:
http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.aspxHash算法的示例:
http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.cryptographichash.aspx
AES-CBC算法示例:
http://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.symmetrickeyalgorithmprovider.aspxHope this helps
Aaron Xue [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已编辑 Aaron XueModerator 2012年7月25日 16:18
- 已建议为答案 Jie BaoModerator 2012年7月26日 5:38
- 已标记为答案 Aaron XueModerator 2012年7月30日 7:57