📄 packet.java
字号:
package au.net.aba.pgp;
/*
* $Id: Packet.java,v 1.2 1998/10/19 06:32:40 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.
*
* If you have not received a copy of the Public Licence, you must destroy all
* copies of this file immediately.
*
* $Source: /aba/CVSROOT/jdk1.1/src/au.net.aba/pgp/Packet.java,v $
* $Revision: 1.2 $
* $Date: 1998/10/19 06:32:40 $
* $State: Exp $
*/
import java.io.*;
//
// Generic PGP packet.
//
class Packet
{
public final static String ident = "$Id: Packet.java,v 1.2 1998/10/19 06:32:40 leachbj Exp $";
//
// Read a cipher packet header and build a data stream
// for the remainder of the packet.
//
static DataInputStream buildCipherPacketInputStream (
InputStream input, byte typeExpected, String errorMessage)
throws IOException
{
// read cipher type byte
DataInputStream data = new DataInputStream (input);
byte ctb = data.readByte ();
// sanity check
if ((ctb & 0x80) == 0){
throw new IOException ("not2 in PGP format " + ctb);
}
// extract type field
byte type = (byte)((ctb & 0x3c) >>> 2);
// check type field
if (type != typeExpected){
throw new IOException (errorMessage);
}
// read length field
long length = ~0L;
switch ((byte)(ctb & 0x03)){
case 0x00:
length = data.readByte ();
break;
case 0x01:
length = data.readUnsignedShort ();
break;
case 0x02:
length = data.readUnsignedShort () << 16;
length |= data.readUnsignedShort ();
break;
}
// length field provided?
if (length != ~0L){
// read rest of packet data
byte[] buffer = new byte[(int)length];
data.readFully (buffer);
// use buffer as packet input stream
data = new DataInputStream (
new ByteArrayInputStream (buffer));
}
return (data);
}
//
// Read a generic cipher packet.
//
public static Packet readCipherPacket (InputStream input)
throws IOException
{
// peek at cipher type byte
// pushback streams are buggy so we make do...
input.mark (1);
int ctb = input.read ();
if (ctb == -1){
throw new EOFException ();
}
input.reset ();
// sanity check
if ((ctb & 0x80) == 0){
throw new IOException ("not1 in PGP format " + ctb);
}
// extract type field
byte type = (byte)((ctb & 0x3c) >>> 2);
// parse packet
Packet packet = null;
switch (type){
case 0x02:
packet = new SignaturePacket (input);
break;
case 0x05:
packet = new SecretKeyCertificatePacket (input);
break;
case 0x06:
packet = new PublicKeyCertificatePacket (input);
break;
case 0x0c:
packet = new KeyringTrustPacket (input);
break;
case 0x0d:
packet = new UserIDPacket (input);
break;
case 0x0e:
// skip packet
buildCipherPacketInputStream (input,(byte)0x09,"panic");
break;
default:
throw new IOException (
"unrecognised PGP packet " + type);
}
return (packet);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -