用Kerberos为J2ME应用程序上锁,第2部分 - 生成一个Kerberos票据请求 - 编程入门网
// (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
用Kerberos为J2ME应用程序上锁,第2部分 - 生成一个Kerberos票据请求(4)时间:2011-08-05 IBM Faheem Khan这个方法首先声明一个名为 finalBytes 的字节数组。这个字节数组包含 INTEGER 数据类型结构的所 有字节。不过,我还不知道 finalBytes 数组的大小。我首先需要计算 INTEGER 结构中的字节数,这种 计算由几步组成: 第一步是计算容纳这个整数值( INTEGER 结构的内容部分)所需要的字节数。为此,我使用了一个 do - while 循环,它不断地将 value 整数除以 256,直到得到的值小于1。当这个循环退出时,容纳内 容部分所需要的字节数就储存在一个名为 contentBytesCount 的变量中。 这个方法再将所需要的长度作为一个整数传递给 getLengthBytes() 方法,这个方法返回以 ASN.1 表 达的长度字节。我将长度字节数储存到一个名为 lengthBytesCount 的变量中。 回想一下在 本系列第一篇文章中讨论过,所有 ASN.1 数据类型表达的字节数组都包含三个部分:标 签字节、长度字节和内容字节。因此,ASN.1 字节数组表达需要包含所有这三部分的足够空间。 下一步是计算将要包含 INTEGER 结构的所有字节的数组的大小。我是通过将标签字节长度(对于 INTEGER 和所有其他在 Kerberos 中使用的标签来说是 1)、长度字节数和内容字节数相加进行这种计算 的。 int totalBytesCount = 1 + lengthBytesCount + contentBytesCount; 一行进行的就是这种计算 ,并将所需要的字节数储存到一个名为 totalBytesCount 的变量中。 下面,我实例化一个大小为 totalBytesCount 的字节数组 finalBytes 。过程的其余部分很简单,我 将标签字节(对于 INTEGER 来说是 0x02 )储存到 finalBytes 数组的开始处。然后,将长度字节拷贝 到 finalBytes 数组中标签字节后面。最后,我将内容字节拷贝到长度字节后并返回 finalBytes 数组。 getGeneralStringBytes()、getOctetStringBytes()、getBitStringBytes() 和 getGeneralizedTimeBytes() 像 getIntegerBytes() 一样,每一个方法返回一种 ASN.1 通用数据类型结构。 清单 4 中的 getGeneralStringBytes() 方法生成一个 ASN.1 GeneralString 的字 |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |