【.NET】加密和解密(.NET)
类名:Security
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text; namespace Tools
{
///
/// Security 的摘要说明。
/// Security类实现.NET框架下的加密和解密。
/// CopyRight [email]KangSoft@Hotmail.com[/email]@[email]Hotmail.com@hotmail.com[/email]
///
public class Security
{
string _QueryStringKey = "abcdefgh"; //URL传输参数加密Key
string _PassWordKey = "hgfedcba"; //PassWord加密Key public Security()
{
//
// TODO: 在此处添加构造函数逻辑
//
} ///
/// 加密URL传输的字符串
///
public string EncryptQueryString(string QueryString)
{
return Encrypt(QueryString, _QueryStringKey);
} ///
/// 解密URL传输的字符串
///
public string DecryptQueryString(string QueryString)
{
return Decrypt(QueryString, _QueryStringKey);
} ///
/// 加密帐号口令
///
public string EncryptPassWord(string PassWord)
{
return Encrypt(PassWord, _PassWordKey);
} ///
/// 解密帐号口令
///
public string DecryptPassWord(string PassWord)
{
return Decrypt(PassWord, _PassWordKey);
} ///
/// DEC 加密过程
///
public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //把字符串放到byte数组中 byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //原文使用ASCIIEncoding.ASCII方法的GetBytes方法
MemoryStream ms = new MemoryStream(); //使得输入密码必须输入英文文本
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
} ///
/// DEC 解密过程
///
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / ];
for (int x = ; x < pToDecrypt.Length / ; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * , ), ));
inputByteArray[x] = (byte)i;
} des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量,此值重要,不能修改
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象 return System.Text.Encoding.Default.GetString(ms.ToArray());
} ///
/// 检查己加密的字符串是否与原文相同
///
public bool ValidateString(string EnString, string FoString, int Mode)
{
switch (Mode)
{
default:
case :
if (Decrypt(EnString, _QueryStringKey) == FoString.ToString())
{
return true;
}
else
{
return false;
}
case :
if (Decrypt(EnString, _PassWordKey) == FoString.ToString())
{
return true;
}
else
{
return false;
}
}
}
}
}
【.NET】加密和解密(.NET)的更多相关文章
- ASP.NET加密和解密数据库连接字符串
大家知道,在应用程序中进行数据库操作需要连接字符串,而如果没有连接字符串,我们就无法在应用程序中完成检索数据,创建数据等一系列的数据库操作.当有人想要获取你程序中的数据库信息,他首先看到的可能会是We ...
- 命令行工具aspnet_regiis.exe实现加密和解密web.config
命令行工具aspnet_regiis.exe,是一个类似于DOS的命令工具,称之为命令解释器.使用命令行工具加密和解密web.config文件中的数据库连接字符串时,只需要简单的语法命令即可. 加密语 ...
- 使用EncryptByPassPhrase和DecryptByPassPhrase对MS SQLServer某一字段时行加密和解密
在数据库实现加密与解密的文章,Insus.NET较早前也有写过,可以在本博客中可以搜索得到. 今天使用EncryptByPassPhrase和DecryptByPassPhrase来简单实现. 在数据 ...
- AES —— JAVA中对称加密和解密
package demo.security; import java.io.IOException; import java.io.UnsupportedEncodingException; impo ...
- 通过ios实现RSA加密和解密
在加密和解密中,我们需要了解的知识有什么事openssl:RSA加密算法的基本原理:如何通过openssl生成最后我们需要的der和p12文件. 废话不多说,直接写步骤: 第一步:openssl来生成 ...
- PHP 使用 mcrypt 扩展中的 mcrypt_encrypt() 和 mcrypt_decrypt() 对数据进行加密和解密
<?php /* 使用 mcrypt 扩展中的 mcrypt_encrypt() 和 mcrypt_decrypt() 对数据进行加密和解密 */ // 加密 $algorithm = MCRY ...
- iOS,一行代码进行RSA、DES 、AES、MD5加密、解密
本文为投稿文章,作者:Flying_Einstein(简书) 加密的Demo,欢迎下载 JAVA端的加密解密,读者可以看我同事的这篇文章:http://www.jianshu.com/p/98569e ...
- C# 利用SQLite对.DB和.logdb加密和解密和SQLite创建数据库
1.最近研究了下利用SQLite为db文件简单的加密和解密 private static SQLiteConnection GetConnection() { SQLiteConnection con ...
- 如何对web.config进行加密和解密
http://blog.csdn.net/jf_jifei/article/details/6527390 在WEB网站开发过程中,如果我们将数据库连接字符串封装到.DLL文件中,将会给数据库和程序的 ...
- RSA加密算法的加密与解密
转发原文链接:RSA加密算法加密与解密过程解析 1.加密算法概述 加密算法根据内容是否可以还原分为可逆加密和非可逆加密. 可逆加密根据其加密解密是否使用的同一个密钥而可以分为对称加密和非对称加密. 所 ...
随机推荐
- 12157 - Tariff Plan
Ampang Communications & Mobile (ACM) provides telecom services for various types of users. Sin ...
- 利用HTML5+Socket.io实现摇一摇控制PC端歌曲切换
我比较喜欢听音乐,特别是周末的时候,电脑开着百度随心听fm,随机播放歌曲,躺在床上享受.但碰到了一个烦人的事情,想切掉不喜欢的曲子,还得起床去操作电脑换歌.于是思考能不能用手机控制电脑切换歌曲,经过一 ...
- 结构-行为-样式-Bootstrap笔记
1.自上而下的内容布局,中间内容可变,应该用: <div class=" container-fluid"> <div class=" row" ...
- emacs 使用教程
http://www.cnblogs.com/liuchaogege/p/4464211.html
- ubuntu 使用apt-get install 安装php5.6--php7
使用ppa增加源:$ sudo apt-get install python-software-properties $ sudo add-apt-repository ppa:ondrej/php ...
- Thinkphp5 设置日志
database.php 'debug' => false, application/config.php 'log' => [ // 日志记录方式,支持 file socket 'typ ...
- Tiny6410之重定位代码到SRAM+4096
重定位代码 两个不同的地址概念: 对于程序而言,需要理解两个地址,一个是程序当前所处的地址,即程序运行时所处的当前地址.二是程序应该位于的运行地址,即编译程序时所指定的程序的链接地址.在Tiny641 ...
- openSuse使用技巧
1.opensuse的gnome默认使用nautilus作为文件浏览工具,若要设置文件的默认排序和视图,参考网页 https://thelinuxexperiment.com/change-the-d ...
- Winform DataGridView直接导出Excel
/// <summary> /// 导出excel /// </summary> /// <param name="fileName">导出文件 ...
- CodeForces 670D Magic Powder
二分. 二分一下答案,然后验证一下. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cst ...