none
Metro Style AES 加解密 RRS feed

  • 问题

  •  public static void Test()
    {            

                string ss = "";
                string s1 = "";
                uint keySize = 16;
                String str2 = "1234567890123456";
                String str3 = "YqweYp9SoT+xkdmz6tCb7KqjLgZmAp9BESGQhnuMl5I=";
                ss = str2;

                HashAlgorithmProvider hashProvider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
                CryptographicHash hashEngine = hashProvider.CreateHash();

                IBuffer buffinto = CryptographicBuffer.ConvertStringToBinary("password", BinaryStringEncoding.Utf8);
                hashEngine.Append(buffinto);
                IBuffer buffoutto = hashEngine.GetValueAndReset();
                byte[] b = BufferToBytes(buffoutto);
                byte[] bytess = new byte[16];

                for (int i = 0; i < 16; i++)
                {
                    bytess[i] = b[i];
                }

                byte[] bytessa = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                IBuffer iv = BytesToBuffer(bytessa);
                //encrypt
                byte[] bytes = new byte[16] { 0x5B, 0xAA, 0x61, 0xE4, 0xC9, 0xB9, 0x3F, 0x3F, 0x06, 0x82, 0x25, 0x0B, 0x6C, 0xF8, 0x33, 0x1B };
                IBuffer ib = BytesToBuffer(bytes);
                SymmetricKeyAlgorithmProvider aesProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
                CryptographicKey aesKey = aesProvider.CreateSymmetricKey(ib);

                IBuffer buffin = CryptographicBuffer.ConvertStringToBinary(str2, BinaryStringEncoding.Utf8);
                IBuffer buffout = CryptographicEngine.Encrypt(aesKey, buffin, iv);
                byte[] b1 = BufferToBytes(buffout);
                ss = CryptographicBuffer.EncodeToHexString(buffout);
                string signature = CryptographicBuffer.EncodeToBase64String(buffout);

                //decrypt
                IBuffer buffde = CryptographicEngine.Decrypt(aesKey, buffout, iv);
                s1 = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffde);

    }

    我先加密str2 可以得到加密后的数据str3

    在解密 可以再得到数据str2

    当我直接解密str3的时候 会报错 说“提供给请求操作的用户缓冲区无效。 (异常来自 HRESULT:0x800706F8)”

    请高手提示 这是什么原因啊?

    谢谢!

    2013年3月15日 5:33

答案

  • Hi,

    我没有发现任何问题,你给我的代码不全,BytesToBuffer没有给我,而且也没有给我你用什么方法转换Base64字符串的。因此我自己补全之后没有发现问题,下面是我的代码:

     public static void Test()
            {
    
                string ss = "";
                string s1 = "";
                uint keySize = 16;
                String str2 = "1234567890123456";
                String str3 = "YqweYp9SoT+xkdmz6tCb7KqjLgZmAp9BESGQhnuMl5I=";
                ss = str2;
    
                HashAlgorithmProvider hashProvider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
                CryptographicHash hashEngine = hashProvider.CreateHash();
    
                IBuffer buffinto = CryptographicBuffer.ConvertStringToBinary("password", BinaryStringEncoding.Utf8);
                hashEngine.Append(buffinto);
                IBuffer buffoutto = hashEngine.GetValueAndReset();
                byte[] b = BufferToBytes(buffoutto);
                byte[] bytess = new byte[16];
    
                for (int i = 0; i < 16; i++)
                {
                    bytess[i] = b[i];
                }
    
                byte[] bytessa = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                IBuffer iv = BytesToBuffer(bytessa);
                //encrypt
                byte[] bytes = new byte[16] { 0x5B, 0xAA, 0x61, 0xE4, 0xC9, 0xB9, 0x3F, 0x3F, 0x06, 0x82, 0x25, 0x0B, 0x6C, 0xF8, 0x33, 0x1B };
                IBuffer ib = BytesToBuffer(bytes);
                SymmetricKeyAlgorithmProvider aesProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
                CryptographicKey aesKey = aesProvider.CreateSymmetricKey(ib);
    
                IBuffer buffin = CryptographicBuffer.ConvertStringToBinary(str2, BinaryStringEncoding.Utf8);
                IBuffer buffout = CryptographicEngine.Encrypt(aesKey, buffin, iv);
                byte[] b1 = BufferToBytes(buffout);
                ss = CryptographicBuffer.EncodeToHexString(buffout);
                string signature = CryptographicBuffer.EncodeToBase64String(buffout);
    
                //decrypt
                IBuffer buffout2 = CryptographicBuffer.DecodeFromBase64String(signature);
                IBuffer buffde = CryptographicEngine.Decrypt(aesKey, buffout2, iv);
                s1 = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffde);
    
            }
    
            static byte[] BufferToBytes(IBuffer buf)
            {
                return WindowsRuntimeBufferExtensions.ToArray(buf, 0, (int)buf.Length);
            }
    
            static IBuffer BytesToBuffer(byte[] bytes)
            {
                return WindowsRuntimeBufferExtensions.AsBuffer(bytes, 0, bytes.Length);
            }
    

    IBuffer和byte[]相互转换的时候我用了简单的方法,不过实质是一样的。

    Aaron
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 JK_186 2013年3月20日 7:07
    2013年3月19日 7:45
    版主

全部回复

  • Hi,

    能把你BufferToBytes()这个方法提供给我一下嘛?这个似乎是你自己写的,你是用的什么方法转换的,是CryptographicBuffer.EncodeToHexString()这个吗?


    Aaron
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2013年3月18日 8:39
    版主
  •         static byte[] BufferToBytes(IBuffer buf)
            {
                using (var dataReader = DataReader.FromBuffer(buf))
                {
                    var bytes = new byte[buf.Capacity];
                    dataReader.ReadBytes(bytes);
                    return bytes;
                }
            }

    这个是我写的BufferToBytes()方法 麻烦你看下 谢谢了!

    2013年3月19日 1:41
  • Hi,

    我没有发现任何问题,你给我的代码不全,BytesToBuffer没有给我,而且也没有给我你用什么方法转换Base64字符串的。因此我自己补全之后没有发现问题,下面是我的代码:

     public static void Test()
            {
    
                string ss = "";
                string s1 = "";
                uint keySize = 16;
                String str2 = "1234567890123456";
                String str3 = "YqweYp9SoT+xkdmz6tCb7KqjLgZmAp9BESGQhnuMl5I=";
                ss = str2;
    
                HashAlgorithmProvider hashProvider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
                CryptographicHash hashEngine = hashProvider.CreateHash();
    
                IBuffer buffinto = CryptographicBuffer.ConvertStringToBinary("password", BinaryStringEncoding.Utf8);
                hashEngine.Append(buffinto);
                IBuffer buffoutto = hashEngine.GetValueAndReset();
                byte[] b = BufferToBytes(buffoutto);
                byte[] bytess = new byte[16];
    
                for (int i = 0; i < 16; i++)
                {
                    bytess[i] = b[i];
                }
    
                byte[] bytessa = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                IBuffer iv = BytesToBuffer(bytessa);
                //encrypt
                byte[] bytes = new byte[16] { 0x5B, 0xAA, 0x61, 0xE4, 0xC9, 0xB9, 0x3F, 0x3F, 0x06, 0x82, 0x25, 0x0B, 0x6C, 0xF8, 0x33, 0x1B };
                IBuffer ib = BytesToBuffer(bytes);
                SymmetricKeyAlgorithmProvider aesProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
                CryptographicKey aesKey = aesProvider.CreateSymmetricKey(ib);
    
                IBuffer buffin = CryptographicBuffer.ConvertStringToBinary(str2, BinaryStringEncoding.Utf8);
                IBuffer buffout = CryptographicEngine.Encrypt(aesKey, buffin, iv);
                byte[] b1 = BufferToBytes(buffout);
                ss = CryptographicBuffer.EncodeToHexString(buffout);
                string signature = CryptographicBuffer.EncodeToBase64String(buffout);
    
                //decrypt
                IBuffer buffout2 = CryptographicBuffer.DecodeFromBase64String(signature);
                IBuffer buffde = CryptographicEngine.Decrypt(aesKey, buffout2, iv);
                s1 = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffde);
    
            }
    
            static byte[] BufferToBytes(IBuffer buf)
            {
                return WindowsRuntimeBufferExtensions.ToArray(buf, 0, (int)buf.Length);
            }
    
            static IBuffer BytesToBuffer(byte[] bytes)
            {
                return WindowsRuntimeBufferExtensions.AsBuffer(bytes, 0, bytes.Length);
            }
    

    IBuffer和byte[]相互转换的时候我用了简单的方法,不过实质是一样的。

    Aaron
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 JK_186 2013年3月20日 7:07
    2013年3月19日 7:45
    版主
  • static byte[] BufferToBytes(IBuffer buf)
            {
                using (var dataReader = DataReader.FromBuffer(buf))
                {
                    var bytes = new byte[buf.Capacity];
                    dataReader.ReadBytes(bytes);
                    return bytes;
                }
            }

            static IBuffer BytesToBuffer(byte[] bytes)
            {
                using (var dataWriter = new DataWriter())
                {
                    dataWriter.WriteBytes(bytes);
                    return dataWriter.DetachBuffer();
                }
            }

    你好!上面是我的转换方法

    可能是我在解密那个密文的时候 没有先把他从base64转回来

     IBuffer buffout2 = CryptographicBuffer.DecodeFromBase64String(signature);

    我看你添加了这句 我把这个添加上 问题就解决了

    谢谢你的帮助 谢谢!

    2013年3月20日 7:12
  • 您好 我也有同样的问题 能帮忙解决下吗
    2014年5月18日 6:52