适用于Windows 7和8的密码术

时间:2013-12-21 05:17:46

标签: windows winforms cryptography windows-store-apps

继续提问 c# Cryptography that works for windows phone 7 and 8 app, silverlight clients and windows tablet apps

我正在尝试找出Windows应用商店和Windows 7应用的功能主体。

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

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

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

    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

在Windows 8中,以下工作

    ' 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之间通用的编码和解码算法是什么?

1 个答案:

答案 0 :(得分:0)

在Windows 7中,您使用EncIV作为EncIVBA,但在Windows 8代码中,您使用data1("某些测试数据来检查此编码正确")作为EncIV,应该输出非常不同的结果。 有时这个问题看起来非常艰难,但真的很简单哈哈哈,我很幸运。“