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

📄 iso9796d1encoding.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:   ISO9796d1Encoding.java

package org.bouncycastle.crypto.encodings;

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

public class ISO9796d1Encoding
	implements AsymmetricBlockCipher
{

	private static byte shadows[] = {
		14, 3, 5, 8, 9, 4, 2, 15, 0, 13, 
		11, 6, 7, 10, 12, 1
	};
	private static byte inverse[] = {
		8, 15, 6, 1, 5, 2, 11, 12, 3, 4, 
		13, 10, 14, 9, 0, 7
	};
	private AsymmetricBlockCipher engine;
	private boolean forEncryption;
	private int bitSize;
	private int padBits;

	public ISO9796d1Encoding(AsymmetricBlockCipher cipher)
	{
		padBits = 0;
		engine = cipher;
	}

	public AsymmetricBlockCipher getUnderlyingCipher()
	{
		return engine;
	}

	public void init(boolean forEncryption, CipherParameters param)
	{
		RSAKeyParameters kParam = null;
		if (param instanceof ParametersWithRandom)
		{
			ParametersWithRandom rParam = (ParametersWithRandom)param;
			kParam = (RSAKeyParameters)rParam.getParameters();
		} else
		{
			kParam = (RSAKeyParameters)param;
		}
		engine.init(forEncryption, kParam);
		bitSize = kParam.getModulus().bitLength();
		this.forEncryption = forEncryption;
	}

	public int getInputBlockSize()
	{
		int baseBlockSize = engine.getInputBlockSize();
		if (forEncryption)
			return (baseBlockSize + 1) / 2;
		else
			return baseBlockSize;
	}

	public int getOutputBlockSize()
	{
		int baseBlockSize = engine.getOutputBlockSize();
		if (forEncryption)
			return baseBlockSize;
		else
			return (baseBlockSize + 1) / 2;
	}

	public void setPadBits(int padBits)
	{
		if (padBits > 7)
		{
			throw new IllegalArgumentException("padBits > 7");
		} else
		{
			this.padBits = padBits;
			return;
		}
	}

	public int getPadBits()
	{
		return padBits;
	}

	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[(bitSize + 7) / 8];
		int r = padBits + 1;
		int z = inLen;
		int t = (bitSize + 13) / 16;
		for (int i = 0; i < t; i += z)
			if (i > t - z)
				System.arraycopy(in, (inOff + inLen) - (t - i), block, block.length - t, t - i);
			else
				System.arraycopy(in, inOff, block, block.length - (i + z), z);

		for (int i = block.length - 2 * t; i != block.length; i += 2)
		{
			byte val = block[(block.length - t) + i / 2];
			block[i] = (byte)(shadows[(val & 0xff) >>> 4] << 4 | shadows[val & 0xf]);
			block[i + 1] = val;
		}

		block[block.length - 2 * z] ^= r;
		block[block.length - 1] = (byte)(block[block.length - 1] << 4 | 6);
		int maxBit = 8 - (bitSize - 1) % 8;
		int offSet = 0;
		if (maxBit != 8)
		{
			block[0] &= 255 >>> maxBit;
			block[0] |= 128 >>> maxBit;
		} else
		{
			block[0] = 0;
			block[1] |= 0x80;
			offSet = 1;
		}
		return engine.processBlock(block, offSet, block.length - offSet);
	}

	private byte[] decodeBlock(byte in[], int inOff, int inLen)
		throws InvalidCipherTextException
	{
		byte block[] = engine.processBlock(in, inOff, inLen);
		int r = 1;
		int t = (bitSize + 13) / 16;
		if ((block[block.length - 1] & 0xf) != 6)
			throw new InvalidCipherTextException("invalid forcing byte in block");
		block[block.length - 1] = (byte)((block[block.length - 1] & 0xff) >>> 4 | inverse[(block[block.length - 2] & 0xff) >> 4] << 4);
		block[0] = (byte)(shadows[(block[1] & 0xff) >>> 4] << 4 | shadows[block[1] & 0xf]);
		boolean boundaryFound = false;
		int boundary = 0;
		for (int i = block.length - 1; i >= block.length - 2 * t; i -= 2)
		{
			int val = shadows[(block[i] & 0xff) >>> 4] << 4 | shadows[block[i] & 0xf];
			if (((block[i - 1] ^ val) & 0xff) == 0)
				continue;
			if (!boundaryFound)
			{
				boundaryFound = true;
				r = (block[i - 1] ^ val) & 0xff;
				boundary = i - 1;
			} else
			{
				throw new InvalidCipherTextException("invalid tsums in block");
			}
		}

		block[boundary] = 0;
		byte nblock[] = new byte[(block.length - boundary) / 2];
		for (int i = 0; i < nblock.length; i++)
			nblock[i] = block[2 * i + boundary + 1];

		padBits = r - 1;
		return nblock;
	}

}

⌨️ 快捷键说明

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