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

📄 der.java

📁 面向应用的智能安全代理平台和工具包是一个综合网络应用的安全共性需求而设计和实现的一个通用性的网络信息安全应用支撑平台
💻 JAVA
字号:
package au.net.aba.crypto.provider;

/*
 * $Id: DER.java,v 1.2 1999/02/17 01:41:51 leachbj Exp $
 * $Author: leachbj $
 *
 * Copyright (C) 1996-1998 Australian Business Access Pty Ltd.
 * All rights reserved.
 * 
 * Use, modification, copying and distribution of this software is subject the
 * terms and conditions of the ABA Public Licence. See the file
 * "PUBLIC_LICENCE" for additional information.
 *
 * If you have not received a copy of the Public Licence, you must destroy all
 * copies of this file immediately. 
 *
 * $Source: /aba/CVSROOT/jdk1.1/src/au.net.aba/crypto/provider/DER.java,v $
 * $Revision: 1.2 $
 * $Date: 1999/02/17 01:41:51 $
 * $State: Exp $
 */

import java.math.BigInteger;
import java.io.*;

/**
 * A simple helper class for dealing with DER stuff.
 */
class DER
{
	static int INTEGER = 0x02;
	static int BIT_STRING = 0x03;
	static int OCTET_STRING = 0x04;
	static int SEQUENCE = 0x10;

	static int CONSTRUCTED = 0x20;

	/*
	 * PKCS#8 version string for PrivateKeyInfo, also used
	 * for PKCS#1 RSAPrivateKey
	 */
	static byte[] version = { (byte)0x02, (byte)0x01, (byte)0x00 };

	/*
	 * X.509 AlgorithmIdentifier for RSA (1.2.840.113549.1.1.1)
	 */
	static byte[] rsaEncryptionAlgorithmIdentifier = {
		// algorithm
		(byte)0x30, (byte)0x0D,

		// rsaEncryption (OID)
		(byte)0x06, (byte)0x09, (byte)0x2A, (byte)0x86,
		(byte)0x48, (byte)0x86, (byte)0xF7, (byte)0x0D,
		(byte)0x01, (byte)0x01, (byte)0x01,

		// null
		(byte)0x05, (byte)0x00
	};

	/**
	 * Read in a BER encoded INTEGER
	 *
	 * @return The new BigInteger
	 */
	static BigInteger readDERint(InputStream in)
	throws IOException
	{
		DataInputStream din = new DataInputStream(in);
		int tag = readTag(din);
		int len = readLen(din);

		if (tag != INTEGER)
		{
			throw new RuntimeException("Expecting tag[0x02] got "
				+ Integer.toHexString(tag));
		}
		if (len > 5096)
		{
			throw new RuntimeException("Length value seems a little big "
				+ Integer.toHexString(len));
		}

		byte[] intBytes = new byte[len];
		din.readFully(intBytes);
		return new BigInteger(intBytes);
	}
	/**
	 * Read in a Primitive or Constructed definite-length field.  Does
	 * not cope with indefinite-length fields.
	 */
	static int readLen(InputStream in)
	throws IOException
	{
		// read the length field
		int len = in.read();
		if (len > 127)
		{
			int longLen = 0;
			len ^= 0x80;
			for (int i = 0; i < len; i++)
			{
				longLen = (longLen * 256) + in.read();
			}

			len = longLen;
		}

		return len;
	}
	/**
	 * Read in an ASN.1 Tag
	 */
	static int readTag(InputStream in)
	throws IOException
	{
		return in.read();
	}
	/**
	 * Writes out the given BigInteger as a DER encoded INTEGER.
	 *
	 * @return The number of bytes written.
	 */
	static int writeDERint(OutputStream out, BigInteger bigInt)
	throws IOException
	{
		byte[] intBytes = bigInt.toByteArray();
		int len = intBytes.length + 1;
		out.write(INTEGER);
		len += writeDERlen(out, intBytes.length);
		out.write(intBytes);

		return len;
	}
	/**
	 * Write out the DER encoding for the given length.
	 *
	 * @return The number of bytes written.
	 */
	static int writeDERlen(OutputStream out, int len)
	throws IOException
	{
		if (len > 127)
		{
			int lenBytes = 1;
			if (len >= 256)
			{
				lenBytes = 2;
			}
			else if (len >= 65536)
			{
				lenBytes = 3;
			}
			else if (len >= 16777216)
			{
				lenBytes = 4;
			}

			out.write((byte)(lenBytes | 0x80));

			for (int i = (lenBytes - 1) * 8; i >= 0; i -= 8)
			{
				int v = (len >> i);
				out.write((byte)v);
			}

			return lenBytes;
		}
		else
		{
			out.write((byte)len);

			return 1;
		}
	}
}

⌨️ 快捷键说明

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