openfire用户加密Blowfish源代码

本文介绍了如何从openfire源代码中提取Blowfish.java类以避免依赖大型jar包,并提供了使用Blowfish进行加密和解密的示例,适用于项目中的独立加密需求。

可能你正在使用openfire.jar里面的Blowfish进行加密,但一个openfire.jar包有7M多,有时候还出现项目可以正常部署,但运行不起来,老是报编译jsp文件出错,没有办法,只好下载openfire源代码,把Blowfish.java这个类代码拷出来,不使用openfire.jar,项目可以正常运行,也不用openfire.jar这个7M多的包,感觉很好




Blowfish blowfish = new Blowfish(getPasswordKey());
String encryptedPassword = blowfish.encryptString(openFireUserPO.getEncryptedPassword());


可以参考

Openfire3.6.4用户密码的保存及加密、解密


下面是Blowfish.java的源代码


/**
 * $RCSfile$
 * $Revision: 3657 $
 * $Date: 2002-09-09 08:31:31 -0700 (Mon, 09 Sep 2002) $
 *
 * Adapted from Markus Hahn's Blowfish package so that all functionality is
 * in a single source file. Please visit the following URL for his excellent
 * package: http://www.hotpixel.net/software.html
 *
 * Copyright (c) 1997-2002 Markus Hahn <markus_hahn@gmx.net>
 *
 * Released under the Apache 2.0 license.
 */

package com.catic.mobilehos.utils;

import java.security.MessageDigest;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * A class that provides easy Blowfish encryption.<p>
 *
 * @author Markus Hahn <markus_hahn@gmx.net>
 * @author Gaston Dombiak
 */
public class Blowfish {
	private Log Log = LogFactory.getLog(this.getClass());

    private BlowfishCBC m_bfish;
    private static Random m_rndGen = new Random();

    /**
     * Creates a new Blowfish object using the specified key (oversized
     * password will be cut).
     *
     * @param password the password (treated as a real unicode array)
     */
    public Blowfish(String password) {
        // hash down the password to a 160bit key
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance("SHA1");
            digest.update(password.getBytes());
        }
        catch (Exception e) {
            Log.error(e.getMessage(), e);
        }

        // setup the encryptor (use a dummy IV)
        m_bfish = new BlowfishCBC(digest.digest(), 0);
        digest.reset();
    }

    /**
     * Encrypts a string (treated in UNICODE) using the
     * standard Java random generator, which isn't that
     * great for creating IVs
     *
     * @param sPlainText string to encrypt
     * @return encrypted string in binhex format
     */
    public String encryptString(String sPlainText) {
        // get the IV
        long lCBCIV;
        synchronized (m_rndGen)
        {
            lCBCIV = m_rndGen.nextLong();
        }

        // map the call;
        return encStr(sPlainText, lCBCIV);
    }

    // Internal routine for string encryption

    private String encStr(String sPlainText,
                          long lNewCBCIV)
    {
        // allocate the buffer (align to the next 8 byte border plus padding)
        int nStrLen = sPlainText.length();
        byte[] buf = new byte [((nStrLen << 1) & 0xfffffff8) + 8];

        // copy all bytes of the string into the buffer (use network byte order)
        int nI;
        int nPos = 0;
        for (nI = 0; nI < nStrLen; nI++)
        {
            char cActChar = sPlainText.charAt(nI);
            buf[nPos++] = (byte) ((cActChar >> 8) & 0x0ff);
            buf[nPos++] = (byte) (cActChar & 0x0ff) ;
        }

        // pad the rest with the PKCS5 scheme
        byte bPadVal = (byte)(buf.length - (nStrLen << 1));
        while (nPos < buf.length)
        {
            buf[nPos++] = bPadVal;
        }

        synchronized (m_bfish) {
            // create the encryptor
            m_bfish.setCBCIV(lNewCBCIV);

            // encrypt the buffer
            m_bfish.encrypt(buf);
        }

        // return the binhex string
        byte[] newCBCIV = new byte[BlowfishCBC.BLOCKSIZE];
        longToByteArray(lNewCBCIV,
                newCBCIV,
                0);

        return bytesToBinHex(newCBCIV, 0, BlowfishCBC.BLOCKSIZE) +
                bytesToBinHex(buf, 0, buf.length);
    }


    /**
     * decrypts a hexbin string (handling is case sensitive)
     * @param sCipherText hexbin string to decrypt
     * @return decrypted string (null equals an error)
     */
    public String decryptString(String sCipherText)
    {
        // get the number of estimated bytes in the string (cut off broken blocks)
        int nLen = (sCipherText.length() >> 1) & ~7;

        // does the given stuff make sense (at least the CBC IV)?
        if (nLen < BlowfishECB.BLOCKSIZE)
            return null;

        // get the CBC IV
        byte[] cbciv = new byte[BlowfishCBC.BLOCKSIZE];
        int nNumOfBytes = binHexToBytes(sCipherText,
                cbciv,
                0,
                0,
                BlowfishCBC.BLOCKSIZE);
        if (nNumOfBytes < BlowfishCBC.BLOCKSIZE)
            return null;

        // something left to decrypt?
        nLen -= BlowfishCBC.BLOCKSIZE;
        if (nLen == 0)
        {
            return "";
        }

        // get all data bytes now
        byte[] buf = new byte[nLen];

        nNumOfBytes = binHexToBytes(sCipherText,
                buf,
                BlowfishCBC.BLOCKSIZE * 2,
                0,
                nLen);

        // we cannot accept broken binhex sequences due to padding
        // and decryption
        if (nNumOfBytes < nLen)
        {
            return null;
        }

        synchronized (m_bfish) {
            // (got it)
            m_bfish.setCBCIV(cbciv);

            // decrypt the buffer
            m_bfish.decrypt(buf);
        }

        // get the last padding byte
        int nPadByte = (int)buf[buf.length - 1] & 0x0ff;

        // ( try to get all information if the padding doesn't seem to be correct)
        if ((nPadByte > 8) || (nPadByte < 0))
        {
            nPadByte = 0;
        }

        // calculate the real size of this message
        nNumOfBytes -= nPadByte;
        if (nNumOfBytes < 0)
        {
            return "";
        }

        // success
        return byteArrayToUNCString(buf, 0, nNumOfBytes);
    }


    /**
     * destroys (clears) the encryption engine,
     * after that the instance is not valid anymore
     */
    public void destroy()
    {
        m_bfish.cleanUp();
    }

    /**
     * implementation of the Blowfish encryption algorithm in ECB mode
     * @author Markus Hahn <markus_hahn@gmx.net>
     * @version Feburary 14, 2001
     */
    private static class BlowfishECB
    {
        /** maximum possible key length */
        public final static int MAXKEYLENGTH = 56;


        /** block size of this cipher (in bytes) */
        public final static int BLOCKSIZE = 8;

        // size of the single boxes
        final static int PBOX_ENTRIES = 18;
        final static int SBOX_ENTRIES = 256;

        // the boxes
        int[] m_pbox;
        int[] m_sbox1;
        int[] m_sbox2;
        int[] m_sbox3;
        int[] m_sbox4;


        /**
         * default constructor
         * @param bfkey key material, up to MAXKEYLENGTH bytes
         */
        public BlowfishECB(byte[] bfkey)
        {
            // create the boxes
            int nI;

            m_pbox = new int[PBOX_ENTRIES];

            for (nI = 0; nI < PBOX_ENTRIES; nI++)
            {
                m_pbox[nI] = pbox_init[nI];
            }

            m_sbox1 = new int[SBOX_ENTRIES];
            m_sbox2 = new int[SBOX_ENTRIES];
            m_sbox3 = new int[SBOX_ENTRIES];
            m_sbox4 = new int[SBOX_ENTRIES];

            for (nI = 0; nI < SBOX_ENTRIES; nI++)
            {
                m_sbox1[nI] = sbox_init_1[nI];
                m_sbox2[nI] = sbox_init_2[nI];
                m_sbox3[nI] = sbox_init_3[nI];
                m_sbox4[nI] = sbox_init_4[nI];
            }

            // xor the key over the p-boxes

            int nLen = bfkey.length;
            if (nLen == 0) return; // such a setup is also valid (zero key "encryption" is possible)
            int nKeyPos = 0;
            int nBuild = 0;
            int nJ;

            for (nI = 0; nI < PBOX_ENTRIES; nI++)
            {
                for (nJ = 0; nJ < 4; nJ++)
                {
                    nBuild = (nBuild << 8) | (((int) bfkey[nKeyPos]) & 0x0ff);

                    if (++nKeyPos == nLen)
                    {
                        nKeyPos = 0;
                    }
                }
                m_pbox[nI] ^= nBuild;
            }


            // encrypt all boxes with the all zero string
            long lZero = 0;

            // (same as above)
            for (nI = 0; nI < PBOX_ENTRIES; nI += 2)
            {
                lZero = encryptBlock(lZero);
                m_pbox[nI] = (int) (lZero >>> 32);
                m_pbox[nI+1] = (int) (lZero & 0x0ffffffffL);
            }
            for (nI = 0; nI < SBOX_ENTRIES; nI += 2)
            {
                lZero = encryptBlock(lZero);
                m_sbox1[nI] = (int) (lZero >>> 32);
                m_sbox1[nI+1] = (int) (lZero & 0x0ffffffffL);
            }
            for (nI = 0; nI < SBOX_ENTRIES; nI += 2)
            {
                lZero = encryptBlock(lZero);
                m_sbox2[nI] = (int) (lZero >>> 32);
                m_sbox2[nI+1] = (int) (lZero & 0x0ffffffffL);
            }
            for (nI = 0; nI < SBOX_ENTRIES; nI += 2)
            {
                lZero = encryptBlock(lZero);
                m_sbox3[nI] = (int) (lZero >>> 32);
                m_sbox3[nI+1] = (int) (lZero & 0x0ffffffffL);
            }
            for (nI = 0; nI < SBOX_ENTRIES; nI += 2)
            {
                lZero = encryptBlock(lZero);
                m_sbox4[nI] = (int) (lZero >>> 32);
                m_sbox4[nI+1] = (int) (lZero & 0x0ffffffffL);
            }
        }

        /**
         * to clear data in the boxes before an instance is freed
         */
        public void cleanUp()
        {
            int nI;

            for (nI = 0; nI < PBOX_ENTRIES; nI++)
            {
                m_pbox[nI] = 0;
            }

            for (nI = 0; nI < SBOX_ENTRIES; nI++)
            {
                m_sbox1[nI] = m_sbox2[nI] = m_sbox3[nI] = m_sbox4[nI] = 0;
            }
        }

        /**
         * selftest routine, to check e.g. for a valid class file transmission
         * @return true: selftest passed / false: selftest failed
         */
        public static boolean selfTest()
        {
            // test vector #1 (checking for the "signed bug")
            byte[] testKey1 = { (byte) 0x1c, (byte) 0x58, (byte) 0x7f, (byte) 0x1c,
                                (byte) 0x13, (byte) 0x92, (byte) 0x4f, (byte) 0xef };
            int[] tv_p1 = { 0x30553228, 0x6d6f295a };
            int[] tv_c1 = { 0x55cb3774, 0xd13ef201 };
            int[] tv_t1 = new int[2];

            // test vector #2 (offical vector by Bruce Schneier)
            String sTestKey2 = "Who is John Galt?";
            byte[] testKey2 = sTestKey2.getBytes();

            int[] tv_p2 = { 0xfedcba98, 0x76543210 };
            int[] tv_c2 = { 0xcc91732b, 0x8022f684 };
            int[] tv_t2 = new int[2];


            // start the tests, check for a proper decryption, too

            BlowfishECB testbf1 = new BlowfishECB(testKey1);

            testbf1.encrypt(tv_p1, tv_t1);

            if ((tv_t1[0] != tv_c1[0]) ||
                    (tv_t1[1] != tv_c1[1]))
            {
                return false;
            }

            testbf1.decrypt(tv_t1);

            if ((tv_t1[0] != tv_p1[0]) ||
                    (tv_t1[1] != tv_p1[1]))
            {
                return false;
            }

            BlowfishECB testbf2 = new BlowfishECB(testKey2);

            testbf2.encrypt(tv_p2, tv_t2);

            if ((tv_t2[0] != tv_c2[0]) ||
                    (tv_t2[1] != tv_c2[1]))
            {
                return false;
            }

            testbf2.decrypt(tv_t2);

            if ((tv_t2[0] != tv
内容概要:本文档围绕“基于序阻抗建模的虚拟同步发电机(VSG)并网逆变器仿真模型”展开,旨在复现高水平期刊论文中的关键研究成果。资源包含完整的Simulink仿真模型与配套MATLAB代码,重点聚焦于VSG在弱电网条件下的序阻抗建模方法、正负序阻抗特性分析、扫频法仿真验证及系统稳定性判据应用。内容深入探讨了VSG并网逆变器的小信号动态响应、阻抗交互机理及其在复杂电网环境中的稳定运行问题,同时关联多项前沿研究主题,如构网型变流器、光伏逆变器阻抗建模、低电压穿越控制等,体现了较强的学术复现与工程仿真价值。; 适合人群:面向具备电力系统、电力电子或自动化等相关专业背景的研究生、科研人员及工程技术人员,特别适用于从事新能源并网、逆变器控制策略设计、电网阻抗建模与稳定性分析等方向的研究者,以及需要复现顶刊论文仿真实验的技术人员。; 使用场景及目标:① 掌握VSG并网系统的序阻抗建模流程,理解正负序阻抗对弱电网稳定性的影响机制;② 学习并实现基于Simulink的扫频法仿真技术,完成阻抗特性辨识与奈奎斯特稳定性判断;③ 借助所提供的模型与代码快速搭建仿真平台,支撑科研论文撰写、课题申报、项目开发与学术复现实验。; 阅读建议:建议结合自身研究方向选择核心模块进行精读与调试,优先运行扫频与阻抗建模部分,重点关注模型参数设置、仿真步长选取与结果物理意义的解读;推荐参考文中提及的博士/硕士论文复现案例,深化理论与实践结合,提升对复杂电力电子系统稳定性问题的理解与建模能力。
内容概要:本文系统研究了光伏并网逆变器与虚拟同步发电机(VSG)在弱电网环境下的正负序阻抗建模方法,并基于Simulink平台构建了两者的精细化阻抗模型,实现了扫频仿真与稳定性对比分析。研究聚焦于不对称电网条件下系统的动态响应特性,通过分序阻抗建模揭示其在扰动下的交互机理,采用扫频法验证模型准确性,并结合奈奎斯特稳定性判据对两类逆变器的并网稳定性进行深入评估。内容涵盖从理论建模、仿真实现到稳定性判据应用的完整技术链条,尤其强调对VSG惯性与阻尼特性的模拟及其对系统稳定裕度的改善作用,为高比例新能源接入引发的弱电网稳定问题提供了有效的分析工具与解决方案,具备较高的学术研究价值与工程复现意义。; 适合人群:电力电子、电力系统自动化、新能源并网技术及相关专业的硕士/博士研究生、科研人员以及从事并网逆变器控制、电网稳定性分析的工程师。; 使用场景及目标:①掌握光伏并网逆变器与虚拟同步发电机的正负序阻抗建模核心技术;②熟练运用Simulink进行阻抗扫描(sweeping)与时域/频域联合仿真;③对比分析跟网型与构网型逆变器在弱电网中的稳定性能差异,为新型电力系统中构网型控制策略的设计与优化提供理论依据和技术支撑。; 阅读建议:建议结合文中提及的“博士论文复现”“期刊复现”等实例,下载配套的Simulink仿真模型与相关代码资源,动手实践阻抗建模与扫频全过程,深入理解锁相环、电流环等控制环节对序阻抗特性的影响,并可进一步拓展至多机并网、宽频振荡等复杂场景的稳定性研究。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值