. Net EncryptionAlgorithmThe returned data is of the byte [] type, which makes it very troublesome to store. The simplest way is to convert byte [] to a string for storage. Of course, if the data volume is large, it should be different.
So I will convert byte [] into a string for a simple accumulation and analysis. Currently, there are three simple solutions for conversion.
1. Convert. tobase64string and convert. frombase64string
This method is simple and practical. The converted string contains: 26 English letters in upper and lower case, +,/, and 0 ~ 9, a total of 64 characters. "=" Usually appears at the end ". The reason is simple.
Principle: Describes 6-bit binary data with 64 characters.
Method: concatenate byte data and group the data in 6 bits. Use 64 characters to describe the data in 6 bits. If the group is less than 6 bits, use "=" to complete the data.
2. Write by yourselfCode, With hexadecimal characters 0 ~ 9. ~ F to describe the data.
Principle: use 16 characters to describe the 4bit binary. That is to say, a byte must be described using two hexadecimal characters.
Method: use characters to describe the first half and the second half of each byte.
This method requires manual writing, and the code is quite simple. You can use crtl + C Ctrl + v.
Public Static String Bytetostring (Byte [] Bytes)
{
Stringbuilder strbuilder = New Stringbuilder ();
Foreach ( Byte BT In Bytes)
{
Strbuilder. appendformat ( "{0: X2 }" , BT );
}
Return Strbuilder. tostring ();
}
Public Static Byte [] Stringtobyte (String Str)
{
Byte [] Bytes = New Byte [Str. Length/2];
For ( Int I = 0; I <Str. Length/2; I ++)
{
Int Btvalue = convert. toint32 (Str. substring (I * 2, 2), 16 );
Bytes [I] = ( Byte ) Btvalue;
}
Return Bytes;
}
3. Encoding encoding is used to automatically convert encoding. getencoding ("encoding format"). getstring () and encoding. getencoding ("encoding format"). getbytes ()
Principle: force conversion using editing rules.
Mode: Use the encoding method to edit the data. The converted byte data or string may contain various strange or invisible characters, which is inconvenient to store.
Summary:
We recommend that you use the first method to convert strings. However, if the format is required, such as in a custom machine language, "+" "/" is not allowed, the second method is considered.
If you convert data to byte or string for memory operations, the third method is a good choice.