AES Symmetric - All platforms

ManhaoChenManhaoChen NZMember ✭✭

hi all,

anyknow how how to write the following for droid ?

` public class AES_Symmetric
{
public static string Encrypt(string value, string password)
{
var iv = CreateInitializationVector(password);
var key = CreateKey(password);
var data = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);
var encryptedBuffer = CryptographicEngine.Encrypt(key, data, iv);
return CryptographicBuffer.EncodeToBase64String(encryptedBuffer);
}

public static string Decrypt(string value, string password)
{
  var iv = CreateInitializationVector(password);
  var key = CreateKey(password);
  var data = CryptographicBuffer.DecodeFromBase64String(value);
  var decryptedBuffer = CryptographicEngine.Decrypt(key, data, iv);
  return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, decryptedBuffer);
}

private static IBuffer CreateInitializationVector(string password)
{
  var provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
  var newPassword = password;
  // make sure we satify minimum length requirements 
  while (newPassword.Length < provider.BlockLength)
  {
    newPassword = newPassword + password;
  }
  //create vecotr 
  var iv = CryptographicBuffer.CreateFromByteArray(Encoding.UTF8.GetBytes(newPassword));
  return iv;
}

private static CryptographicKey CreateKey(string password)
{
  var provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
  var newPassword = password;
  // make sure we satify minimum length requirements 
  while (newPassword.Length < provider.BlockLength)
  {
    newPassword = newPassword + password;
  }
  var buffer = CryptographicBuffer.ConvertStringToBinary(newPassword, BinaryStringEncoding.Utf8);
  buffer.Length = provider.BlockLength;
  var key = provider.CreateSymmetricKey(buffer);
  return key;
}

}`

Tagged:
Sign In or Register to comment.