Click here to Skip to main content
15,919,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How can I encrypt a 32 bit character using C#

Thanks in advance

The OP posted as a comment:

I mean char datatype.In english it is 1 byte.I think in some other languages such as chinese it may be 2 byte or max 4 byte.Is it correct or not.I need to encrypt password which is in chinese(ie 2 byte or max 4 byte.not sure).So I used the code as shown below
crypto.Key = ASCIIEncoding.Default.GetBytes("UDNAHNNU");
                crypto.IV = ASCIIEncoding.Default.GetBytes("UDNAHNNU");
                byte[] inputByte = ASCIIEncoding.Default.GetBytes("オープンコンテント");
                ICryptoTransform Encryptor = crypto.CreateEncryptor();
                byte[] outputByte = Encryptor.TransformFinalBlock(inputByte, 0, inputByte.Length);
                strout = Convert.ToBase64String(outputByte);



It successfully gave the encrypted string.After that I tried to decrypt the encrypted value using the below code

byte[] inpt = Convert.FromBase64String(richTextBox2.Text);
            crypto.Key = ASCIIEncoding.Default.GetBytes("UDNAHNNU");
            ICryptoTransform Decryptor = crypto.CreateDecryptor();
            byte[] oupt = Decryptor.TransformFinalBlock(inpt, 0, inpt.Length);
            richTextBox1.Text = ASCIIEncoding.Default.GetString(oupt);


But it returns "??????" like this.Please help me.

[edit]OP additional info added - OriginalGriff[/edit]
Posted
Updated 18-Jul-11 21:37pm
v2
Comments
Sergey Alexandrovich Kryukov 19-Jul-11 2:05am    
Not clear what you want to do.
--SA

There are a few problems with what you are asking:
1) What is a "32 bit character"? The char datatype is 16 bit, ASCII characters are 7 or 8 bit. UTF-32 is possible, but incredibly rare in practice.
2) Why are you trying to encrypt a single 32 bit value? What do you want to do with it? Since there is a lack of input data, any encryption is unlikely to be strong.
3) Are you talking about Encoding characters, rather than Encrypting?

OP posted further info.


Ah! Don't encrypt it at all. Hash it.
There is a Tip/Trick giving the basics here: Password Storage: How to do it.[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 19-Jul-11 2:37am    
This is more or less clear. 32-bit character is incorrect, but characters beyond BOM cannot be expressed in 16 or less bit. Unicode is not a 16-bit encoding -- it is not encoding at all. The problem is the difference between character (it does not have bits, and abstract concept) and presentation of a characters in UTF.

I covered both encoding and encryption, please see my solution.
--SA
kutz 2 19-Jul-11 3:33am    
I mean char datatype.In english it is 1 byte.I think in some other languages such as chinese it may be 2 byte or max 4 byte.Is it correct or not.I need to encrypt password which is in chinese(ie 2 byte or max 4 byte.not sure).So I used the code as shown below

crypto.Key = ASCIIEncoding.Default.GetBytes("UDNAHNNU");
crypto.IV = ASCIIEncoding.Default.GetBytes("UDNAHNNU");
byte[] inputByte = ASCIIEncoding.Default.GetBytes("オープンコンテント");
ICryptoTransform Encryptor = crypto.CreateEncryptor();
byte[] outputByte = Encryptor.TransformFinalBlock(inputByte, 0, inputByte.Length);
strout = Convert.ToBase64String(outputByte);





It successfully gave the encrypted string.After that I tried to decrypt the encrypted value using the below code


byte[] inpt = Convert.FromBase64String(richTextBox2.Text);
crypto.Key = ASCIIEncoding.Default.GetBytes("UDNAHNNU");
ICryptoTransform Decryptor = crypto.CreateDecryptor();
byte[] oupt = Decryptor.TransformFinalBlock(inpt, 0, inpt.Length);
richTextBox1.Text = ASCIIEncoding.Default.GetString(oupt);



But it returns "??????" like this.Please help me.
OriginalGriff 19-Jul-11 3:39am    
Answer updated
First of all: strictly speaking, a character is not 8, 16 or 32-bit. A character is characterized by its Unicode code point, which is an integer understood in its abstract mathematical sense, regardless of its binary presentation in a computer, endianess, etc. The machine presentation is defined by Unicode UTFs (Unicode Transformation Format).

With .NET, UTF-16 encoding is used. The characters beyond BOM (Base Multilingual Plain, code points 0 to 0xFFFF) are represented by surrogate pairs, two 16-bit words.

See:
http://unicode.org/[^],
http://unicode.org/faq/utf_bom.html[^].

Now, encryption is performed in a way totally agnostic to the structure of data. All the data is presented as array of bytes. So, the first step — obtaining array of bytes, is performed by System.Text.Encoding.GetBytes. It's important, that after decryption the same encoding was used to call GetChars method. See http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[^].

For any Unicode strings, four encodings are good, no matter if characters fit in BMP or go outside of it: System.Text.UnicodeEncoding (UTF-16, in fact — ugly Windows jargon), System.Text.UTF32Encoding, System.Text.UTF7Encoding (pretty much outdated) and System.Text.UTF8Encoding (most used for stream, interop, Web, etc.)

When a topic of serialization of Unicode strings is clear, we're ready for encryption.

For encryption topics, see System.Security.Cryptography namespace: http://msdn.microsoft.com/en-us/library/system.security.cryptography.aspx[^].

—SA
 
Share this answer
 
v3
Comments
Mohammad A Rahman 19-Jul-11 2:22am    
My 5
clear explanation.
Sergey Alexandrovich Kryukov 19-Jul-11 2:34am    
Thank you, Mohammad.
--SA
CPallini 19-Jul-11 3:04am    
My 5.
Sergey Alexandrovich Kryukov 19-Jul-11 3:29am    
Thank you.
--SA
Espen Harlinn 19-Jul-11 3:38am    
Nice reply, my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900