1

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;
        }
    }

I develop Universal windows application(UWP) then tried encrypt and decrypt to image file, but i convert image to encrypt text then i couldn't convert when i decrypt that text into image. So how do i do that?

8
  • Are your string is base64 string? Commented Sep 6, 2016 at 7:10
  • Yes, ex:(FXr1sQhcoFsg3fRqyTovZrUw4g0nWrJtPAuk9iQgqXjWw33IXYUyEINuJViIDUGl) this is encrypt string, but i decrypt the text it's return when i put the breakpoint in the string decryptedstring variable it's show on "windows.ui.xaml.media.imaging.bitmapimage" so how do i convert the image. Commented Sep 6, 2016 at 7:25
  • As I understood, decryptedString - base64string, and you want to create image from decryptedString Commented Sep 6, 2016 at 7:29
  • Yes, I want like do that... Commented Sep 6, 2016 at 7:32
  • why do you want to store BitmapImage in string? Even if you have a good reason then plainString.ToString() won't work becuase it will return you "Windows.UI.Xaml.Media.Imaging.BitmapImage" not the BitmapImage converted to string Commented Sep 6, 2016 at 8:42

1 Answer 1

1

You would want to convert your base 64 string to a byte array, and then create the ImageSource from that.

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;
        }

with imgCase being a Xaml image.

in terms of initially creating the base64string you would want to do something like this:

Converting BitMapImage to 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;

And or a StorageFile to 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);

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.