在VS中用WindowsApplication做一个exe程序,用来给数据库密码加密,加密代码如下
private void generateBtn_Click(object sender, EventArgs e) { string pwd = pwdtxt.Text; if(pwd==null ||pwd.Length==0) { 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 < 8) { encryptKey += "@#$%^&*!"; } byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring (0,8)); 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, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } catch { return encryptString; } }
/**/ /// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> private string DecryptDES(string decryptString, string decryptKey) { try { if (decryptKey.Length < 8) { decryptKey += "@#$%^&*!"; } byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring (0,8)); 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, 0, 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 == 4) { string enpwd = groups[2].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 < 8) { decryptKey += "@#$%^&*!"; } byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8)); 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, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return decryptString; } }
下面是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 ""; } }