⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsaengine.java

📁 进行与数字证书相关开发必须的java源码
💻 JAVA
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
// Source File Name:   RSAEngine.java

package jit.crypto.engines;

import jit.crypto.*;
import jit.crypto.params.RSAKeyParameters;
import jit.crypto.params.RSAPrivateCrtKeyParameters;
import jit.math.BigInteger;

public class RSAEngine
    implements AsymmetricBlockCipher
{

    private RSAKeyParameters key;
    private boolean forEncryption;

    public RSAEngine()
    {
    }

    public void init(boolean forEncryption, CipherParameters param)
    {
        key = (RSAKeyParameters)param;
        this.forEncryption = forEncryption;
    }

    public int getInputBlockSize()
    {
        int bitSize = key.getModulus().bitLength();
        if(forEncryption)
            return (bitSize + 7) / 8 - 1;
        else
            return (bitSize + 7) / 8;
    }

    public int getOutputBlockSize()
    {
        int bitSize = key.getModulus().bitLength();
        if(forEncryption)
            return (bitSize + 7) / 8;
        else
            return (bitSize + 7) / 8 - 1;
    }

    public byte[] processBlock(byte in[], int inOff, int inLen)
    {
        if(inLen > getInputBlockSize() + 1)
            throw new DataLengthException("input too large for RSA cipher.\n");
        if(inLen == getInputBlockSize() + 1 && (in[inOff] & 0x80) != 0)
            throw new DataLengthException("input too large for RSA cipher.\n");
        byte block[];
        if(inOff != 0 || inLen != in.length)
        {
            block = new byte[inLen];
            System.arraycopy(in, inOff, block, 0, inLen);
        } else
        {
            block = in;
        }
        BigInteger input = new BigInteger(1, block);
        byte output[];
        if(key instanceof RSAPrivateCrtKeyParameters)
        {
            RSAPrivateCrtKeyParameters crtKey = (RSAPrivateCrtKeyParameters)key;
            BigInteger p = crtKey.getP();
            BigInteger q = crtKey.getQ();
            BigInteger dP = crtKey.getDP();
            BigInteger dQ = crtKey.getDQ();
            BigInteger qInv = crtKey.getQInv();
            BigInteger mP = input.remainder(p).modPow(dP, p);
            BigInteger mQ = input.remainder(q).modPow(dQ, q);
            BigInteger h = mP.subtract(mQ);
            h = h.multiply(qInv);
            h = h.mod(p);
            BigInteger m = h.multiply(q);
            m = m.add(mQ);
            output = m.toByteArray();
        } else
        {
            output = input.modPow(key.getExponent(), key.getModulus()).toByteArray();
        }
        if(forEncryption)
        {
            if(output[0] == 0 && output.length > getOutputBlockSize())
            {
                byte tmp[] = new byte[output.length - 1];
                System.arraycopy(output, 1, tmp, 0, tmp.length);
                return tmp;
            }
            if(output.length < getOutputBlockSize())
            {
                byte tmp[] = new byte[getOutputBlockSize()];
                System.arraycopy(output, 0, tmp, tmp.length - output.length, output.length);
                return tmp;
            }
        } else
        if(output[0] == 0)
        {
            byte tmp[] = new byte[output.length - 1];
            System.arraycopy(output, 1, tmp, 0, tmp.length);
            return tmp;
        }
        return output;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -