Fork me on Github
欢迎来到zhang-shuai的博客
zhangshuai
晓看天色暮看云,行也思君,坐也思君

navicat解密mysql

先获取mysql加密密码,在使用php/Java解密程序解出密码,

1.使用navicat导出连接,文件中有加密密码,

2.在注册表中,计算机\HKEY_CURRENT_USER\SOFTWARE\PremiumSoft\Navicat\Servers\{连接名},点击找到pwd对应的值,即为加密密码

 

<?php
namespace FatSmallTools;
class NavicatPassword
{
    protected $version = 0;
    protected $aesKey = 'libcckeylibcckey';
    protected $aesIv = 'libcciv libcciv ';
    protected $blowString = '3DC5CA39';
    protected $blowKey = null;
    protected $blowIv = null;
    
    public function __construct($version = 12)
    {
        $this->version = $version;
        $this->blowKey = sha1('3DC5CA39', true);
        $this->blowIv = hex2bin('d9c7c3c8870d64bd');
    }

    public function encrypt($string)
    {
        $result = FALSE;
        switch ($this->version) {
            case 11:
                $result = $this->encryptEleven($string);
                break;
            case 12:
                $result = $this->encryptTwelve($string);
                break;
            default:
                break;
        }
        return $result;
    }

    protected function encryptEleven($string)
    {
        $round = intval(floor(strlen($string) / 8));
        $leftLength = strlen($string) % 8;
        $result = '';
        $currentVector = $this->blowIv;
        for ($i = 0; $i < $round; $i++) {
            $temp = $this->encryptBlock($this->xorBytes(substr($string, 8 * $i, 8), $currentVector));
            $currentVector = $this->xorBytes($currentVector, $temp);
            $result .= $temp;
        }
        if ($leftLength) {
            $currentVector = $this->encryptBlock($currentVector);
            $result .= $this->xorBytes(substr($string, 8 * $i, $leftLength), $currentVector);
        }
        return strtoupper(bin2hex($result));
    }

    protected function encryptBlock($block)
    {
        return openssl_encrypt($block, 'BF-ECB', $this->blowKey, OPENSSL_RAW_DATA|OPENSSL_NO_PADDING); 
    }

    protected function decryptBlock($block)
    {
        return openssl_decrypt($block, 'BF-ECB', $this->blowKey, OPENSSL_RAW_DATA|OPENSSL_NO_PADDING); 
    }

    protected function xorBytes($str1, $str2)
    {
        $result = '';
        for ($i = 0; $i < strlen($str1); $i++) {
            $result .= chr(ord($str1[$i]) ^ ord($str2[$i]));
        }
        return $result;
    }

    protected function encryptTwelve($string)
    {
        $result = openssl_encrypt($string, 'AES-128-CBC', $this->aesKey, OPENSSL_RAW_DATA, $this->aesIv);
        return strtoupper(bin2hex($result));
    }
    
    public function decrypt($string)
    {
        $result = FALSE;
        switch ($this->version) {
            case 11:
                $result = $this->decryptEleven($string);
                break;
            case 12:
                $result = $this->decryptTwelve($string);
                break;
            default:
                break;
        }
        return $result;
    }

    protected function decryptEleven($upperString)
    {
        $string = hex2bin(strtolower($upperString));
        $round = intval(floor(strlen($string) / 8));
        $leftLength = strlen($string) % 8;
        $result = '';
        $currentVector = $this->blowIv;
        for ($i = 0; $i < $round; $i++) {
            $encryptedBlock = substr($string, 8 * $i, 8);
            $temp = $this->xorBytes($this->decryptBlock($encryptedBlock), $currentVector);
            $currentVector = $this->xorBytes($currentVector, $encryptedBlock);
            $result .= $temp;
        }
        if ($leftLength) {
            $currentVector = $this->encryptBlock($currentVector);
            $result .= $this->xorBytes(substr($string, 8 * $i, $leftLength), $currentVector);
        }
        return $result;
    }
    
    protected function decryptTwelve($upperString)
    {
        $string = hex2bin(strtolower($upperString));
        return openssl_decrypt($string, 'AES-128-CBC', $this->aesKey, OPENSSL_RAW_DATA, $this->aesIv);
    }
}



use FatSmallTools\NavicatPassword;
//需要指定版本,11或12
//$navicatPassword = new NavicatPassword(12);
$navicatPassword = new NavicatPassword(11);
//解密,15057C7FA392=密码
$decode = $navicatPassword->decrypt('15057C7FA392');
echo $decode."\n";
php Code

 

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;

import cn.hutool.crypto.symmetric.SymmetricCrypto;

import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;


public class MysqlPwdCrack {
    private Integer version = 0;
    private String aesKey = "libcckeylibcckey";
    private String aesIv = "libcciv  libcciv";
    private String blowString = "3DC5CA39";
    private SecretKeySpec blowKey = null;
    private byte[] blowIv = null;

    private SymmetricCrypto blowfish;
    private AES aes;

    public MysqlPwdCrack(int version){
        this.version = version;

        byte[] bytes = SecureUtil.sha1().digest(this.blowString, CharsetUtil.charset("utf-8"));
        this.blowKey = new SecretKeySpec(bytes,"Blowfish");
        this.blowIv  = HexUtil.decodeHex("d9c7c3c8870d64bd");

        this.blowfish = new SymmetricCrypto("Blowfish/ECB/NoPadding", this.blowKey);
        this.aes = new AES(Mode.CBC, Padding.PKCS5Padding, new SecretKeySpec(this.aesKey.getBytes(),"AES"), new IvParameterSpec(this.aesIv.getBytes()));

    }

    public String encrypt(String str){
        String res = "";
        switch (this.version){
            case 11:
                res = this.encryptEleven(str);
                break;
            case 12:
                res = this.encryptTwelve(str);
                break;
            default:break;
        }

        return res;
    }

    public String decrypt(String str){
        String res = "";
        switch (this.version){
            case 11:
                res = this.decryptEleven(str);
                break;
            case 12:
                res = this.decryptTwelve(str);
                break;
            default:break;
        }
        return res;
    }

    private String encryptEleven(String str){
        byte[] inData = StrUtil.bytes(str);

        int blockSize = inData.length / 8;
        int leftLen = inData.length % 8;

        byte[] iv = Arrays.copyOf(this.blowIv, this.blowIv.length);

        byte[] ret = new byte[inData.length];

        for (int i = 0; i < blockSize; i++) {
            byte[] temp = Arrays.copyOfRange(inData,i*8,(i+1)*8);


            this.xorBytes(temp,iv,temp.length);
            temp = this.encryptBlock(temp);
            this.xorBytes(iv,temp,iv.length);

            System.arraycopy(temp,0,ret,i*8,8);
        }
        if (leftLen != 0){
            iv = this.encryptBlock(iv);
            byte[] temp = Arrays.copyOfRange(inData, blockSize * 8, blockSize * 8 + leftLen);
            xorBytes(temp,iv,leftLen);
            ArrayUtil.copy(temp,0,ret,blockSize*8,temp.length);
        }

        return StrUtil.str(ret,CharsetUtil.charset("utf-8"));
    }


    private String decryptEleven(String str){
        byte[] inData = HexUtil.decodeHex(str);

        int blockSize = inData.length / 8;
        int leftLen = inData.length % 8;

        byte[] iv = Arrays.copyOf(this.blowIv, this.blowIv.length);
        byte[] ret = new byte[inData.length];

        for (int i = 0; i < blockSize; i++) {
            byte[] temp = Arrays.copyOfRange(inData,i*8,(i+1)*8);

            temp = this.decryptBlock(temp);
            this.xorBytes(temp,iv,temp.length);

            ArrayUtil.copy(temp,0,ret,i*8,8);
            for (int j = 0; j < iv.length; j++) {
                iv[j] = (byte)(iv[j]^inData[i*8+j]);
            }
        }
        if (leftLen != 0){
            iv = this.encryptBlock(iv);
            byte[] temp = Arrays.copyOfRange(inData, blockSize * 8, blockSize * 8 + leftLen);
            xorBytes(temp,iv,leftLen);
            for (int j = 0; j < temp.length; j++) {
                ret[blockSize*8+j] = temp[j];
            }
        }

         return StrUtil.str(ret,CharsetUtil.charset("utf-8"));
    }

    private byte[] encryptBlock(byte[] str){
        return blowfish.encrypt(str);
    }

    private byte[] decryptBlock(byte[] str){
        return blowfish.decrypt(str);
    }

    private String encryptTwelve(String str){
        byte[] bytes = HexUtil.decodeHex(str);
        String s = aes.decryptStr(bytes);
        return s;
    }
    
    private String decryptTwelve(String str){
        byte[] bytes = aes.encrypt(str.getBytes());
        return HexUtil.encodeHexStr(bytes);
    }
    
    private void xorBytes(byte[] a,byte[] b,int len){
        for (int i = 0; i < len; i++) {
            int aVal = a[i] & 0xff; // convert byte to integer
            int bVal = b[i] & 0xff;
            a[i] = (byte) (aVal ^ bVal); // xor aVal and bVal and typecast to byte
        }
    }

    public static void main(String[] args) {
        // 默认11
        MysqlPwdCrack mysqlPwdCrack = new MysqlPwdCrack(11);

        String pwd1 = "15057C7FA392";
        String decrypt = mysqlPwdCrack.decrypt(pwd1);

        System.out.println(decrypt);

    }
}
java Code

 

package com.atguigu;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;

import cn.hutool.crypto.symmetric.SymmetricCrypto;

import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;


public class MysqlPasswordUtil {
    private Integer version = 0;
    private String aesKey = "libcckeylibcckey";
    private String aesIv = "libcciv  libcciv";
    private String blowString = "3DC5CA39";
    private SecretKeySpec blowKey = null;
    private byte[] blowIv = null;

    private SymmetricCrypto blowfish;
    private AES aes;

    public MysqlPasswordUtil(int version){
        this.version = version;
        // this.blowKey = SecureUtil.sha1(this.blowString);
        byte[] bytes = SecureUtil.sha1().digest(this.blowString, CharsetUtil.charset("utf-8"));
        this.blowKey = new SecretKeySpec(bytes,"Blowfish");
        this.blowIv  = DatatypeConverter.parseHexBinary("d9c7c3c8870d64bd");

        this.blowfish = new SymmetricCrypto("Blowfish/ECB/NoPadding", this.blowKey);
        this.aes = new AES(Mode.CBC, Padding.PKCS5Padding, new SecretKeySpec(this.aesKey.getBytes(),"AES"), new IvParameterSpec(this.aesIv.getBytes()));

    }

    public String encrypt(String str){
        String res = "";
        switch (this.version){
            case 11:
                res = this.encryptEleven(str);
                break;
            case 12:
                res = this.encryptTwelve(str);
                break;
            default:break;
        }

        return res;
    }

    public String decrypt(String str){
        String res = "";
        switch (this.version){
            case 11:
                res = this.decryptEleven(str);
                break;
            case 12:
                res = this.decryptTwelve(str);
                break;
            default:break;
        }
        return res;
    }

    private String encryptEleven(String str){
        byte[] inData = str.getBytes(CharsetUtil.charset("utf-8"));

        int blockSize = inData.length / 8;
        int leftLen = inData.length % 8;

        byte[] iv = Arrays.copyOf(this.blowIv, this.blowIv.length);
        byte[] ret = new byte[inData.length];

        for (int i = 0; i < blockSize; i++) {
            byte[] temp = Arrays.copyOfRange(inData,i*8,(i+1)*8);
            this.xorBytes(temp,iv);
            temp = this.encryptBlock(temp);
            this.xorBytes(iv,temp);

            System.arraycopy(temp,0,ret,i*8,8);
        }
        if (leftLen != 0){
            iv = this.encryptBlock(iv);
            byte[] temp = Arrays.copyOfRange(inData, blockSize * 8, blockSize * 8 + leftLen);
            xorBytes(temp,iv,leftLen);
            System.arraycopy(temp,0,ret,blockSize*8,temp.length);
        }

        return StrUtil.str(ret,CharsetUtil.charset("utf-8"));
    }


    private String decryptEleven(String str){
        byte[] inData = DatatypeConverter.parseHexBinary(str);
        // byte[] inData = str.getBytes(CharsetUtil.charset("utf-8"));

        int blockSize = inData.length / 8;
        int leftLen = inData.length % 8;

        byte[] iv = Arrays.copyOf(this.blowIv, this.blowIv.length);
        byte[] ret = new byte[inData.length];

        for (int i = 0; i < blockSize; i++) {
            byte[] temp = Arrays.copyOfRange(inData,i*8,(i+1)*8);

            temp = this.decryptBlock(temp);
            this.xorBytes(temp,iv);

            System.arraycopy(temp,0,ret,i*8,8);
            for (int j = 0; j < iv.length; j++) {
                iv[j] = (byte)(iv[j]^inData[i*8+j]);
            }
        }
        if (leftLen != 0){
            iv = this.encryptBlock(iv);
            byte[] temp = Arrays.copyOfRange(inData, blockSize * 8, blockSize * 8 + leftLen);
            xorBytes(temp,iv,leftLen);
            for (int j = 0; j < temp.length; j++) {
                ret[blockSize*8+j] = temp[j];
            }
        }

        // return StrUtil.str(ret,CharsetUtil.charset("utf-8"));
        String s = new String(ret, StandardCharsets.UTF_8);
        return s;
    }

    private byte[] encryptBlock(byte[] str){
        return blowfish.encrypt(str);
        // AES aes = new AES(Mode.ECB, Padding.NoPadding, this.blowKey);
        // return aes.encrypt(str);
    }

    private byte[] decryptBlock(byte[] str){
        return blowfish.decrypt(str);
        // AES aes = new AES(Mode.ECB, Padding.NoPadding, this.blowKey);
        // return aes.decrypt(str);
    }

    private String decryptTwelve(String str){
        byte[] bytes = aes.encrypt(str.getBytes());
        return DatatypeConverter.printHexBinary(bytes);
    }

    private String encryptTwelve(String str){
        byte[] bytes = DatatypeConverter.parseHexBinary(str);
        String s = aes.decryptStr(bytes);
        return s;
    }

    private void xorBytes(byte[] a,byte[] b){
        for (int i = 0; i < a.length; i++) {
            int aVal = a[i] & 0xff; // convert byte to integer
            int bVal = b[i] & 0xff;
            a[i] = (byte) (aVal ^ bVal); // xor aVal and bVal and typecast to byte
        }
    }

    private void xorBytes(byte[] a,byte[] b,int len){
        for (int i = 0; i < len; i++) {
            int aVal = a[i] & 0xff; // convert byte to integer
            int bVal = b[i] & 0xff;
            a[i] = (byte) (aVal ^ bVal); // xor aVal and bVal and typecast to byte
        }
    }

    public static void main(String[] args) {
        // 默认11
        MysqlPasswordUtil mysqlPasswordUtil = new MysqlPasswordUtil(11);

        String pwd1 = "15057C7FA392";
        String decrypt = mysqlPasswordUtil.decrypt(pwd1);

        System.out.println(decrypt);

    
posted @ 2022-09-22 22:39  咘網鶵釁  阅读(374)  评论(0编辑  收藏  举报