RSACryptoServiceProvider helper [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was looking for RSACryptoServiceProvider helper and found two different implementations
1) http://www.cnblogs.com/WYB/archive/2008/06/19/1225704.html
2) https://github.com/robvolk/Helpers.Net/blob/master/Src/Helpers.Net/EncryptionExtensions.cs
both of them working
var encryptedBytes = myBytes.RSAEncrypt(publicKey);
System.Text.Encoding.Unicode.GetString(encryptedBytes);
returns strings like "蹩巷Ӂය馧㾵봽놶徤蕺蓷課Ϝ堲泍썳⁙䃑ക늏...."
myString.EncryptStringUsingXMLFile(publicKey)
returns strings like "AnvFFT6YpoiAyIFwl+tueZq56Zcb0B7WhBEvz5uWl...."
May be some one can explain why first one producing Chinese strings and how to change that?
What approach is better?

To answer your first question. While it may look like it is producing Chinese characters what is actually happening is it is turning a byte array into unicode. In c# typically when you want to store a byte array you convert it to base64 which is what your second example appears to return.
Your first example would become this:
var encryptedBytes = myBytes.RSAEncrypt(publicKey);
Convert.ToBase64String(encryptedBytes) // this line changed
returns strings like "AnvFFT6YKpoiAy...."
As for what is recommended, the most common is to use base64. The reasons people use base64 over unicode or UTF-8 for binary data can be found in these answers:
https://stackoverflow.com/a/201491/701062
MSDN - Convert.ToBase64String(byte[])
http://msdn.microsoft.com/en-us/library/dhx0d524(v=vs.100).aspx
MSDN - Convert.FromBase64String(string) - Useful if you need to convert back into a byte array
http://msdn.microsoft.com/en-us/library/system.convert.frombase64string(v=vs.100).aspx

Related

reading lines with variable numbers of fields from a file in c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How do you read the lines one by one if their fields are uneven and you need to know when it ends.
For example:
A;B;C;D
E;F;G;H;J
'A' is a person and 'B' , 'C' and 'D' are his friends.It goes the same for the second line I wrote.I know I could just write it with an even number of fields but I think this is a neater way to do it.
Thank you.
There are two functions that make this really easy: StreamReader.ReadLine and String.Split.
You use StreamReader.ReadLine to get the entire line of text:
string lineOfInput = reader.ReadLine();
Then, you can split on the semicolons to get all your "fields":
string[] fields = lineOfInput.Split(';');
fields[0] will contain the "person" and the rest, his "friends".
See StreamReader and Split on MSDN.
CSV files are deceptively simple. See this question, Simple csv reader?, for some details.
But I'd just use Sebastien Lorion's Fast CSV Reader from CodeProject:
http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader
All you've got to do is set the correct field separator and you're good to go. It's fast, it works well. It implements IDataReader, so it acts like a normal .Net data reader implementation.

How to validate a Text in Asp.net [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to put check to validate textbox that input value must not be similar to the values already present in database. Like:
if there is value with text "Hello" in database then user must not be allowed to save value either he writes:
Hello
HELLO
hElLo
HeLLO
Hello etc
I followed this http://www.dotnetperls.com/string-isupper-islower but as i am new to c# so have little confuse that how to match above defined words as all are same words Hello
I typically just convert both values (user input and stored value) to lower case when making the comparison.
Edit: if both values are in .NET, you could use String.Compare(s1, s2, StringComparison.OrdinalIgnoreCase)
Do you need to do this in code? I would suggest that you just make a unique constraint on the column and let the database handle that for you. Depending on the database you are using you may need to do a little additional work to handle the case sensitivity.

how to use StreamReader in C# to read only entries over a specific length to a list? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
when using StreamReader in C# to load a txt file into a list, i assume that using a simple "If" the string's length is over a particular length, it will add it to the list. can anyone provide C# code for this? this IS homework, but it's NOT a C# class. the instructor would gladly provide this if i asked this specifically. thx.
the txt file is a dictionary of ~280,000 words, one per line. very simple move to turn into a list, but i'm wondering about getting words at least 2 characters long.
Just use LINQ to give you a subset.
List<string> lines = File.ReadLines(filename)
.Where(l => l.Length > specifiedWordLength)
.ToList();

Encryption and Decryption of string with out using Base64String [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
How to encrypt Decrypt text without using Base64String?
I don't want to use Base64String because encrypted text should not contains any special character like #, $, #, /, \, |,=,% ,^
Well the obvious approach if you don't want to use base64 is to use base16 - i.e. hex.
There are plenty of examples of converting between a byte array and a hex string representation on Stack Overflow. (BitConverter.ToString(data).Replace("-", "") is an inefficient way of performing the conversion to a string; there's nothing quite as simple for the reverse, but it's not much code.)
EDIT: As noted in comments, SoapHexBinary has a simple way of doing this. You may wish to wrap the use of that class in a less SOAP-specific type, of course :)
Of course that will use rather more space than base64. One alternative is to use base64, but using a different set of characters: find 65 characters you can use (the 65th is for padding) and encode it that way. (You may find there's a base64 library available which allows you to specify the characters to use, but if not it's pretty easy to write.)
Do not try to just use a normal Encoding - it's not appropriate for data which isn't fundamentally text.
EDIT: As noted in comments, you can use base32 as well. That can be case-insensitive (potentially handy) and you can avoid I/1 and O/0 for added clarity. It's harder to code and debug though.
There's a great example in the MySQL Connector source code for the ASP.NET membership provider implementation. It may be a little hassle to download and research, but it has a well-established encryption and decryption module in there.
http://dev.mysql.com/downloads/connector/net/#downloads
Choose the 'source code' option before downloading.
If you want encoding/decoding for data transmission or condensed character storage, you should edit your question. Answers given to an encoding question will be much different than answers given to an encryption/decryption question.

how to convert or display emf image in c#? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
this is a part of the documentation for a device I'm working on.
any idea how to interpret this to an image or bitmap or anything usable?
this definition below is xml definitions
3.2.13. IMAGE
properties: content complex
children: IMAGE_DATA
used by: complexType Cbc_Result
attributes: Name Type Use Default Fixed Annotation
ImageType Type_Image required
DataSize xs:long required
This node gathers information on an images files calculated by the software(Image_Data represents entire content of an emf file representing an image)
sample data :
< IMAGE DataSize="6676" ImageType="3">< IMAGE_DATA>AQAAAGwAAAAAAAAAAAAAAMcAAABjAAAAA
AAAAAAAAABNEgAANQwAACBFTUYAAAEAFBoAAI0AAAAF
AAAAAAAAAAAAAAAAAAAAVgUAAAADAABAAQAA8AAAAAAAAAAAAAAAAAAAAADiBACAqQMAEQAAAAwA
AAAIAAAAEgAAAAwAAAACAAAAFAAAAAwAAAANAAAACQAAABAAAADIMgAAjBwAAAkAAAAQAAAAyDIA
AIwcAAALAAAAEAAAAMgAAABkAAAASwAAAEAAAAAwAAAABQAAACAAAAABAAAAAQAAABAAAAAAAAAA
AAAAADB1AAAwdQAAAA< /IMAGE_DATA>< /IMAGE>
Looks like Base64 encoded (with stripped == signes at the end) content of file in EMF format - http://en.wikipedia.org/wiki/Windows_Metafile.
Make sure to add needed number of = at the end (http://en.wikipedia.org/wiki/Base64 for details) to successfully use Conver.FromBase64 function to get byte array.
I don't know if regular rendering methods support EMF... But Check Drawing namespace and Image class in particular.

Categories