openfire用户加密Blowfish源代码

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

可能你正在使用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);

            tes
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值