C 可逆加密類

2021-06-03 00:08:19 字數 2707 閱讀 7940

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.io;

using system.security.cryptography;

namespace fromargs

{class symmetricmethod

{private symmetricalgorithm mobjcryptoservice;

private string key;

///

/// 對稱加密類的建構函式

///

public symmetricmethod()

mobjcryptoservice = new rijndaelmanaged();

key = "guz(%&hj7x89h$yubi0456ftmat5&fvhufcy76*h%(hilj$lhj!y6&(*jkp87jh7";

///

/// 獲得金鑰

///

/// 金鑰

private byte getlegalkey()

string stemp = key;

mobjcryptoservice.generatekey();

byte byttemp = mobjcryptoservice.key;

int keylength = byttemp.length;

if (stemp.length > keylength)

stemp = stemp.substring(0, keylength);

else if (stemp.length < keylength)

stemp = stemp.padright(keylength, ' ');

return asciiencoding.ascii.getbytes(stemp);

///

/// 獲得初始向量iv

///

/// 初試向量iv

private byte getlegaliv()

string stemp = "e4ghj*ghg7!rnifb&95guy86gfghub#er57hbh(u%g6hj($jhwk7&!hg4ui%$hjk";

mobjcryptoservice.generateiv();

byte byttemp = mobjcryptoservice.iv;

int ivlength = byttemp.length;

if (stemp.length > ivlength)

stemp = stemp.substring(0, ivlength);

else if (stemp.length < ivlength)

stemp = stemp.padright(ivlength, ' ');

return asciiencoding.ascii.getbytes(stemp);

///

/// 加密方法

///

/// 待加密的串

/// 經過加密的串

public string encrypto(string source)

byte bytin = utf8encoding.utf8.getbytes(source);

memorystream ms = new memorystream();

mobjcryptoservice.key = getlegalkey();

mobjcryptoservice.iv = getlegaliv();

icryptotransform encrypto = mobjcryptoservice.createencryptor();

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

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

cs.flushfinalblock();

ms.close();

byte bytout = ms.toarray();

return convert.tobase64string(bytout);

///

/// 解密方法

///

/// 待解密的串

/// 經過解密的串

public string decrypto(string source)

byte bytin = convert.frombase64string(source);

memorystream ms = new memorystream(bytin, 0, bytin.length);

mobjcryptoservice.key = getlegalkey();

mobjcryptoservice.iv = getlegaliv();

icryptotransform encrypto = mobjcryptoservice.createdecryptor();

cryptostream cs = new cryptostream(ms, encrypto, cryptostreammode.read);

streamreader sr = new streamreader(cs);

return sr.readtoend();

雜湊(不可逆)加密通用類庫函式

using system using system.io using system.security.cryptography public class hasher 雜湊金鑰 public byte hashkey get 需要產生加密雜湊的字串 public string hashtext ge...

雜湊(不可逆)加密通用類庫函式

using system using system.io using system.security.cryptography public class hasher 雜湊金鑰 public byte hashkey get 需要產生加密雜湊的字串 public string hashtext ge...

python 文字簡單可逆加密

其實很簡單,就是把一段文字每個字元都通過某種方式改變 比如加1 這樣就實現了文字的加密操作,解密就是其逆運算 coding utf 8 import sys reload sys sys.setdefaultencoding utf8 加密def jiami filename raw input p...