首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >适用于windows 7和8的加密技术

适用于windows 7和8的加密技术
EN

Stack Overflow用户
提问于 2013-12-21 13:17:46
回答 1查看 173关注 0票数 1

从问题c# Cryptography that works for windows phone 7 and 8 app, silverlight clients and windows tablet apps开始

我正在尝试弄清楚windows store应用程序和windows7应用程序的函数主体。

任务密钥( byte[] AesEncrypt,byte[] data,byte[] iv);

任务密钥( byte[] AesDecrypt,byte[] data,byte[] iv);

对于Windows7,以下代码可以正常工作

代码语言:javascript
复制
    Dim Password As String = "PasswordPassword"
    Dim data1 As String = "Some test data to check this encodes correctly  "

    Dim PasswordBA = System.Text.UTF8Encoding.UTF8.GetBytes(Password.ToArray)
    Dim EncIVBA() As Byte = {183, 124, 217, 35, 247, 115, 33, 34, 191, 47, 186, 52, 54, 145, 56, 14}

    ' Encrypt the data. 
    Dim encAlg = System.Security.Cryptography.Aes.Create()
    encAlg.Key = PasswordBA
    encAlg.IV = EncIVBA
    Dim encryptionStream As New MemoryStream()
    Dim encrypt As New CryptoStream(encryptionStream, encAlg.CreateEncryptor(), CryptoStreamMode.Write)
    Dim utfD1 As Byte() = New System.Text.UTF8Encoding(False).GetBytes(data1)
    encrypt.Write(utfD1, 0, utfD1.Length)
    encrypt.FlushFinalBlock()
    encrypt.Close()
    Dim edata1 As Byte() = encryptionStream.ToArray() ' 40, 104 ..
    IO.File.WriteAllBytes("C:\Users\Ralph\AppData\Local\Packages\5ff3fe41-65d0-4e09-b1db-9082e9763abb_e4zkcfr8ebjb0\LocalState\ForWin8.dat", edata1)

    ' Decrypt, thus showing it can be round-tripped. 
    'Dim EData2 = IO.File.ReadAllBytes("D:\ForWin8.dat")
    Dim EData2 = IO.File.ReadAllBytes("C:\Users\Ralph\AppData\Local\Packages\5ff3fe41-65d0-4e09-b1db-9082e9763abb_e4zkcfr8ebjb0\LocalState\ForWin7.dat")
    Dim decAlg = System.Security.Cryptography.Aes.Create
    decAlg.Key = PasswordBA
    decAlg.IV = EncIVBA
    Dim decryptionStreamBacking As New MemoryStream()
    Dim decrypt As New CryptoStream(decryptionStreamBacking, decAlg.CreateDecryptor(), CryptoStreamMode.Write)
    decrypt.Write(edata2, 0, edata2.Length)
    decrypt.Flush()
    decrypt.Close()
    Dim data2 As String = New UTF8Encoding(False).GetString(decryptionStreamBacking.ToArray())

    If Not data1.Equals(data2) Then
        Console.WriteLine("Error: The two values are not equal.")
    Else
        Console.WriteLine("The two values are equal.")
    End If

在windows8中,下面的代码是有效的:

代码语言:javascript
复制
    ' encrypt
    Dim data1 As String = "Some test data to check this encodes correctly"
    Dim Data1Len = data1.Length
    Dim Data1LenPad = ((Data1Len - 1) Or 15) + 1
    data1 = data1.PadRight(Data1LenPad, " "c)
    Dim algname = SymmetricAlgorithmNames.AesCbc
    Dim alg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(algname)
    Dim pw = CryptographicBuffer.ConvertStringToBinary("PasswordPassword", BinaryStringEncoding.Utf8)
    Dim bl = alg.BlockLength
    Dim buffMsg = CryptographicBuffer.ConvertStringToBinary(data1, BinaryStringEncoding.Utf8)

    Dim EncIVBA() As Byte = {183, 124, 217, 35, 247, 115, 33, 34, 191, 47, 186, 52, 54, 145, 56, 14}
    Dim EncIV = CryptographicBuffer.ConvertStringToBinary(data1, BinaryStringEncoding.Utf8)

    Dim key = alg.CreateSymmetricKey(pw)
    Dim Enc = CryptographicEngine.Encrypt(key, buffMsg, EncIV)

    Dim EncLen = Enc.Length
    Dim Encba As Byte()
    ReDim Encba(CInt(EncLen - 1))
    CryptographicBuffer.CopyToByteArray(Enc, Encba)

    Dim file1 As Windows.Storage.StorageFile = Await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("ForWin7.dat", Windows.Storage.CreationCollisionOption.ReplaceExisting)
    Dim fileStream1 As Stream = Await file1.OpenStreamForWriteAsync()
    fileStream1.Write(Encba, 0, Encba.Length)
    Await fileStream1.FlushAsync()

    'Dim file As Windows.Storage.StorageFile = Await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("ForWin7.dat")
    Dim file As Windows.Storage.StorageFile = Await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("ForWin8.dat")
    Dim Enc2 = Await FileIO.ReadBufferAsync(file)

    Dim Data2Buf = CryptographicEngine.Decrypt(key, Enc2, EncIV)
    Dim Data2 = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, Data2Buf)

    If Not data1.Equals(Data2) Then
        Dim Status = "Error: The two values are not equal."
    Else
        Dim Status = "The two values are equal."
    End If

但是文件是不一样的。Windows 7代码无法解码Windows 8代码生成的文件,反之亦然。

Windows 7和Windows 8通用的编码和解码算法是什么?

EN

回答 1

Stack Overflow用户

发布于 2017-03-06 23:25:32

在Windows7中,你使用EncIV作为EncIVBA,但在Windows8代码中,你使用data1 (“一些测试数据来检查编码正确”)作为EncIV,这应该会输出非常不同的结果。有时候这个问题看起来真的很难,但其实很简单哈哈哈,我在这个问题上很幸运。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20715735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档