C#加密解密总结文档格式.docx
- 文档编号:15084848
- 上传时间:2022-10-27
- 格式:DOCX
- 页数:12
- 大小:20.43KB
C#加密解密总结文档格式.docx
《C#加密解密总结文档格式.docx》由会员分享,可在线阅读,更多相关《C#加密解密总结文档格式.docx(12页珍藏版)》请在冰豆网上搜索。
returns>
加密后的字符串<
/returns>
public
string
SHA1(string
source)
{
return
FormsAuthentication.HashPasswordForStoringInConfigFile(source,
"
SHA1"
);
}
MD5加密字符串
MD5(string
MD5"
;
方法二(可逆加密解密):
System.Security.Cryptography;
Encode(string
data)
byte[]
byKey
=
System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byIV
System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
DESCryptoServiceProvider
cryptoProvider
new
DESCryptoServiceProvider();
int
i
cryptoProvider.KeySize;
MemoryStream
ms
MemoryStream();
CryptoStream
cst
CryptoStream(ms,
cryptoProvider.CreateEncryptor(byKey,
byIV),
CryptoStreamMode.Write);
StreamWriter
sw
StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
Convert.ToBase64String(ms.GetBuffer(),
0,
(int)ms.Length);
Decode(string
byEnc;
try
byEnc
Convert.FromBase64String(data);
catch
null;
MemoryStream(byEnc);
cryptoProvider.CreateDecryptor(byKey,
CryptoStreamMode.Read);
StreamReader
sr
StreamReader(cst);
sr.ReadToEnd();
方法三(MD5不可逆):
//MD5不可逆加密
//32位加密
GetMD5_32(string
s,
_input_charset)
MD5
md5
MD5CryptoServiceProvider();
t
md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
StringBuilder
sb
StringBuilder(32);
for
(int
0;
t.Length;
i++)
sb.Append(t[i].ToString("
x"
).PadLeft(2,
'
0'
));
sb.ToString();
//16位加密
static
GetMd5_16(string
ConvertString)
MD5CryptoServiceProvider
t2
BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)),
4,
8);
t2.Replace("
-"
t2;
方法四(对称加密):
System.IO;
private
SymmetricAlgorithm
mobjCryptoService;
Key;
对称加密类的构造函数
SymmetricMethod()
mobjCryptoService
RijndaelManaged();
Key
Guz(%&
hj7x89H$yuBI0456FtmaT5&
fvHUFCy76*h%(HilJ$lhj!
y6&
(*jkP87jH7"
获得密钥
密钥<
GetLegalKey()
sTemp
mobjCryptoService.GenerateKey();
bytTemp
mobjCryptoService.Key;
KeyLength
bytTemp.Length;
if
(sTemp.Length
KeyLength)
sTemp.Substring(0,
KeyLength);
else
sTemp.PadRight(KeyLength,
ASCIIEncoding.ASCII.GetBytes(sTemp);
获得初始向量IV
初试向量IV<
GetLegalIV()
E4ghj*Ghg7!
rNIfb&
95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&
!
hg4ui%$hjk"
mobjCryptoService.GenerateIV();
mobjCryptoService.IV;
IVLength
IVLength)
IVLength);
sTemp.PadRight(IVLength,
加密方法
Source"
待加密的串<
经过加密的串<
Encrypto(string
Source)
bytIn
UTF8Encoding.UTF8.GetBytes(Source);
mobjCryptoService.Key
GetLegalKey();
mobjCryptoService.IV
GetLegalIV();
ICryptoTransform
encrypto
mobjCryptoService.CreateEncryptor();
cs
encrypto,
cs.Write(bytIn,
bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
bytOut
ms.ToArray();
Convert.ToBase64String(bytOut);
解密方法
待解密的串<
经过解密的串<
Decrypto(string
Conve
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 加密 解密 总结
![提示](https://static.bdocx.com/images/bang_tan.gif)