UWP img in notification from base64 string - c#

when I create a notification in uwp app, and I try set the image, it does work when I do something like:
((XmlElement)imageAttribute[0]).SetAttribute("src", "ms-appx:///Assets/Test.png");
This works fine. But what I need is to set the image from base64 string and not from the Assets folder. Does anyone have any solutions?

You cannot read a string and have it work as binary data. You need to first read the base64 string and convert it back to binary which would be usually in a type of byte array or something.
after you read the base64 string and convert it back to the binary data, then you can use that instead of the binary file in your attribute instead of referencing a resource.
There are multiple sources out there for converting base64 to binary data and/or files so an internet search should yield the results you are looking for... without knowing anything about the language you are writing in, it is impossible to give examples here but the method is the same.

Related

How to Save a Binary Representation to file

I have the following textual binary representation: "0x255044462D312E340D0A25FFFFFFF..."
I know it's a pdf.
I know it's the textual represantation from a sql server column (image data type).
But im lost to find out how to save this binary to a pdf file on my disk and view the content.
Maybe someone can hint me in the right direction.
Best Regards and Thanks in Advance
You're correct that it is a PDF file (at least it masquerades like on. You have hexadecimally encoded bytes; the first read:
255044462D312E340D0A
%PDF-1.4<CR><LF>
So you appear to have a PDF 1.4 string.
Just take two characters from the string, treat them as hex, convert them to the correct byte and write them to a file. Write binary, not textually (you don't want to add additional line-breaks in there, PDF is too binary to let that work.
(I did the conversion using this site: http://www.dolcevie.com/js/converter.html)
I'm not sure what database you are working with or how you are getting your string that you have above.
Many databases allow you to save binary data as a blob or some other byte array type. I believe in MSSQL this is called an "image" but I am not 100% on that. I would start by looking into the two following links in order. The first link talks about how to pull byte array data from a database. The example is in Visual Basic but should be easily changed to C# if that is what you are using.
The second link contains an example of how to save that byte array data to the file system.
I would also suggest posting some of the code you have tried as well so that the community may comment and point out areas you possibly had misunderstandings on.
1.) http://support.microsoft.com/kb/308042
2.) Save and load MemoryStream to/from a file
http://www.pdfsharp.com/PDFsharp/ can read in binary data and you can call .Save() and it will make the PDF file to disk for you.

Read and Encryption of a PDF file in c#

I want to read a the content of a PDF file and encrypt the content using AES256 encryption and post the content(encrypted) as a base64 string.
for that i have 2 solution
read the content using a stream reader(PDF formated data) the encrypt the content and the base64 encoding, Finally send the encrypted string
Read PDF content and convert it into text then encrpt and then send
Which is the best method, If i use first method then there will be any problem for failure
I need your opinion Please help me
Your first method seems absolutely fine and I would certainly go for that approach. What you are essentially doing is simple transmitting a file from one machine to another.
If you consider this without encryption all you should be aiming to do would be to send the file stream exactly the same as you read it, this ensures the receiver gets the file in it's original state and can reliable open the file as it will be in the exact same format as it started.
Now when we consider adding encryption, all we are doing is change the raw binary data of the file. As long as we decrypt the file at the other end using the same key parameters we can be sure that we will still have the same original raw file data we started with (assuming we don't get any data loss during connection - you could add a hash check for this if desired, for example)

VB6 to C# XML String Conversion Special Characters

I've been using the DOMDocument object from VB6 (MSXML) to create and save an XML file that has an encrypted string. However, this string I think has certain special characters...
<EncryptedPassword>ÆÔ¤ïÎ
߯8KHÖN›¢)Þ,qiãÔÙ</EncryptedPassword>
With this, I go into my C# Project, and de-serialise this XML file in UTF-8 encoding and it fails on this string. I've tried serialisation via ASCII and this gets a couple characters more, but still fails. If I put a plain text string in this place, all is ok! :(
I'm thinking that maybe I'm better converting the string into an MD5 type string from VB6 first, and decoding the MD5 string in .NET and then decrypting the actual string with special characters, but it's an extra step to code all this up and was hoping someone might have a better idea for me here?
Thanks in advance!
The best thing for you to do is to encode your encrypted string in something that will use the ASCII charset. The easiest way to do this is to take your encrypted string and then encode it into Base64 and write this encoded value to the XML element.
And in .net, simply take the value of the XML element and decode it from Base64 and 'voila', you have your enrypted string.
.Net can easily decode a base64 string, see: http://msdn.microsoft.com/en-us/library/system.text.encoding.ascii.aspx. (This page may make it look a bit complicated than it really is).
VB6 does not have native support for Base64 encoding but a quick trawl on google throws up some examples on how it can be achieved quite easily:
http://www.vbforums.com/showthread.php?t=379072
http://www.nonhostile.com/howto-encode-decode-base64-vb6.asp
http://www.mcmillan.org.nz/Programming/post/Base64-Class.aspx
http://www.freevbcode.com/ShowCode.asp?ID=2038
I've concluded that storing these characters in the XML file is wrong. VB6 allows this, but .NET doesn't! Therefore I have converted the string to a Base64 array in line with this link: -
http://www.nonhostile.com/howto-encode-decode-base64-vb6.asp
Now, on the .NET side, the file will de-serialise back into my class where I now store the password as a byte array. I then convert this back to the string I need to decrypt which now appears to raise another problem!
string password = Encoding.UTF7.GetString(this.EncryptedPassword);
With this encoding conversion, I get the string almost exactly back to how I want, but there is a small greater than character that is just not translating properly! Then a colleague found a stack overflow post that had the final answer! There's a discrepancy between VB6 and .NET on this type of conversion. Doing the following instead did the trick: -
string password = Encoding.GetEncoding(1252).GetString(this.EncryptedPassword);
Thanks for all the help, much appreciated. The original post about this is # .Net unicode problem, vb6 legacy

C# Encryption Android Decryption

I encrypt a string in c# (RijndaelManaged), which returns a byte array.
I want to save that byte array to a database in order to get it decrypted later in the android device.
I have tried converting it to base64 in c#, But as I understand android doesnt support base64 decoding before API8.
Is there another way saving the byte array in the DB, so I can later fetch it and decrypt it on android?
10X
You can find Java Base64 decoders everywhere http://www.source-code.biz/base64coder/java/ , add it to your Android project and you're off.
If not, just translate each byte into two hexadecimal digits in C#, and then back in Android. It's very easy to do so yourself.
Update :
you can implement the base64 encoding yourself on both sides so that you can be sure, you can see example code of how to implement Base64 encoding and decoding in languages like c, C++, java, and javascript on wikibooks
Yes, if you are using SQL Server, you can use VarBinary
you can read more about insert a byte array into database in this SO question
You can insert BLOB Data in SQLITE and MySql too

Convert BLOB to Jpg using C#

I am currently developing a web application that receives data from an on-site database. The database developers have developed some web-services that I am able to call and send/receive data to and from the the database.
When I want to display an image the method returns the image in BLOB format. My question is: what is the best way to convert BLOB to .jpg or .bmp so I can display the image correctly? If someone could point me in the right direction that would be great!
Cheers,
Tristan
You can create a generic handler (ashx file) and have it return the byte array.
For example, the url http://www.example.com/GetImage.ashx?imageId=1 could contain code to write the bytes received from the image with id of 1.
You will need to implement the handler code yourself to do the database query, etc. and write the bytes out to the response. You will also need to set the response mime type to jpg, png or whatever format your images are in.
Check out this stackoverflow post for more details: How to bind a MemoryStream to asp:image control?
byte[] array and mimetype are the key words :)
There is no one image format associated with BLOB. It's simply a way of putting raw binary in a database. You need to figure out which format it actually is (libmagic may help), then you can convert if needed.

Categories