在Windows Phone上进行加密

本文关键字:加密 Windows Phone | 更新日期: 2023-09-27 18:10:16

我编写了一些代码,允许我在c#中进行加密-主要使用System.Security.Cryptography中的AesManaged()SHA256Managed()

用例是该工具需要能够获取加密的数据,解密,显示给用户,允许编辑并在再次发送之前再次加密。

我希望能够在Windows Phone上做类似的事情,但似乎该命名空间在手机上不可用。

那么我现在有什么选择呢?它会在Windows Phone 10上运行吗?似乎在手机应用程序中进行加密操作是一项相对常见的任务?

编辑:添加关于应用程序应该做什么的信息

在Windows Phone上进行加密

你想用密码做什么?因为如果您只需要存储一些用户凭据,最好的方法是使用PasswordVault

MSDN参考此处https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.security.credentials.passwordvault.aspx

我在这里做了一个例子http://depblog.weblogs.us/2014/11/20/migrating-from-sl8-0-protectdata-to-rt8-1-passwordvault/

添加了一些关于如何从Vault中添加和删除条目的示例代码(更多细节请参见博客文章)

public async Task AddAccount(Account accountToAdd)
{
    //Reinitialize the vault to see if the given account is already available
    await this.InitializeSettingsService();
    Account accountFromVault = this.Accounts.FirstOrDefault(item => item.UserName.Equals(accountToAdd.UserName, StringComparison.OrdinalIgnoreCase));
    if(accountFromVault == null)
        _vault.Add(new PasswordCredential(Constants.VAULTRESOURCENAME, accountToAdd.UserName, accountToAdd.Password));
    if (accountFromVault != null && !accountFromVault.Password.Equals(accountToAdd.Password, StringComparison.Ordinal))
    {
        _vault.Remove(new PasswordCredential(Constants.VAULTRESOURCENAME, accountFromVault.UserName, accountFromVault.Password));
        _vault.Add(new PasswordCredential(Constants.VAULTRESOURCENAME, accountToAdd.UserName, accountToAdd.Password));
    }
    Account accountFromMemory = this.Accounts.FirstOrDefault(item => item.UserName.Equals(accountToAdd.UserName, StringComparison.OrdinalIgnoreCase));
    if (accountFromMemory != null)
    {
        if (!accountFromMemory.Password.Equals(accountToAdd.Password, StringComparison.OrdinalIgnoreCase))
        {
            this.Accounts.Remove(accountFromMemory);
            this.Accounts.Add(accountToAdd);
        }
    }
    else
        this.Accounts.Add(accountToAdd);
}
public async Task RemoveAccount(Account accountToRemove)
{
    //Reinitialize the vault to see if the given account is already available
    await this.InitializeSettingsService();
    Account accountFromVault = this.Accounts.FirstOrDefault(item => item.UserName.Equals(accountToRemove.UserName, StringComparison.OrdinalIgnoreCase));
    if (accountFromVault != null)
        _vault.Remove(new PasswordCredential(Constants.VAULTRESOURCENAME, accountToRemove.UserName, accountToRemove.Password));
    Account accountFromMemory = this.Accounts.FirstOrDefault(item => item.UserName.Equals(accountToRemove.UserName, StringComparison.OrdinalIgnoreCase));
    if (accountFromMemory != null)
        this.Accounts.Remove(accountFromMemory);
}

正如@WDS所指出的,用于加密的工具位于Windows.Security.Cryptography命名空间中-在#WP8上可用。

所以我重写了我的哈希实现,像这样:
public IBuffer ComputeHash(string value)
{
    IBuffer buffUtf8Msg = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);
    var objAlgProv = HashAlgorithmProvider.OpenAlgorithm("SHA256");
    var strAlgNameUsed = objAlgProv.AlgorithmName;
    var buffHash = objAlgProv.HashData(buffUtf8Msg);
    if (buffHash.Length != objAlgProv.HashLength)
    {
        throw new Exception("There was an error creating the hash");
    }
    return buffHash;
}

如何加密和解密的例子可以在这里找到:

https://msdn.microsoft.com/en-us/library/windows/apps/windows.security.cryptography.core.cryptographicengine.aspx