2012年3月6日 星期二

[.Net] 字串加解密

public static string encode(string code) {



CAPICOM.EncryptedData oCrypt = new CAPICOM.EncryptedData();



oCrypt.Content = code;



oCrypt.Algorithm.KeyLength = CAPICOM.CAPICOM_ENCRYPTION_KEY_LENGTH.CAPICOM_ENCRYPTION_KEY_LENGTH_40_BITS; oCrypt.Algorithm.Name = CAPICOM.CAPICOM_ENCRYPTION_ALGORITHM.CAPICOM_ENCRYPTION_ALGORITHM_DES; oCrypt.SetSecret(format, CAPICOM.CAPICOM_SECRET_TYPE.CAPICOM_SECRET_PASSWORD);



return oCrypt.Encrypt(CAPICOM.CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BASE64);



}



public static string decode(string code) {



CAPICOM.EncryptedData oDecrypt = new CAPICOM.EncryptedData(); oDecrypt.Algorithm.KeyLength = CAPICOM.CAPICOM_ENCRYPTION_KEY_LENGTH.CAPICOM_ENCRYPTION_KEY_LENGTH_40_BITS; oDecrypt.Algorithm.Name = CAPICOM.CAPICOM_ENCRYPTION_ALGORITHM.CAPICOM_ENCRYPTION_ALGORITHM_DES; oDecrypt.SetSecret(format, CAPICOM.CAPICOM_SECRET_TYPE.CAPICOM_SECRET_PASSWORD); oDecrypt.Decrypt(code);



return oDecrypt.Content;

}




---------------------------------------------------------------------------------


public static string encode(string code) {


byte[] strByte = Encoding.Default.GetBytes(code);


return Convert.ToBase64String(strByte);
}
public static string decode(string code) {


try {


byte[] strByte = Convert.FromBase64String(code);


return Encoding.Default.GetString(strByte);


}


catch (Exception e) { return ""; }


}


沒有留言:

張貼留言