在VS中用WindowsApplication做一个exe程序,用来给数据库密码加密,加密代码如下

 private void generateBtn_Click(object sender, EventArgs e)
{
string pwd = pwdtxt.Text;
if(pwd==null ||pwd.Length==)
{
return;
}
string resultPwd = EncryptDES(pwd,"");
resultPwdTxt.Text = resultPwd;
}

加密生成按钮代码

/**/
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
private string EncryptDES(string encryptString, string encryptKey)
{
try
{
if (encryptKey.Length < )
{
encryptKey += "@#$%^&*!";
}
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring (,));
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbKey), CryptoStreamMode.Write);
cStream.Write(inputByteArray, , inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
catch
{
return encryptString;
}
}

DES加密代码

        /**/
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns> private string DecryptDES(string decryptString, string decryptKey)
{
try
{
if (decryptKey.Length < )
{
decryptKey += "@#$%^&*!";
}
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring (,));
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbKey), CryptoStreamMode.Write);
cStream.Write(inputByteArray, , inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
} private void button1_Click(object sender, EventArgs e)
{
string resultPwd = resultPwdTxt.Text;
string pwd = DecryptDES(resultPwd, "");
textBox1.Text = pwd;
}

解密测试代码

下面是.net对数据库连接字符串中加密的密码进行密码解密

public static string getdecodePwd(string currentConnectionStr)
{
Regex regex = new Regex("(;Password=)(.*)(;)", RegexOptions.IgnoreCase);
MatchCollection matcheCollection = regex.Matches(currentConnectionStr);
foreach (Match match in matcheCollection)
{
GroupCollection groups = match.Groups;
if (groups.Count == )
{
string enpwd = groups[].Value;
string depwd = DecryptDES(enpwd, "");
currentConnectionStr = regex.Replace(currentConnectionStr, ";Password=" + depwd + ";");
}
}
return currentConnectionStr;
} private static string DecryptDES(string decryptString, string decryptKey)
{
try
{
if (decryptKey.Trim().Length < )
{
decryptKey += "@#$%^&*!";
}
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(, ));
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbKey), CryptoStreamMode.Write);
cStream.Write(inputByteArray, , inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}

.net密码解密

下面是javat对数据库连接字符串中加密的密码进行密码解密

public static String getdecodePwd(String currentConnectionStr){
Pattern regex = Pattern.compile("(;Password=)(.*)(;)",Pattern.CASE_INSENSITIVE);
Matcher matcher=regex.matcher(currentConnectionStr);
if(matcher.find()&&matcher.groupCount()==3){
String enpwd = matcher.group(2);
String depwd = DecryptDES(enpwd, "");
currentConnectionStr =currentConnectionStr.replaceAll("(;Password=)(.*)(;)",";Password=" + depwd + ";");
}
return currentConnectionStr;
} private static String DecryptDES(String decryptString,String decryptKey){
try{
if(decryptKey.trim().length()<8){
decryptKey += "@#$%^&*!";
}
String key=decryptKey.substring(0, 8);
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytesrc = decoder.decodeBuffer(decryptString);
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] retByte = cipher.doFinal(bytesrc);
String result= new String(retByte);
return result;
}catch(Exception e){
e.printStackTrace();
return "";
}
}

java密码解密

最新文章

  1. Java 中获取类路径 classpath 的方法
  2. 【初探Spring】------Spring IOC(二):初始化过程---简介
  3. 【工具使用】sublime text3
  4. iPhone6搜索如何打开?详细使用方法
  5. 安装SQLServer2008后Windows防火墙上的端口开放
  6. SignalR 的跨域支持
  7. 从头开发MUDLIB
  8. JMS开源比较
  9. RabbitMQ (两)工作队列
  10. C#使用FFmpeg 将视频格式转换成Gif图片示例
  11. Mac下Charles Contents乱码解决办法
  12. 关于inet_addr() 函数
  13. python之读取配置文件模块configparser(一)基本操作
  14. IIS 一键安装及卸载
  15. insmod 签名问题
  16. Appium下载安装及环境配置
  17. [Day3]Scanner类、Random类、流程控制语句
  18. 解决IDEA查看源码时提示:Library source does not match the bytecode for class的问题分析
  19. UITextField in a UITableViewCell
  20. (一)MySQL登录与退出

热门文章

  1. window10删除导航栏中的onedrive
  2. Java设计模式(6)桥模式(Bridge模式)
  3. 安卓程序代写 网上程序代写[原]BluetoothServerSocket详解
  4. mysql执行带外键的sql文件时出现mysql ERROR 1215 (HY000): Cannot add foreign key constraint的解决
  5. 关于Unity中FPS第一人称射击类游戏制作(专题十)
  6. lua------------------Unity3D研究院编辑器之打开unity不可识别的文件(十三)
  7. ORACLE 几个我忍了他很多年的问题
  8. Java常用类(二)String类详解
  9. Obj格式模型 读取
  10. 转 linux 权限