DES加密,解密

2022-06-18 23:03:26 字數 2765 閱讀 7675

using system;

using system.io;

using system.text;

using system.security.cryptography;

namespace elight.infrastructure

descryptoserviceprovider des = new descryptoserviceprovider();

byte inputbytearray = encoding.getencoding("utf-8").getbytes(text);

byte a = asciiencoding.ascii.getbytes(key);

des.key = asciiencoding.ascii.getbytes(key);

des.iv = asciiencoding.ascii.getbytes(key);

memorystream ms = new memorystream();

cryptostream cs = new cryptostream(ms, des.createencryptor(), cryptostreammode.write);

cs.write(inputbytearray, 0, inputbytearray.length);

cs.flushfinalblock();

///

/// des解密,使用自定義金鑰。

///

/// 待解密的秘文

/// 必須是8位字元的金鑰字串(不能有特殊字元)

///

public static string desdecrypt(this string cyphertext, string key)

if (string.isnullorempty(cyphertext))

return string.empty;

descryptoserviceprovider des = new descryptoserviceprovider();

byte inputbytearray = new byte[cyphertext.length / 2];

for (int x = 0; x < cyphertext.length / 2; x++)

des.key = asciiencoding.ascii.getbytes(key);

des.iv = asciiencoding.ascii.getbytes(key);

memorystream ms = new memorystream();

cryptostream cs = new cryptostream(ms, des.createdecryptor(), cryptostreammode.write);

cs.write(inputbytearray, 0, inputbytearray.length);

cs.flushfinalblock();

stringbuilder ret = new stringbuilder();

return system.text.encoding.getencoding("utf-8").getstring(ms.toarray());

}///

/// des加密,使用預設金鑰。

///

/// 待加密的明文

///

public static string desencrypt(this string text)

///

/// des解密,使用預設金鑰。

///

/// 待解密的秘文

///

public static string desdecrypt(this string cyphertext)

#endregion

#region base64加密解密

///

/// base64加密,採用指定字元編碼方式加密。

///

/// 待加密的明文

/// 字元編碼

///

public static string base64encrypt(this string input, encoding encode)

///

/// base64加密,採用utf8編碼方式加密。

///

/// 待加密的明文

///

public static string base64encrypt(this string input)

///

/// base64解密,採用utf8編碼方式解密。

///

/// 待解密的秘文

///

public static string base64decrypt(this string input)

///

/// base64解密,採用指定字元編碼方式解密。

///

/// 待解密的秘文

/// 字元的編碼

///

public static string base64decrypt(this string input, encoding encode)

#endregion

#region md5加密

///

/// 字串md5加密。

///

/// 需要加密的字串

///

///

/// 檔案流md5加密。

///

/// 需要加密的檔案流

///

DES加密解密

using system using system.drawing using system.collections using system.componentmodel using system.windows.forms using system.data using system.io us...

DES加密解密

加密類 date 2012 04 23 pm.public class encrypt private encrypt 建立加密類物件.public static encrypt createcryptoserviceprovider des加密.待加密字串.加密金鑰.8位 public strin...

DES加密解密

using system.data using system.configuration using system.web using system.web.security using system.web.ui using system.web.ui.webcontrols using syst...