zoukankan      html  css  js  c++  java
  • .Net使用DES加密,.Net和java分别解密,并正则匹配替换加密密码为明文

    在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;
                }
            }
    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 < 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;
                }
            }
    .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密码解密
  • 相关阅读:
    从搭eclipse环境到导入maven工程
    基于jquery的多选下拉列框再次更改样式和交互
    BootStrap的typeahead使用过程中遇到的问题
    Vue webapp项目通过HBulider打包原生APP
    微信相机
    前端小新手,记录项目中不懂的问题
    判断pdf、word文档、图片等文件类型(格式)、大小的简便方法
    JavaScript学习笔记(一)——Map、Set与iterable
    oracle nvl函数
    mybaits中主键自动生成并返回主键
  • 原文地址:https://www.cnblogs.com/hujiapeng/p/7264247.html
Copyright © 2011-2022 走看看