C# Encryption Android Decryption - c#

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

Related

Change encoding from Default to Unicode for encryption

I have a project where everything that is stored in database is encrypted. For encoding we use System.Text.Encoding.Default.GetBytes(text).
The problem is that now the client wants to add support for polish (and other nordic) characters and using the Default encoding doesn't work, the polish characters get converted to english characters (e.g Ą gets converted to A).
I can't change the encoding (Unicode seems to work) as the previous data will be lost.
Is there any way to get around this and add support for new characters while keeping the old data?
To be clear; you realise that "encoding" is not "encrypting", but I suppose you encrypt the byte array you get from encoding your string data?
Then I'd suggest either decrypting and re-encoding and re-encrypting all existing data using UTF-8 (the most efficient encoding for Western alphabets), or add a "version" or "encoding" column indicating with which encoding the data was encrypted.

UWP img in notification from base64 string

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.

How to encode RGB images (bitmaps) into VP8 format?

Anyone know how I could encode RGB images (24bppRgb) into VP8 format using C#? Basically I'm capturing the screen as bitmap frames and I want to encode them to reduce the size before sending.
From what I've seen there are only code examples for C++ not C#.
Suppose I'm asking for a C# alternative to this question.
You could either port the code (a bit job I'm sure) or build an application in C++ which does the actual conversion, and then call that application via command line in C#, storing the result in a txt or a log file of some sort.

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.

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

Categories