// Default key vector
Private Static Byte [] Keys = { 0x12 , 0x34 , 0x56 , 0x78 , 0x90 , 0xab , 0xcd , 0xef };
/// <Summary>
/// DES encrypted string
/// </Summary>
/// <Param name = "encryptstring"> String to be encrypted </Param>
/// <Param name = "encryptkey"> Encryption key, which must be 8 bits </Param>
/// <Returns> The encrypted string is returned successfully. If the encrypted string fails, the source string is returned. </Returns>
Public Static String Encryptdes ( String Encryptstring, String Encryptkey)
{
Try
{
Byte [] Rgbkey = Encoding. utf8.getbytes (encryptkey. substring ( 0 , 8 ));
Byte [] Rgbiv = Keys;
Byte [] Inputbytearray = Encoding. utf8.getbytes (encryptstring );
Descryptoserviceprovider dcsp = New Descryptoserviceprovider ();
Memorystream mstream = New Memorystream ();
Cryptostream cstream = New Cryptostream (mstream, dcsp. createencryptor (rgbkey, rgbiv), cryptostreammode. Write );
Cstream. Write (inputbytearray, 0 , Inputbytearray. Length );
Cstream. flushfinalblock ();
Return Convert. tobase64string (mstream. toarray ());
}
Catch
{
Return Encryptstring;
}
}
/// <Summary>
/// Des decryption string
/// </Summary>
/// <Param name = "decryptstring"> String to be decrypted </Param>
/// <Param name = "decryptkey"> Decryption key, which must be 8 bits, the same as the encryption key </Param>
/// <Returns> The decrypted string is returned successfully, and the source string fails to be returned. </Returns>
Public Static String Decryptdes ( String Decryptstring, String Decryptkey)
{
Try
{
Byte [] Rgbkey = Encoding. utf8.getbytes (decryptkey );
Byte [] Rgbiv = Keys;
Byte [] Inputbytearray = Convert. frombase64string (decryptstring );
Descryptoserviceprovider dcsp = New Descryptoserviceprovider ();
Memorystream mstream = New Memorystream ();
Cryptostream cstream = New Cryptostream (mstream, dcsp. createdecryptor (rgbkey, rgbiv), cryptostreammode. Write );
Cstream. Write (inputbytearray, 0 , Inputbytearray. Length );
Cstream. flushfinalblock ();
Return Encoding. utf8.getstring (mstream. toarray ());
}
Catch
{
Return decryptstring;
}< BR >}