我正在尝试在 Android 上使用 Bouncycastle 读取 ASN1 对象。我希望它是一个 DERSequence,在 Bouncycastle 中它是 ASN1Sequence 的一个子类,它是 ASN1Object 的一个子类。
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERSequence;
...
ASN1InputStream ais = ...;
Object o = ais.readObject();
// Eclipse's debugger now says o is a DERSequence, as expected.
DERSequence o2 = (DERSequence)o;
ASN1Sequence o3 = o2;
ASN1Object o4 = o3;
// And o4 is now exactly what I want.
ASN1Object o5 = (ASN1Object)o;
// But this throws:
/// java.lang.ClassCastException: org.bouncycastle.asn1.DERSequence
根据答案的反馈,我构建了另一个更短的示例:
Object o = new DERSequence();
ASN1Object o1 = new DERSequence(); // This behaves fine.
ASN1Object o2 = (ASN1Object)o; // Throws ClassCastException.
是什么导致这个演员失败?