Encrypt.cs
BitmapImage bitmapImage = new BitmapImage(new Uri(this.BaseUri,@"D:\Others\Quotes\1.jpg"));
var plainString = bitmapImage;
string key = txtkey.Text; // Key
string encryptedString = await EncryptStringHelper(plainString.ToString(), key); // Encrypt method and get string
txbencrypt.Text = encryptedString;
Decrypt.cs
string encryptedString = txbencrypt.Text; // Encrypt text
string key = txtkey.Text; // Same key
string decryptedString = await DecryptStringHelper(encryptedString, key);
imgoutput.Source = decryptedString;
private Task<string> EncryptStringHelper(string plainString, string key)
{
try
{
var hashKey = GetMD5Hash(key);
var decryptBuffer = CryptographicBuffer.ConvertStringToBinary(plainString, BinaryStringEncoding.Utf8);
var AES = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
var symmetricKey = AES.CreateSymmetricKey((IBuffer)hashKey);
var encryptedBuffer = CryptographicEngine.Encrypt(symmetricKey, decryptBuffer, null);
var encryptedString = CryptographicBuffer.EncodeToBase64String(encryptedBuffer);
return Task.Run(() =>
{
return encryptedString;
});
}
catch (Exception ex)
{
return null;
}
}
public Task<string> DecryptStringHelper(string encryptedString, string key)
{
try
{
var hashKey = GetMD5Hash(key);
IBuffer decryptBuffer = CryptographicBuffer.DecodeFromBase64String(encryptedString);
var AES = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
var symmetricKey = AES.CreateSymmetricKey((IBuffer)hashKey);
var decryptedBuffer = CryptographicEngine.Decrypt(symmetricKey, decryptBuffer, null);
string decryptedString = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, decryptedBuffer);
return Task.Run(() =>
{
return decryptedString;
});
}
catch (Exception ex)
{
return null;
}
}
我开发通用Windows应用程序(UWP)然后尝试加密和解密到图像文件,但我转换图像加密文本然后当我将该文本解密为图像时我无法转换。那我该怎么做?
答案 0 :(得分:1)
您希望将base 64字符串转换为字节数组,然后从中创建ImageSource。
byte[] data = Convert.FromBase64String(base64string);
if (data.caseImage.Count() > 1)
{
MemoryStream ms = new MemoryStream(data.caseImage, 0, data.caseImage.Length);
BitmapImage img = new BitmapImage();
var ras = ms.AsRandomAccessStream();
await img.SetSourceAsync(ras);
imgCase.Source = img;
}
imgCase是Xaml图像。
就最初创建base64string而言,你想要做这样的事情:
将BitMapImage转换为ImageSource:
BitmapImage bitmapCamera = new BitmapImage();
bitmapCamera.SetSource(streamCamera);
// Convert the camera bitap to a WriteableBitmap object,
// which is often a more useful format.
int width = bitmapCamera.PixelWidth;
int height = bitmapCamera.PixelHeight;
WriteableBitmap wBitmap = new WriteableBitmap(width, height);
using (var stream = await capturedMedia.OpenAsync(FileAccessMode.Read))
{
wBitmap.SetSource(stream);
}
imgPreview.Source = wBitmap;
或者StorageFile到Base64String:
byte[] fileBytes = null;
var imageFile = *Your storageFile*;
mimetype = imageFile.ContentType;
filetype = imageFile.FileType;
using (IRandomAccessStreamWithContentType stream = await imageFile.OpenReadAsync())
{
fileBytes = new byte[stream.Size];
using (DataReader reader = new DataReader(stream))
{
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(fileBytes);
}
}
string base64 = Convert.ToBase64String(fileBytes);
希望这有帮助。