zoukankan
html css js c++ java
字符串加密方法
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Security.Cryptography;
using
System.IO;
namespace
Component
{
public
class
Security
{
public
Security()
{
}
//
默认密钥向量
private
static
byte
[] Keys
=
{
0x12
,
0x34
,
0x56
,
0x78
,
0x90
,
0xAB
,
0xCD
,
0xEF
}
;
/**/
///
<summary>
///
DES加密字符串
///
</summary>
///
<param name="encryptString">
待加密的字符串
</param>
///
<param name="encryptKey">
加密密钥,要求为8位
</param>
///
<returns>
加密成功返回加密后的字符串,失败返回源串
</returns>
public
static
string
EncryptDES(
string
encryptString,
string
encryptKey)
{
try
{
byte
[] 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;
}
}
/**/
///
<summary>
///
DES解密字符串
///
</summary>
///
<param name="decryptString">
待解密的字符串
</param>
///
<param name="decryptKey">
解密密钥,要求为8位,和加密密钥相同
</param>
///
<returns>
解密成功返回解密后的字符串,失败返源串
</returns>
public
static
string
DecryptDES(
string
decryptString,
string
decryptKey)
{
try
{
byte
[] 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;
}
}
}
}
查看全文
相关阅读:
C语言之分支语句
C语言之运算符与表达式
C语言之数据类型④——中文字符
独特的对象引用:this
理解赋值“=”的含义
编写类的 “模板”
类的定义
Java语言规范
第一周总结
定义常量
原文地址:https://www.cnblogs.com/pw/p/615060.html
最新文章
Java 泛型(二):自定义泛型结构
每日随笔
每日随笔
每周总结
每日随笔
每日随笔
每日随笔
每日随笔
每日随笔
nginx常用模块(连接限制模块 请求限制模块)
热门文章
nginx常用模块(状态模块)
nginx常用模块(访问控制模块)
nginx常用模块(目录索引模块 访问限制模块)
日志格式 日志参数
nginx搭建小游戏(域名搭建)
Linux虚拟机各个版本激活码
nginx搭建小游戏(IP搭建小游戏)
C语言之循环语句
C语言之switch语句与if...else..语句的比较及switch语句高效的原因
C语言之switch语句
Copyright © 2011-2022 走看看