我制作了一些代码,允许我在c#中进行加密 - 主要使用System.Security.Cryptography中的AesManaged()
和SHA256Managed()
。
用例是该工具需要能够拉出加密的数据,解密,显示给用户,允许再次编辑和加密,然后再将其发回。
我希望能够在Windows Phone上做类似的事情,但似乎手机上没有命名空间。
那么我现在的选择是什么?它是否可以在Windows Phone 10上使用?看起来在手机应用程序中执行加密操作会是一项相对常见的任务吗?
编辑:添加了有关应用应该执行的操作的信息
答案 0 :(得分:1)
你想用密码学做什么?
因为如果您只需要存储一些用户凭据,最好的方法是使用PasswordVault
我在这里举了一个例子 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);
}
答案 1 :(得分:0)
正如@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;
}
如何进行加密和解密的示例可以在这里找到: