↓ 跳过正文
  1. 文章/

Windows 8中的AES解密

Rubén Infante Docio
作者
Rubén Infante Docio
软件工程师

今天我们要谈的是加密的美好世界,而不是](http://www.antonio-ramos.es/2008/06/cifrar-versus-encriptar.html)加密[(被放入加密空间)。

安全性很重要,这就是为什么我们将探讨如何在服务器端加密以及在Metro应用中解密Windows 8客户端的原因。

在这个例子中,我们将使用带有256字节加密的AES(军用加密)

服务队伍
#

public string Get()
        {
            //
            String original = "1234567812345678";
            String key = "CLAVEDESEGURIDAD";
            byte[] KeyArr = System.Text.Encoding.ASCII.GetBytes(key);
            String iv = "VECTORINICIALIZACION";
            byte[] IvArr = System.Text.Encoding.ASCII.GetBytes(iv);
            using (AesManaged myAes = new AesManaged())
            {
                return EncryptStringToBytes_Aes(original, KeyArr, IvArr);
            }

        }
        static string EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
        {
            byte[] encrypted;
            using (AesManaged aesAlg = new AesManaged())
            {
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(Key, IV);

                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(plainText);
                            swEncrypt.Flush();
                            encrypted = msEncrypt.ToArray();
                            return Convert.ToBase64String(encrypted);
                        }
                    }
                }
            }

        }

客户部分

 function RunSample(cadenacifradaenbase64) {

	var Algorithm = Windows.Security.Cryptography.Core.SymmetricKeyAlgorithmProvider.openAlgorithm("AES_CBC");
        	var keymaterial2 = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary("CLAVEDESEGURIDAD", Windows.Security.Cryptography.BinaryStringEncoding.utf8);
        	var key2;
        	try {
        	    key2 = Algorithm.createSymmetricKey(keymaterial2);

        	}
       	 catch (ex) {
        	    Scenario5Output.textContent += ex.message;
        	    Scenario5Output.textContent += "An invalid key size was selected for the given algorithm.n";
        	    return;
       	 }
	var bufferCifrado = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(cadenacifradaenbase64);
	var iv = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary("VECTORINICIALIZACION", Windows.Security.Cryptography.BinaryStringEncoding.utf8);
	var decrypted = Windows.Security.Cryptography.Core.CryptographicEngine.decrypt(key2, bufferCifrado , iv);
        var solucion = Windows.Security.Cryptography.CryptographicBuffer.convertBinaryToString(Windows.Security.Cryptography.BinaryStringEncoding.utf8, decrypted);
}

如果我发现你喜欢这类文章,第二天我们会反过来进行

相关文章