Reputation: 2013
I have made some code that allows me to do cryptography in c# - using primarily AesManaged()
and SHA256Managed()
in System.Security.Cryptography.
The use case is that the tool needs to be able to pull off an encrypted piece of data, decrypt it, display it to the user, allow for editing and encrypt again before sending it back again.
I would like to be able to do similar on Windows Phone, but it seems that the namespace is not available on the phone.
So what are my options now? Will it be available on Windows Phone 10? It seems that doing crypto-stuff would be a relatively common task in a phone app?
Edit: added information about what the app should do
Upvotes: 2
Views: 69
Reputation: 2013
As @WDS noted, the tools for doing cryptography is located in Windows.Security.Cryptography namespace - available on #WP8 .
So I rewrote my hash-implementation like this:
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;
}
Examples of how to do encryption and decryption can be found here:
Upvotes: 0
Reputation: 6142
What are you trying to do with the cryptography?
Because if you just need to store some user credentials, best way is using the PasswordVault
MSDN reference here https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.security.credentials.passwordvault.aspx
I've made an example of this here http://depblog.weblogs.us/2014/11/20/migrating-from-sl8-0-protectdata-to-rt8-1-passwordvault/
Added some example code on how to add and remove entries from the Vault ( more details on the blog post )
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);
}
Upvotes: 1