金鑰隨機生成 :
class des
// 建立key
public string generatekey()
descryptoserviceprovider descrypto = (descryptoserviceprovider)descryptoserviceprovider.create();
return asciiencoding.ascii.getstring(descrypto.key);
//預設金鑰向量
private static byte keys = ;
///
/// des加密字串
///
/// 待加密的字串
/// 加密金鑰,要求為8位
/// 加密成功返回加密後的字串,失敗返回源串
public string encryptdes(string encryptstring, string encryptkey)
trybyte 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;
///
/// des解密字串
///
/// 待解密的字串
/// 解密金鑰,要求為8位,和加密金鑰相同
/// 解密成功返回解密後的字串,失敗返源串
public string decryptdes(string decryptstring, string decryptkey)
trybyte 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;
DES 加密解密字串
字串如果操過指定長度則將超出的部分用指定字串代替 要檢查的字串 指定長度 用於替換的字串 擷取後的字串 private static string getsubstring string p srcstring,int p length,string p tailstring 取指定長度的字串 要檢...
C 對字串加密解密 DES
using system using system.collections.generic using system.linq using system.text using system.security using system.security.cryptography using syste...
C 對字串加密解密 DES
using system using system.collections.generic using system.linq using system.text using system.security using system.security.cryptography using syste...