📄 publickeysecuredobject.java
字号:
package au.net.aba.crypto;
/*
* $Id: PublicKeySecuredObject.java,v 1.8 1998/10/27 05:10:49 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.
*
* $Source: /aba/CVSROOT/jdk1.1/src/au.net.aba/crypto/PublicKeySecuredObject.java,v $
* $Revision: 1.8 $
* $Date: 1998/10/27 05:10:49 $
* $State: Exp $
*/
import java.io.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
/**
* a holding class for secret key encrypted objects which have their secret
* keys attached. The secret key is encrypted using some other algorithm.
*/
public class PublicKeySecuredObject implements Externalizable
{
public final static String ident = "$Id: PublicKeySecuredObject.java,v 1.8 1998/10/27 05:10:49 leachbj Exp $";
static final long serialVersionUID = 1L;
static final String STD_ALGORITHM = "RC4";
static final String STD_PROVIDER = "ABA";
byte[] keyBlock;
byte[] ivBlock;
SecuredObject securedObj;
/*
* if we are not using externalisable these should probably be transient
*/
Cipher in, out;
/**
* general constructor used by externalisable.
*/
public PublicKeySecuredObject()
{
in = out = null;
}
/**
* create a public key secured object using the standard algorithm
* and the standard provider for the secret key. The object is
* encrypted with the secret key and the secret key is encrypted
* with the cipher passed in.
*
* @param obj a serialisable object to be encrypted.
* @param cipher the cipher to be used for encrypting the
* secret key.
* @param random a random source for generating the secret
* key.
* @exception IOException if object not serialisable or some other
* exception occurs.
*/
public PublicKeySecuredObject(
Object obj,
Cipher cipher,
SecureRandom random)
throws IOException
{
this(obj, cipher, random, STD_ALGORITHM, STD_PROVIDER);
}
/**
* create a public key secured object. The object is encrypted with
* a secret key and the secret key is encrypted with the cipher passed
* in.
*
* @param obj a serialisable object to be encrypted.
* @param cipher the cipher to be used for encrypting the
* secret key.
* @param random a random source for generating the secret
* key.
* @param algorithm the name of the algorithm that the object will
* be encrypted with.
* @param provider the provider name for the object encryption
* algorithm.
* @exception IOException if object not serialisable or some other
* exception occurs.
*/
public PublicKeySecuredObject(
Object obj,
Cipher cipher,
SecureRandom random,
String algorithm,
String provider)
throws IOException
{
Key key;
try
{
KeyGenerator keyGen;
keyGen = KeyGenerator.getInstance(algorithm, provider);
keyGen.init(random);
key = keyGen.generateKey();
out = Cipher.getInstance(algorithm, provider);
out.init(Cipher.ENCRYPT_MODE, key, random);
}
catch (Exception e)
{
throw new IOException(e.toString());
}
/*
* encrypt the key bytes.
*/
ByteArrayOutputStream bOut;
DataOutputStream dOut;
byte[] ivBytes, keyBytes;
bOut = new ByteArrayOutputStream();
dOut = new DataOutputStream(bOut);
ivBytes = out.getIV();
keyBytes = key.getEncoded();
dOut.writeUTF(algorithm);
dOut.writeUTF(provider);
dOut.writeShort(keyBytes.length);
dOut.write(keyBytes);
if (ivBytes != null)
{
dOut.writeShort(ivBytes.length);
dOut.write(ivBytes);
}
else
{
dOut.writeShort(0);
}
ivBlock = cipher.getIV();
try
{
keyBlock = cipher.doFinal(bOut.toByteArray());
}
catch (Exception e)
{
throw new IOException(e.toString());
}
/*
* encrypt and hash the object.
*/
securedObj = new SecuredObject(obj, out);
/*
* set up the ciphers for getDecryptor/getEncryptor
*/
IvParameterSpec paramSpec;
if (ivBytes != null)
{
paramSpec = new IvParameterSpec(ivBytes);
}
else
{
paramSpec = null;
}
try
{
in = Cipher.getInstance(algorithm, provider);
in.init(Cipher.DECRYPT_MODE, key, paramSpec);
out.init(Cipher.ENCRYPT_MODE, key, paramSpec);
}
catch (Exception e)
{
throw new IOException(e.toString());
}
}
/**
* return a Cipher capable of decrypting another object encrypted
* with the same secret key as this one.
*
* @return the secret key cipher that was originally used, set
* up for decryption.
*/
public Cipher getDecryptor()
{
return in;
}
/**
* return a Cipher capable of encrypting another object with
* the secret key used to encrypt this one. Note: in the event
* you are dealing with an object constructed by someone else,
* this call is only meaningful if you have decrypted the object.
*
* @return the secret key cipher that was originally used, set
* up for encryption.
*/
public Cipher getEncryptor()
{
return out;
}
/**
* returns a copy of the initialisation vector.
*
* @return the IV vector for the cipher that encrypted the secret key's
*/
public byte[] getIV()
{
if (ivBlock != null)
{
byte[] tmp;
tmp = new byte[ivBlock.length];
System.arraycopy(ivBlock, 0, tmp, 0, tmp.length);
}
return null;
}
/**
* Return the plain text object.
*
* @param cipher The cipher used to decrypt the secret key and
* allow the decryption of the object.
* @return The plain text object.
* @exception ClassNotFoundException The object class could not be
* loaded.
*/
public Object getObject(
Cipher cipher)
throws IOException, ClassNotFoundException
{
ByteArrayInputStream bIn;
DataInputStream dIn;
ObjectInputStream oIn;
Object obj;
short length;
String algorithm, provider;
byte[] data, keyBytes, ivBytes;
try
{
data = cipher.doFinal(keyBlock);
}
catch (Exception e)
{
throw new IOException(e.toString());
}
bIn = new ByteArrayInputStream(data);
dIn = new DataInputStream(bIn);
algorithm = dIn.readUTF();
provider = dIn.readUTF();
length = dIn.readShort();
keyBytes = new byte[length];
dIn.read(keyBytes);
/*
* decode the object.
*/
IvParameterSpec paramSpec;
SecretKeySpec secretKey;
length = dIn.readShort();
if (length != 0)
{
ivBytes = new byte[length];
dIn.read(ivBytes);
paramSpec = new IvParameterSpec(ivBytes);
}
else
{
paramSpec = null;
}
secretKey = new SecretKeySpec(keyBytes, algorithm);
try
{
in = Cipher.getInstance(algorithm, provider);
in.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
}
catch (Exception e)
{
throw new IOException(e.toString());
}
obj = securedObj.getObject(in);
/*
* set up the ciphers for getDecryptor/getEncryptor
*/
try
{
out = Cipher.getInstance(algorithm, provider);
in.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
out.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
}
catch (Exception e)
{
throw new IOException(e.toString());
}
return obj;
}
/**
* serialisation support using Externalizable.
*
* @param in the object input stream.
* @exception ClassNotFoundException - the class definition of
* the serialised object could not be loaded.
*/
public void readExternal(
ObjectInput in)
throws IOException, ClassNotFoundException
{
short length;
length = in.readShort();
if (length != 0)
{
ivBlock = new byte[length];
in.readFully(ivBlock, 0, ivBlock.length);
}
else
{
ivBlock = null;
}
length = in.readShort();
if (length != 0)
{
keyBlock = new byte[length];
in.readFully(keyBlock, 0, keyBlock.length);
}
else
{
keyBlock = null;
}
securedObj = (SecuredObject)in.readObject();
}
/**
* serialisation support using Externalizable.
*
* @param out the object output stream.
*/
public void writeExternal(
ObjectOutput out)
throws IOException
{
if (ivBlock != null)
{
out.writeShort(ivBlock.length);
out.write(ivBlock, 0, ivBlock.length);
}
else
{
out.writeShort(0);
}
if (keyBlock != null)
{
out.writeShort(keyBlock.length);
out.write(keyBlock, 0, keyBlock.length);
}
else
{
out.writeShort(0);
}
out.writeObject(securedObj);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -