📄 asn1object.java
字号:
type.toString() + "\n" + e.toString());
}
catch (IllegalAccessException e)
{
throw new ASN1Exception("Cannot create instance for " +
type.toString() + "\n" + e.toString());
}
}
/**
* Same as the method create(ASN1Type), except that this method assigns
* the created ASN1Object an initial value.
* @param v the object to be assigned to the ASN1Object as its value.
*/
public static ASN1Object create(ASN1Type type, Object v)
throws ASN1Exception
{
ASN1Object o = create(type);
o.setValue(v);
return o;
}
/*
* Conversion methods:
* The following section contains methods that convert ASN1Objects
* to/from various forms, like byte array, character string, file,
* encrypted form, etc.
*/
/**
* Initializes the DER encoded byte array of the ASN1Object.
*/
public void initByteArray()
{
try
{
byte [] buf = ASN1Util.toByteArrayDER(this);
setByteArray(buf);
}
catch(ASN1Exception asn1e)
{
asn1e.printStackTrace(System.out);
setByteArray(null);
}
}
/**
* Converts to a DER byte array.
*/
public byte [] toDERBytes()
{
try
{
return ASN1Util.toByteArrayDER(this);
}
catch(ASN1Exception asn1e)
{
asn1e.printStackTrace(System.out);
return null;
}
}
/*
* Converts to an encrypted DER byte array. - *** Not used ***
* @param cipher the encryption algorithm.
* @param iv 8-byte array which will be filled up with IV bytes.
* @param pw the Password object to get an encrypion password.
* @exception com.ca.commons.security.pkcs5.PasswordException if error occured
* when get a new password from 'pw'.
*
public byte [] toDERBytes(String cipher, byte [] iv, Password pw)
throws PasswordException {
byte [] buf = toPEMBytes(cipher, iv, pw);
return Base64Coder.decode(buf);
}
*/
/**
* Converts to a DER file (in binary format).
*/
public boolean toDERFile(File file)
throws IOException
{
try
{
ASN1Util.saveDER(this, file);
return true;
}
catch(ASN1Exception asn1e)
{
asn1e.printStackTrace(System.out);
return false;
}
}
/**
* Converts to a base64 encoded byte array.
*/
public byte [] toBase64()
{
try
{
return ASN1Util.toByteArrayPEM(this);
}
catch(ASN1Exception asn1e)
{
asn1e.printStackTrace(System.out);
return null;
}
}
/*
* Converts to an encrypted PEM byte array. - *** Not used ***
* @param cipher the encryption algorithm.
* @param iv 8-byte array which will be filled up with IV bytes.
* @param pw the Password object to get an encrypion password.
* @exception com.ca.commons.security.pkcs5.PasswordException if error occured
* when get a new password from 'pw'.
*
private byte [] toPEMBytes(String cipher, byte [] iv, Password pw)
throws PasswordException {
// cannot see any chance to use it directly at the moment
byte [] buf = toDERBytes();
byte [] tmp = new byte[8];
byte [] enc = Envelope.writeEncByteArray(buf, cipher, tmp, 0, pw);
if (iv != null && iv.length >= 8) {
System.arraycopy(tmp, 0, iv, 0, 8);
}
return enc;
}
*/
/**
* Converts to a PEM file.
* @param name the object type/name.
*/
public boolean toPEMFile(File file, String name)
throws IOException
{
try
{
ASN1Util.savePEM(this, file, name);
return true;
}
catch(ASN1Exception asn1e)
{
asn1e.printStackTrace(System.out);
return false;
}
}
/*
* Converts to an encrypted PEM file. - *** Not used ***
* @param name the object type/name.
* @param cipher the encryption algorithm.
* @param pw the Password object to get an encrypion password.
* @exception com.ca.commons.security.pkcs5.PasswordException if error occured
* when get a new password from 'pw'.
*
public boolean toPEMFile(File file, String name,
String cipher, Password pw) throws IOException, PasswordException {
byte [] buf = toDERBytes();
return Envelope.writeEncFile(file, buf, name, cipher, 0, pw);
}
*/
/*
* Converts to a PEM String. - *** Not used ***
* @param name the object type/name.
*
public String toPEMString(String name) {
try {
return toPEMString(name, null, null);
} catch(PasswordException pe) {
return null;
}
}
/*
* Converts to an encrypted PEM String. - *** Not used ***
* @param name the object type/name.
* @param cipher the encryption algorithm.
* @param pw the Password object to get an encrypion password.
* @exception com.ca.commons.security.pkcs5.PasswordException if error occured
* when get a new password from 'pw'.
*
public String toPEMString(String name, String cipher,
Password pw) throws PasswordException {
byte [] buf = toDERBytes();
try {
return Envelope.writeEncFile(buf, name, cipher, 0, pw);
} catch(IOException ioe) {
return null;
}
}
*/
/*
* from other objects to ASN1Object
*/
/* - *** Not used ***
* Converts from a byte array. The byte array can be either
* base64 encoded or not, and can be encrypted or not.
* @param cipher the encryption algorithm.
* @param iv the Initial Vector.
* @param pw the Password object to get an encrypion password.
* @exception com.ca.commons.security.pkcs5.PasswordException if cannot get the
* password or the password is wrong.
*
public static ASN1Object fromBytes(byte [] data, String cipher,
byte [] iv, Password pw) throws PasswordException {
if (data == null)
throw new NullPointerException();
byte [] buf = Envelope.readEncByteArray(data, cipher, iv, pw);
try {
return ASN1Util.fromByteArray(buf);
} catch(ASN1Exception asn1e) {
asn1e.printStackTrace(System.out);
return null;
}
}
*/
/**
* Converts from a byte array. The byte array can be either
* base64 encoded or not, but is assumed not encrypted.
*/
public static ASN1Object fromBytes(byte [] data)
{
try
{
// return fromBytes(data, null, null, null);
// } catch(PasswordException pe) {
return ASN1Util.fromByteArray(data);
}
catch(ASN1Exception asn1e)
{
System.out.println(CBParse.bytes2Hex(data));
asn1e.printStackTrace(System.out);
return null;
}
}
/**
* Converts from a DER or PEM file which is not encrypted.
*/
public static ASN1Object fromFile(File file)
throws IOException
{
/*
ASN1Object o = null;
try { - *** Not used ***
o = fromFile(file, null);
} catch(PasswordException pe) {
if (pe.getCode() == PasswordException.PASSWORD_REQUIRED) {
throw new IllegalArgumentException("expect a passphrase");
}
}
if (o == null) { */
try
{
return ASN1Util.fromFile(file);
}
catch(ASN1Exception asn1e)
{
asn1e.printStackTrace(System.out);
return null;
}
// }
// return o;
}
/* - *** Not used ***
* Converts from a PEM file which is either encrypted or not.
* @param pw the Password object to get an encrypion password.
* @exception com.ca.commons.security.pkcs5.PasswordException if cannot get the
* password or the password is wrong.
*
public static ASN1Object fromFile(File file, Password pw)
throws IOException, PasswordException {
byte [] buf = Envelope.readEncFile(file, pw);
try {
return ASN1Util.fromByteArray(buf);
} catch(ASN1Exception asn1e) {
asn1e.printStackTrace(System.out);
return null;
}
}
*/
/* - *** Not used ***
* Converts from a String which is actually a PEM file in String form.
* The PEM file can be either encrypted or not.
* @param pw the Password object to get an encrypion password.
* @exception com.ca.commons.security.pkcs5.PasswordException if cannot get the
* password or the password is wrong.
*
public static ASN1Object fromString(String s, Password pw)
throws PasswordException {
byte [] buf = null;
try {
buf = Envelope.readEncFile(s, pw);
} catch(IOException ioe) {
return null;
}
try {
return ASN1Util.fromByteArray(buf);
} catch(ASN1Exception asn1e) {
asn1e.printStackTrace(System.out);
return null;
}
}
/** - *** Not used ***
* Converts from a String which is actually a PEM file in String form.
* The PEM file is not encrypted.
*
public static ASN1Object fromString(String s) {
try {
return fromString(s, null);
} catch(PasswordException pe) {
if (pe.getCode() == PasswordException.PASSWORD_REQUIRED) {
throw new IllegalArgumentException("expect a passphrase");
}
return null;
}
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -