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

📄 rsakeypairgenerator.java

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

/*
 * $Id: RSAKeyPairGenerator.java,v 1.9 1998/10/30 04:20:14 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/RSAKeyPairGenerator.java,v $
 * $Revision: 1.9 $
 * $Date: 1998/10/30 04:20:14 $
 * $State: Exp $
 */

import java.math.*;
import java.security.*;

/**
 * A class for generating a random RSA public/private key pair.
 */
public class RSAKeyPairGenerator extends KeyPairGenerator
{
	public final static String ident = "$Id: RSAKeyPairGenerator.java,v 1.9 1998/10/30 04:20:14 leachbj Exp $";

	private static BigInteger one = BigInteger.valueOf(1);
	private static BigInteger two = BigInteger.valueOf(2);

	int		strength;
	SecureRandom	random;
	int		certainty = 20;

	/**
	 * basic constructor.
	 */
	public RSAKeyPairGenerator()
	{
		super("RSA");
	}
	/**
	 * Generates a RSA key pair.
	 *
	 * @return a RSA public/private key pair.
	 */
	public KeyPair generateKeyPair()
	{
		BigInteger	p, q, n, d, e, pSub1, qSub1, phi;

		/*
		 * Each part of the key should be half the given strength
		 */
		int keyStrength = (strength + 1) / 2;

		p = new BigInteger(keyStrength, certainty, random);

		do
		{
			/**
			 * Generate a random number of strength bits that is a
			 * probable prime with a certainty of 1 - 1/2**certainty.
			 */
			q = new BigInteger(keyStrength, certainty, random);
		}
		while (p.equals(q));		// just in case

		if (p.compareTo(q) < 0)
		{
			BigInteger      tmp = p;

			p = q;
			q = tmp;
		}

		n = p.multiply(q);

		pSub1 = p.subtract(one);
		qSub1 = q.subtract(one);
		phi = pSub1.multiply(qSub1);

		e = BigInteger.valueOf(0x11);

		while (!e.gcd(phi).equals(one))
		{
			e = e.add(two);
		}

		d = e.modInverse(phi);

		/*
		 * create the factors for the private key
		 */
		BigInteger	dP, dQ, qInv;

		dP = d.remainder(pSub1);
		dQ = d.remainder(qSub1);
		qInv = q.modInverse(p);

		/*
		 * create the keys
		 */
		RSAPubKey	pubKey;
		RSAPrivKeyCrt	privKey;

		pubKey = new RSAPubKey(n, e);
		privKey = new RSAPrivKeyCrt(n, e, d, p, q, dP, dQ, qInv);

		return new KeyPair(pubKey, privKey);
	}
	/**
	 * Initialises the key pair generator for a certain strength, using
	 * the default parameter set. 
	 *
	 * @param strength	the size of the key (in bits).
	 * @param random	a source of randomness.
	 */
	public void initialize(
		int		strength,
		SecureRandom	random)
	{
		this.strength = strength;
		this.random = random;
	}
}

⌨️ 快捷键说明

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