【发布时间】:2016-03-22 00:01:33
【问题描述】:
我正在开发 ASN.1 加密文件的解码器,并得到 p>
java.io.IOException: DER length more than 4 bytes: 63
我想了解为什么充气城堡会抛出这个异常。
如果有人能给我他们的 2 美分,将不胜感激!
代码片段
ASN1InputStream bIn = null;
try {
byte[] bFile = encoded;
InputStream input = new ByteArrayInputStream(bFile);
bIn = new ASN1InputStream(input);
Object temp = null;
// logger.info("Decoding and emitting file : ");
System.out.println("Decoding and emitting file : ");
while ((temp = bIn.readObject()) != null){
if (temp instanceof DERTaggedObject) {
DERTaggedObject octs = (DERTaggedObject) temp;
ASN1Set instance = ASN1Set.getInstance(octs, false);
错误被抛出
bIn.readObject()
我已经跟踪到 ASN1InputStream 类 readLength() 方法的异常。 http://www.docjar.org/html/api/org/bouncycastle/asn1/ASN1InputStream.java.html
static int readLength(InputStream s, int limit)
283 throws IOException
284 {
285 int length = s.read();
286 if (length < 0)
287 {
288 throw new EOFException("EOF found when length expected");
289 }
290
291 if (length == 0x80)
292 {
293 return -1; // indefinite-length encoding
294 }
295
296 if (length > 127)
297 {
298 int size = length & 0x7f;
299
300 if (size > 4)
301 {
302 throw new IOException("DER length more than 4 bytes");
303 }
304
305 length = 0;
306 for (int i = 0; i < size; i++)
307 {
308 int next = s.read();
309
310 if (next < 0)
311 {
312 throw new EOFException("EOF found reading length");
313 }
314
315 length = (length << 8) + next;
316 }
317
318 if (length < 0)
319 {
320 throw new IOException("corrupted stream - negative length found");
321 }
322
323 if (length >= limit) // after all we must have read at least 1 byte
324 {
325 throw new IOException("corrupted stream - out of bounds length found");
326 }
327 }
328
329 return length;
330 }
非常感谢任何有助于理解为什么抛出此异常的帮助!
谢谢!
【问题讨论】:
-
代码中的错误或损坏的输入。
-
代码看起来没问题,所以我怀疑输入数据无效或损坏。
-
我有没有办法检查输入数据是否无效或损坏?
-
你能给我们一些关于数据来自哪里的信息,以及预期的格式是什么(即其中应该是什么 ASN.1 结构)。 “ASN.1 加密文件”暗示您可能正在解析解密的输出,在这种情况下,一个明显的可能性是它没有正确解密。或者,也许该文件实际上是 base64 编码或其他东西,而您已经跳过了解码步骤。
-
感谢您的回复!我试图解码的数据是由 Cisco ASR 5000 机器生成的 PGW-CDR。
标签: java exception bouncycastle asn.1 decoder