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

📄 pkcs1encoding.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:   PKCS1Encoding.java

package org.bouncycastle.crypto.encodings;

import java.security.SecureRandom;
import org.bouncycastle.crypto.*;
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
import org.bouncycastle.crypto.params.ParametersWithRandom;

public class PKCS1Encoding
	implements AsymmetricBlockCipher
{

	private static int HEADER_LENGTH = 10;
	private SecureRandom random;
	private AsymmetricBlockCipher engine;
	private boolean forEncryption;
	private boolean forPrivateKey;

	public PKCS1Encoding(AsymmetricBlockCipher cipher)
	{
		engine = cipher;
	}

	public AsymmetricBlockCipher getUnderlyingCipher()
	{
		return engine;
	}

	public void init(boolean forEncryption, CipherParameters param)
	{
		AsymmetricKeyParameter kParam;
		if (param instanceof ParametersWithRandom)
		{
			ParametersWithRandom rParam = (ParametersWithRandom)param;
			random = rParam.getRandom();
			kParam = (AsymmetricKeyParameter)rParam.getParameters();
		} else
		{
			random = new SecureRandom();
			kParam = (AsymmetricKeyParameter)param;
		}
		engine.init(forEncryption, kParam);
		forPrivateKey = kParam.isPrivate();
		this.forEncryption = forEncryption;
	}

	public int getInputBlockSize()
	{
		int baseBlockSize = engine.getInputBlockSize();
		if (forEncryption)
			return baseBlockSize - HEADER_LENGTH;
		else
			return baseBlockSize;
	}

	public int getOutputBlockSize()
	{
		int baseBlockSize = engine.getOutputBlockSize();
		if (forEncryption)
			return baseBlockSize;
		else
			return baseBlockSize - HEADER_LENGTH;
	}

	public byte[] processBlock(byte in[], int inOff, int inLen)
		throws InvalidCipherTextException
	{
		if (forEncryption)
			return encodeBlock(in, inOff, inLen);
		else
			return decodeBlock(in, inOff, inLen);
	}

	private byte[] encodeBlock(byte in[], int inOff, int inLen)
		throws InvalidCipherTextException
	{
		byte block[] = new byte[engine.getInputBlockSize()];
		if (forPrivateKey)
		{
			block[0] = 1;
			for (int i = 1; i != block.length - inLen - 1; i++)
				block[i] = -1;

		} else
		{
			random.nextBytes(block);
			block[0] = 2;
			for (int i = 1; i != block.length - inLen - 1; i++)
				for (; block[i] == 0; block[i] = (byte)random.nextInt());

		}
		block[block.length - inLen - 1] = 0;
		System.arraycopy(in, inOff, block, block.length - inLen, inLen);
		return engine.processBlock(block, 0, block.length);
	}

	private byte[] decodeBlock(byte in[], int inOff, int inLen)
		throws InvalidCipherTextException
	{
		byte block[] = engine.processBlock(in, inOff, inLen);
		if (block.length < getOutputBlockSize())
			throw new InvalidCipherTextException("block truncated");
		if (block[0] != 1 && block[0] != 2)
			throw new InvalidCipherTextException("unknown block type");
		int start;
		for (start = 1; start != block.length && block[start] != 0; start++);
		if (++start >= block.length || start < HEADER_LENGTH)
		{
			throw new InvalidCipherTextException("no data in block");
		} else
		{
			byte result[] = new byte[block.length - start];
			System.arraycopy(block, start, result, 0, result.length);
			return result;
		}
	}

}

⌨️ 快捷键说明

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