3

在过去的几天里,我一直在尝试将用于登录海岸奇才角色生成器 API 的简单加密(在此处找到)(这样我的应用程序可以直接从服务器下载用户的角色)移植到我可以在 Windows 8 Metro 应用程序中使用,因为 AesManaged 没有进入 Metro 加密库。鉴于我充其量只是一个新手程序员,这已经证明有点超出我的技能。

这是我需要移植的代码:

public static byte[] SimpleEncrypt(string value, string key)
{
    byte[] buffer2;
    ICryptoTransform transform = GetSimpleAlgorithm(key).CreateEncryptor();
    using (MemoryStream stream = new MemoryStream())
    {
        using (CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write))
        {
            byte[] bytes = Encoding.UTF8.GetBytes(value);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.Flush();
            stream2.FlushFinalBlock();
            stream.Position = 0L;
            buffer2 = stream.ToArray();
        }
    }
    return buffer2;
}

private static SymmetricAlgorithm GetSimpleAlgorithm(string key)
{
    AesManaged aes = new AesManaged();
    byte[] source = new SHA256Managed().ComputeHash(Encoding.UTF8.GetBytes(key));
    return new AesManaged { Key = source, IV = source.Take<byte>((aes.BlockSize / 8)).ToArray<byte>() };
}

这用于在通过登录之前加密密码:

contentClient.Login(username, SimpleEncrypt(password, username));

如果需要,Web 服务位于:http: //ioun.wizards.com/ContentVault.svc

在第一个链接中对该链接的评论中,有人在 2 月份为 Windows 8 提出了一些代码,但说代码有一些问题我必须在它编译之前修复,即使那样,当我尝试用它登录,我从服务中得到一个异常,说“填充无效并且无法删除”。

这是我目前正在使用的内容:

private static byte[] SimpleEncrypt(string value, string key)
{
    var simpleAlgorithm = GetSimpleAlgorithm(key);
    var encryptedBuffer = CryptographicEngine.Encrypt(simpleAlgorithm.Item1, CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8), simpleAlgorithm.Item2);
    var result = new byte[encryptedBuffer.Length];
    CryptographicBuffer.CopyToByteArray(encryptedBuffer, out result);
    return result;
}

private static Tuple<CryptographicKey, IBuffer> GetSimpleAlgorithm(string key)
{
    var provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
    var keyAsBinary = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
    var source = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256).HashData(keyAsBinary);
    var shortKey = CryptographicBuffer.CreateFromByteArray(UTF8Encoding.UTF8.GetBytes(key).Take((int)provider.BlockLength).ToArray());

    return new Tuple<CryptographicKey,IBuffer>(provider.CreateSymmetricKey(source), shortKey);
}

任何帮助让这项工作将不胜感激。

4

1 回答 1

1

Welp,花了更多时间查看它,并找出了问题所在。

建议的更新版本使用“密钥”字符串来创建 IV,而它应该使用密钥的哈希值。

这是功能版本,以防万一有人想要它:

private static byte[] SimpleEncrypt(string value, string key)
{
    var simpleAlgorithm = GetSimpleAlgorithm(key);
    CryptographicKey encryptKey = simpleAlgorithm.Item1;
    IBuffer IV = simpleAlgorithm.Item2;
    var encryptedBuffer = CryptographicEngine.Encrypt(encryptKey, CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8), IV);

    var result = new byte[encryptedBuffer.Length];
    CryptographicBuffer.CopyToByteArray(encryptedBuffer, out result);
    return result;
}

private static Tuple<CryptographicKey, IBuffer> GetSimpleAlgorithm(string key)
{
    var provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
    var keyAsBinary = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
    var source = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256).HashData(keyAsBinary);

    byte[] sourceArray = new byte[source.Length];
    CryptographicBuffer.CopyToByteArray(source, out sourceArray);

    var shortKey = CryptographicBuffer.CreateFromByteArray(sourceArray.Take((int)provider.BlockLength).ToArray());

    return new Tuple<CryptographicKey,IBuffer>(provider.CreateSymmetricKey(source), shortKey);
}
于 2012-11-06T12:33:13.720 回答