• .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密码解密
  • 相关阅读:
    加密算法使用(五):RSA使用全过程
    加密算法使用(四):AES的使用
    加密算法使用(三):用用BASE64
    加密算法使用(二):使用MD5加密字符串(另:byte数组转16进制自动补零方法写法)
    加密算法使用(一):用CRC32来压缩32uuid字符串
    【转载】阿里云ECS Linux服务器禁止某些IP访问
    【转载】Sqlserver数据库备份的几种方式
    【转载】网站域名备案相关流程介绍
    【转载】C#工具类:实现文件操作File的工具类
    【转载】 禁止国外IP访问你的网站
  • 原文地址:https://www.cnblogs.com/hujiapeng/p/7264247.html
Copyright © 2020-2023  润新知