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

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

package jit.crypto.encodings;

import jit.crypto.*;
import jit.crypto.params.AsymmetricKeyParameter;
import jit.crypto.params.ParametersWithRandom;
import jit.security.SecureRandom;

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