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

📄 rsaengine.java

📁 java 文件下载器。可自定义
💻 JAVA
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   RSAEngine.java

package org.bouncycastle.crypto.engines;

import java.math.BigInteger;
import org.bouncycastle.crypto.*;
import org.bouncycastle.crypto.params.RSAKeyParameters;
import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;

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 + -