2016-09-06 1 views
1

Encrypt.cs는UWP에서 문자열을 image.source로 변환하는 방법은 무엇입니까?

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

문자열이'base64' 문자열입니까? –

+0

예, 예 : (FXr1sQhcoFsg3fRqyTovZrUw4g0nWrJtPAuk9iQgqXjWw33IXYUyEINuJViIDUGl) 이것은 암호화 된 문자열입니다. 그러나 "windows.ui.xaml.media.imaging.bitmapimage"에 표시되는 문자열 decryptedstring에 중단 점을 넣을 때 반환하는 텍스트의 암호를 해독합니다. 이미지를 변환하십시오. – Azarudeen

+0

내가 이해 한대로'decryptedString' - base64string, 그리고'decryptedString'에서 이미지를 만들고 싶습니다. –

답변

1

기본 64 문자열을 바이트 배열로 변환 한 다음 이미지 원본을 만들어야합니다.

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 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 : BitMapImage 변환

ImageSource에 : 측면에서

처음 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); 

희망이 도움이됩니다.

관련 문제