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

📄 rsapubkey.java

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

/*
 * $Id: RSAPubKey.java,v 1.9 1999/02/02 00:56:35 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/RSAPubKey.java,v $
 * $Revision: 1.9 $
 * $Date: 1999/02/02 00:56:35 $
 * $State: Exp $
 */

import java.math.BigInteger;
import java.security.interfaces.RSAPublicKey;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * A class for ABA RSA public keys.
 */
public class RSAPubKey implements RSAPublicKey
{
	public final static String ident = "$Id: RSAPubKey.java,v 1.9 1999/02/02 00:56:35 leachbj Exp $";

	//==================================
	// Protected Interface
	//==================================

	/**
	 * The exponent component of the two part key that is
	 * required by the RSA algorithm.
	 */
	protected BigInteger	exponent;

	/**
	 * The modulus component of the two part key that is
	 * required by the RSA algorithm.
	 */
	protected BigInteger	modulus;

	//==================================
	// Constructors
	//==================================

	/**
	 * Construct an empty RSAPubKey.
	 */
	public RSAPubKey()
	{
	}
	/**
	 * Construct an RSAPubKey from an X.509 encoded byte array
	 */
	public RSAPubKey(byte[] encKey)
	{
		x509Decode(encKey);
	}
	/**
	 * Construct an RSAPubKey from two integral components.
	 *
	 * @param modulus	The modulus value.
	 * @param exponent	The exponent value.
	 */
	public RSAPubKey(
		BigInteger modulus,
		BigInteger exponent)
	{
		this.modulus = modulus;
		this.exponent = exponent;
	}
	/**
	 * Return the algorithm for this key.
	 *
	 * @return the string RSA.
	 */
	public String getAlgorithm()
	{
		return "RSA";
	}
	/**
	 * Return an encoded representation for this key.  Returns a
	 * DER encoded X.509 SubjectPublicKeyInfo block containing
	 * a RSAPublicKey as defined in PKCS#1.
	 *
	 * @see #getFormat
	 * @see #toString
	 */
	public byte[] getEncoded()
	{
		return x509Encode();
	}
	/**
	 * Return the format this key is in.  This returns "X.509".
	 */
	public String getFormat()
	{
		return "X.509";
	}
	//==================================
	// Pub Interface
	//==================================

	/**
	 * Returns the modulus.
	 *
	 * @return the modulus
	 */
	public BigInteger getModulus()
	{
		return modulus;
	}
	/**
	 * Returns the public exponent.
	 *
	 * @return the public exponent
	 */
	public BigInteger getPublicExponent()
	{
		return exponent;
	}
	/**
	 * Generate a String representation of this key.
	 *
	 * @return		The key as a string.
	 */
	public String toString()
	{
		return modulus.toString(16)
			+ "." + exponent.toString(16);
	}
	private void x509Decode(byte[] encKey)
	{
		ByteArrayInputStream bin = new ByteArrayInputStream(encKey);

		try
		{
			int tag, len;

			// SubjectPublicKeyInfo, SEQUENCE
			DER.readTag(bin);
			DER.readLen(bin);

			// AlgorithmIdentifier
			bin.skip(DER.rsaEncryptionAlgorithmIdentifier.length);

			// subjectPublicKey BIT STRING
			DER.readTag(bin);
			DER.readLen(bin);
			bin.skip(1);

			// RSAPublicKey, SEQUENCE
			DER.readTag(bin);
			DER.readLen(bin);

			modulus = DER.readDERint(bin);
			exponent = DER.readDERint(bin);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			throw new ExceptionInInitializerError(e);
		}
	}
/*

SubjectPublicKeyInfo ::= SEQUENCE { 
	algorithm AlgorithmIdentifier, 
	subjectPublicKey BIT STRING 
} 
	  
AlgorithmIdentifier ::= SEQUENCE { 
	algorithm OBJECT IDENTIFIER, 
	parameters ANY DEFINED BY algorithm OPTIONAL 
} 

PKCS#1 defines;

rsaEncryption OBJECT IDENTIFIER ::=  { 1 2 840 113549 1 1 1 }

RSAPublicKey ::= SEQUENCE {
	modulus INTEGER, -- n
	publicExponent INTEGER -- e
}

*/

	private byte[] x509Encode()
	{
		try
		{
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			DER.writeDERint(bout, modulus);
			DER.writeDERint(bout, exponent);

			byte[] keyBytes = bout.toByteArray();

			bout = new ByteArrayOutputStream();
			bout.write(DER.SEQUENCE | DER.CONSTRUCTED);
			DER.writeDERlen(bout, keyBytes.length);
			bout.write(keyBytes);
			byte[] rsaPublicKey = bout.toByteArray();

			bout = new ByteArrayOutputStream();
			bout.write(DER.BIT_STRING);
			DER.writeDERlen(bout, rsaPublicKey.length + 1);
			bout.write(0x00);
			bout.write(rsaPublicKey);
			byte[] subjectPublicKey = bout.toByteArray();

			bout = new ByteArrayOutputStream();
			bout.write(DER.SEQUENCE | DER.CONSTRUCTED);
			DER.writeDERlen(bout, DER.rsaEncryptionAlgorithmIdentifier.length
				+ subjectPublicKey.length);
			bout.write(DER.rsaEncryptionAlgorithmIdentifier);
			bout.write(subjectPublicKey);

			return bout.toByteArray();
		}
		catch (IOException e)
		{
			e.printStackTrace();
			throw new ExceptionInInitializerError(e);
		}
	}
}

⌨️ 快捷键说明

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