RSA public key length read and write



 

 

Read length:

this.length = DerInputStream.getLength(i & 0xFF, paramInputStream);

 

static int getLength(int paramInt, InputStream paramInputStream)
  throws IOException
{
  int j = paramInt;    int i;
  if ((j & 0x80) == 0) {
    i = j;
  } else {
    j &= 127;



    if (j == 0)
      return -1;
    if ((j < 0) || (j > 4)) {
      throw new IOException("DerInputStream.getLength(): lengthTag=" + j + ", " + ((j < 0) ? "incorrect DER encoding." : "too big."));

    }

    for (i = 0; j > 0; --j) {
      i <<= 8;
      i += (0xFF & paramInputStream.read());
    }
  }
  return i;
}

 

Write length:

public void putLength(int paramInt)
  throws IOException
{
  if (paramInt < 128) {
    write((byte)paramInt);
  }
  else if (paramInt < 256) {
    write(-127);
    write((byte)paramInt);
  }
  else if (paramInt < 65536) {
    write(-126);
    write((byte)(paramInt >> 8));
    write((byte)paramInt);
  }
  else if (paramInt < 16777216) {
    write(-125);
    write((byte)(paramInt >> 16));
    write((byte)(paramInt >> 8));
    write((byte)paramInt);
  }
  else {
    write(-124);
    write((byte)(paramInt >> 24));
    write((byte)(paramInt >> 16));
    write((byte)(paramInt >> 8));
    write((byte)paramInt);
  }
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326566008&siteId=291194637