用 Kerberos 为 J2ME 应用程序上锁,第 2 部分: 生成一个 Kerberos 票据请求
2010-03-30 00:00:00 来源:WEB开发网getIntegerBytes()
这个方法取一个整数( value )作为参数并返回以 ASN.1 INTEGER 表达的这个整数值。回想一下在本系列 第一篇文章 的表 1 中曾提到,在ASN.1 中 INTEGER 是一种通用数据类型。
清单 3 中显示了 getIntegerBytes() 方法的实现。
清单 3. getIntegerBytes() 方法
public byte[] getIntegerBytes (int integerContents)
{
//1. Declare a byte array named finalBytes, which will
// hold all the bytes of the ASN.1 byte array representation.
byte finalBytes[];
//2. Calculate the number of bytes required to hold the
// contents part of the ASN.1 byte array representation.
int tempValue = integerContents;
int contentBytesCount = 1;
do {
tempValue = tempValue / 256;
if (tempValue >0)
contentBytesCount ++;
} while (tempValue > 0);
//3. Use the getLengthBytes() method of Listing 3 to author
// the length bytes. Store the length bytes in an array named lengthBytes.
byte lengthBytes[] = getLengthBytes(contentBytesCount );
//4. Get the number of bytes in the lengthBytes array.
int lengthBytesCount = lengthBytes.length;
//5. Calculate the number of bytes required to hold the
// complete ASN.1 byte array representation
// (the sum total of the number of tag bytes, length bytes, and content bytes).
// Store the number of bytes in a variable named totalBytesCount.
int totalBytesCount = 1 + lengthBytesCount + contentBytesCount ;
//6. Instantiate the finalBytes array to totalBytesCount size.
finalBytes = new byte[totalBytesCount];
//7. Copy the tag byte at the start of the finalBytes array.
finalBytes[0] = (byte)0x02;
//8. Copy the length bytes from the lengthBytes array
// to the finalBytes array just after the tag byte.
for (int i=0; i < lengthBytes.length; i++)
finalBytes[i+1] = lengthBytes[i];
//9. Copy the content bytes to the finalBytes array
// just after the length bytes.
int k = totalBytesCount - lengthBytesCount - 1;
for (int j=lengthBytesCount+1; j<totalBytesCount; j++){
k--;
finalBytes[j] = (byte) (integerContents >>> (k*8) & 255);
}//for
//10. Return the finalBytes array.
return finalBytes;
}//getIntegerBytes
更多精彩
赞助商链接