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;
}
}
}
}
查看全文
相关阅读:
Nginx 学习笔记(七)如何解决nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
jQuery基础 (四)——使用jquery-cookie 实现点赞功能
Travis CI实现持续部署
三大云安全工具(CASB、CSPM、CWPP)的使用场景
数据访问安全代理 CASB
SDP(软件定义边界)让SDN更安全,你的对面可不能是一条狗!
从BeyondCorp说起
[Docker] Docker整体架构图
当博弈论遇上机器学习:一文读懂相关理论
用Rust重写Linux内核模块体验
原文地址:https://www.cnblogs.com/pw/p/615060.html
最新文章
错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 解决方法
最近面试的几个额外问题
netty支持的各种socketchannel实现及比较
Informatica 常用组件Lookup缓存之二 使用永久查找高速缓存
Informatica 常用组件Lookup缓存之一 概述
Informatica 常用组件Lookup之十 创建查找转换
Informatica 常用组件Lookup之九 配置未连接的查找转换
Informatica 常用组件Lookup之八 查找高速缓存
Informatica 常用组件Lookup之七 查找条件
Informatica 常用组件Lookup之六 查询
热门文章
Informatica 常用组件Lookup之五 转换属性
Informatica 常用组件Lookup之四 查找组件
Informatica 常用组件Lookup之三 关系和平面文件查找
Golang入门教程(三)beego 框架安装
Github 开源项目(二)gorun (go语言工具)
Nginx 学习笔记(八)http和https跨域问题解决
流媒体技术学习笔记之(十八)Ubuntu 16.04.3 如何编译 FFmpeg 记录
微信小程序入门教程(一)API接口数据记录
Openresty 学习笔记(二)Nginx Lua 正则表达式相关API
Git与GitHub学习笔记(八)git如何同时同步提交到码云和GitHub上
Copyright © 2011-2022 走看看