Original document encryptionAlgorithmDescription:
1.1 Message Security
DesEncryption ensures message readability.
Message encryption algorithm:Base64 (des (MD5 (Message Body) +Message Body ))
Message usedDesThe encryption algorithm rules are as follows:
1. The key is a length16,16A string consisting of hexadecimal characters, such:1234567890 abcdef
When used, the two adjacent operators are considered as one16The plaintext of the number, and then convert it to the actual8Bit key
2.To encrypt data, followPkcs5Rules. (Missing7Bit Complement7Items0x07, Missing6Bit is supplemented6Items0x06, And so on.8Bit.8Items0x08)
3.Select the actual encryption modeDES-ECB
4.AfterDesEncrypted data must passBase64Encode a string converted to plain text
Huawei prompts the Java jar package,. net is no way, there are a lot of online queries, there are a lot of des methods, the encryption results are not the same, often carefully read the encryption algorithm, finally achieved in, provide for everyone
Code
Using System;
Using System. text;
Using System. componentmodel;
Using System. collections;
Using System. Security;
Using System. Security. cryptography;
Using System. diagnostics;
Using System. IO;
Namespace Adclib. util
{
/// <Summary>
/// Class1.
/// </Summary>
Public Class Securityutil // Encryption and decryption
{
/// <Summary>
/// 3DES encrypted string
/// </Summary>
/// <Param name = "a_strstring"> String to be encrypted </Param>
/// <Param name = "a_strkey"> Key </Param>
/// <Returns> Base64-encoded string after Encryption </Returns>
/// <Remarks> Static Method, uses the default ASCII Encoding </Remarks>
Public Static String Encryptdes ( String A_strstring, String A_strkey)
{
Descryptoserviceprovider des = New Descryptoserviceprovider ();
// Tripledescryptoserviceprovider des = new tripledescryptoserviceprovider ();
// Md5cryptoserviceprovider hashmd5 = new md5cryptoserviceprovider ();
Byte [] B = Strtohexbyte (a_strkey );
Des. Key = B;
// Des. IV = B;
// Des. Key = B; // Asciiencoding. ASCII. getbytes (a_strkey ); // Hashmd5.computehash (asciiencoding. ASCII. getbytes (a_strkey ));
Des. Mode = Ciphermode. ECB;
Des. padding = Paddingmode. pkcs7;
icryptotransform desencrypt = des. createencryptor ();
Byte[] Buffer=Asciiencoding. utf8.getbytes (a_strstring );
ReturnConvert. tobase64string (desencrypt. transformfinalblock (buffer,0, Buffer. Length ));
}//End Method
Private Static Byte [] Strtohexbyte ( String Hexstring)
{
Byte [] Returnbytes = New Byte [Hexstring. Length / 2 ];
For ( Int I = 0 ; I < Returnbytes. length; I ++ )
Returnbytes [I] = Convert. tobyte (hexstring. substring (I * 2 , 2 ), 16 );
Return Returnbytes;
}
/// <Summary>
/// 3DES decryption string
/// </Summary>
/// <Param name = "a_strstring"> String to be decrypted </Param>
/// <Param name = "a_strkey"> Key </Param>
/// <Returns> Decrypted string </Returns>
/// <Exception CREF = ""> Key Error </Exception>
/// <Remarks> Static Method, uses the default ASCII Encoding </Remarks>
Public Static String Decryptdes ( String A_strstring, String A_strkey)
{
Descryptoserviceprovider des = New Descryptoserviceprovider ();
Des. Key=Strtohexbyte (a_strkey );
Des. Mode=Ciphermode. ECB;
Des. padding=Paddingmode. pkcs7;
Icryptotransform desdecrypt=Des. createdecryptor ();
String Result = " " ;
Try
{
Byte [] Buffer = Convert. frombase64string (a_strstring );
Result = Asciiencoding. utf8.getstring (desdecrypt. transformfinalblock (buffer, 0 , Buffer. Length ));
}
Catch (Exception E)
{
Throw(NewException ("Invalid key or input string is not a valid base64 string", E ));
}
Return Result;
} // End Method
/// <Summary>
/// MD5
/// </Summary>
/// <Param name = "data"> </param>
/// <Returns> </returns>
Public Static String MD5 ( String Data)
{
System. Security. cryptography. md5cryptoserviceprovider MD5 = New System. Security. cryptography. md5cryptoserviceprovider ();
Return Bitconverter. tostring (md5.computehash (encoding. utf8.getbytes (data). Replace ( " - " , "" ). Tolower ();
}
}
}