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

📄 pemwriter.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:   PEMWriter.java

package org.bouncycastle.openssl;

import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.cert.*;
import java.security.interfaces.*;
import org.bouncycastle.asn1.*;
import org.bouncycastle.asn1.cms.ContentInfo;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.pkcs.RSAPrivateKeyStructure;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.x509.DSAParameter;
import org.bouncycastle.jce.PKCS10CertificationRequest;
import org.bouncycastle.util.Strings;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.x509.X509AttributeCertificate;
import org.bouncycastle.x509.X509V2AttributeCertificate;

// Referenced classes of package org.bouncycastle.openssl:
//			PEMUtilities

public class PEMWriter extends BufferedWriter
{

	private String provider;

	public PEMWriter(Writer out)
	{
		this(out, "BC");
	}

	public PEMWriter(Writer out, String provider)
	{
		super(out);
		this.provider = provider;
	}

	private void writeHexEncoded(byte bytes[])
		throws IOException
	{
		bytes = Hex.encode(bytes);
		for (int i = 0; i != bytes.length; i++)
			write((char)bytes[i]);

	}

	private void writeEncoded(byte bytes[])
		throws IOException
	{
		char buf[] = new char[64];
		bytes = Base64.encode(bytes);
		for (int i = 0; i < bytes.length; i += buf.length)
		{
			int index;
			for (index = 0; index != buf.length && i + index < bytes.length; index++)
				buf[index] = (char)bytes[i + index];

			write(buf, 0, index);
			newLine();
		}

	}

	public void writeObject(Object o)
		throws IOException
	{
		String type;
		byte encoding[];
		if (o instanceof X509Certificate)
		{
			type = "CERTIFICATE";
			try
			{
				encoding = ((X509Certificate)o).getEncoded();
			}
			catch (CertificateEncodingException e)
			{
				throw new IOException((new StringBuilder()).append("Cannot encode object: ").append(e.toString()).toString());
			}
		} else
		if (o instanceof X509CRL)
		{
			type = "X509 CRL";
			try
			{
				encoding = ((X509CRL)o).getEncoded();
			}
			catch (CRLException e)
			{
				throw new IOException((new StringBuilder()).append("Cannot encode object: ").append(e.toString()).toString());
			}
		} else
		{
			if (o instanceof KeyPair)
			{
				writeObject(((KeyPair)o).getPrivate());
				return;
			}
			if (o instanceof PrivateKey)
			{
				PrivateKeyInfo info = new PrivateKeyInfo((ASN1Sequence)ASN1Object.fromByteArray(((Key)o).getEncoded()));
				if (o instanceof RSAPrivateKey)
				{
					type = "RSA PRIVATE KEY";
					encoding = info.getPrivateKey().getEncoded();
				} else
				if (o instanceof DSAPrivateKey)
				{
					type = "DSA PRIVATE KEY";
					DSAParameter p = DSAParameter.getInstance(info.getAlgorithmId().getParameters());
					ASN1EncodableVector v = new ASN1EncodableVector();
					v.add(new DERInteger(0));
					v.add(new DERInteger(p.getP()));
					v.add(new DERInteger(p.getQ()));
					v.add(new DERInteger(p.getG()));
					BigInteger x = ((DSAPrivateKey)o).getX();
					BigInteger y = p.getG().modPow(x, p.getP());
					v.add(new DERInteger(y));
					v.add(new DERInteger(x));
					encoding = (new DERSequence(v)).getEncoded();
				} else
				{
					throw new IOException("Cannot identify private key");
				}
			} else
			if (o instanceof PublicKey)
			{
				type = "PUBLIC KEY";
				encoding = ((PublicKey)o).getEncoded();
			} else
			if (o instanceof X509AttributeCertificate)
			{
				type = "ATTRIBUTE CERTIFICATE";
				encoding = ((X509V2AttributeCertificate)o).getEncoded();
			} else
			if (o instanceof PKCS10CertificationRequest)
			{
				type = "CERTIFICATE REQUEST";
				encoding = ((PKCS10CertificationRequest)o).getEncoded();
			} else
			if (o instanceof ContentInfo)
			{
				type = "PKCS7";
				encoding = ((ContentInfo)o).getEncoded();
			} else
			{
				throw new IOException("unknown object passed - can't encode.");
			}
		}
		writeHeader(type);
		writeEncoded(encoding);
		writeFooter(type);
	}

	public void writeObject(Object obj, String algorithm, char password[], SecureRandom random)
		throws IOException
	{
		if (obj instanceof KeyPair)
		{
			writeObject(((KeyPair)obj).getPrivate());
			return;
		}
		String type = null;
		byte keyData[] = null;
		if (obj instanceof RSAPrivateCrtKey)
		{
			type = "RSA PRIVATE KEY";
			RSAPrivateCrtKey k = (RSAPrivateCrtKey)obj;
			RSAPrivateKeyStructure keyStruct = new RSAPrivateKeyStructure(k.getModulus(), k.getPublicExponent(), k.getPrivateExponent(), k.getPrimeP(), k.getPrimeQ(), k.getPrimeExponentP(), k.getPrimeExponentQ(), k.getCrtCoefficient());
			keyData = keyStruct.getEncoded();
		} else
		if (obj instanceof DSAPrivateKey)
		{
			type = "DSA PRIVATE KEY";
			DSAPrivateKey k = (DSAPrivateKey)obj;
			DSAParams p = k.getParams();
			ASN1EncodableVector v = new ASN1EncodableVector();
			v.add(new DERInteger(0));
			v.add(new DERInteger(p.getP()));
			v.add(new DERInteger(p.getQ()));
			v.add(new DERInteger(p.getG()));
			BigInteger x = k.getX();
			BigInteger y = p.getG().modPow(x, p.getP());
			v.add(new DERInteger(y));
			v.add(new DERInteger(x));
			keyData = (new DERSequence(v)).getEncoded();
		}
		if (type == null || keyData == null)
			throw new IllegalArgumentException((new StringBuilder()).append("Object type not supported: ").append(obj.getClass().getName()).toString());
		String dekAlgName = Strings.toUpperCase(algorithm);
		if (dekAlgName.equals("DESEDE"))
			dekAlgName = "DES-EDE3-CBC";
		int ivLength = dekAlgName.startsWith("AES-") ? 16 : 8;
		byte iv[] = new byte[ivLength];
		random.nextBytes(iv);
		byte encData[] = PEMUtilities.crypt(true, provider, keyData, password, dekAlgName, iv);
		writeHeader(type);
		write("Proc-Type: 4,ENCRYPTED");
		newLine();
		write((new StringBuilder()).append("DEK-Info: ").append(dekAlgName).append(",").toString());
		writeHexEncoded(iv);
		newLine();
		newLine();
		writeEncoded(encData);
		writeFooter(type);
	}

	private void writeHeader(String type)
		throws IOException
	{
		write((new StringBuilder()).append("-----BEGIN ").append(type).append("-----").toString());
		newLine();
	}

	private void writeFooter(String type)
		throws IOException
	{
		write((new StringBuilder()).append("-----END ").append(type).append("-----").toString());
		newLine();
	}
}

⌨️ 快捷键说明

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