资源简介
用java实现的网际校验和算法,详情请查阅博主关于网际校验和算法的博客:https://blog.csdn.net/qq_41664447/article/details/103450083
代码片段和文件信息
package internet;
public class CheckData {
public static void check(String[] strings int type) {
switch (type) {
case 1:ICMPData(strings);break;
case 6:TCPData(strings);break;
default: System.out.println(“数据类型不符合要求“);break;
}
}
private static void ICMPData(String[] strings) {
//确定ICMP开始部分
int IpLenOfHead = transformToTen(strings[14] 2);
System.out.println(“IpLen: “ + IpLenOfHead);
}
private static void TCPData(String[] strings) {
//确定ICMP开始部分
int IpLenOfHead = transformToTen(strings[14] 1) * 4;
System.out.println(“IP数据报首部长度: “ + IpLenOfHead + “Bytes“);
int TCPLenOfHead = transformToTen(strings[14 + IpLenOfHead + 12] 2) * 4;
System.out.println(“TCP首部长度: “ + TCPLenOfHead + “Bytes“);
int TCPLenOfBody = strings.length - IpLenOfHead - TCPLenOfHead - 14;
System.out.println(“TCP数据部分长度:“ + TCPLenOfBody);
//校验
/*
* (1)十二字节伪首部
* (2)TCP首部
* (3)TCP数据部分
*/
String dummyHead = ““;
for(int i = 26; i < 34; i ++) {
dummyHead += strings[i] + “ “;
}
dummyHead += “00“ + “ “;
dummyHead += “06“ + “ “;
dummyHead += “00“ + “ “;
int temp = TCPLenOfBody + TCPLenOfHead;
dummyHead += temp + “ “;
//追加TCP首部和数据部分到dummyHead
for(int i = 14 + IpLenOfHead; i < strings.length; i ++) {
dummyHead += strings[i] + “ “;
}
//运算检验
boolean b = verify(dummyHead);
}
private static boolean verify(String s) {
System.out.println(“\n校验和序列:“);
String []strings = s.split(“ “);
for(int i = 0; i < strings.length; i ++) {
if(i % 2 == 0 && i != 0) {
System.out.println();
}
System.out.print(strings[i] + “ “);
}
//提取最后一列
int sum1 = 0;//最右边
int sum2 = 0;
int sum3 = 0;
int sum4 = 0;
for(int i = 0; i < strings.length; i ++) {
if(i % 2 != 0) {
sum1 += transformToTen(strings[i] 1);
sum2 += transformToTen(strings[i] 2);
} else {
sum3 += transformToTen(strings[i] 1);
sum4 += transformToTen(strings[i] 2);
}
}
System.out.println();
System.out.println(sum4 + “ “ + sum3 + “ “ + sum2 + “ “ + sum1);
//转换为16进制的高低位
int high1 = sum1 / 16;
int low1 = sum1 % 16;
sum2 += high1;
int high2 = sum2 / 16;
int low2 = sum2 % 16;
sum3 += high2;
int high3 = sum3 / 16;
int low3 = sum3 % 16;
sum4 += high3;
int high4 = sum4 / 16;
int low4 = sum4 % 16;
low1 += high4;
low2 += low1 / 16;
low1 = low1 % 16;
low3 += low2 / 16;
low2 = low2 % 16;
low4 += low3 / 16;
low3 = low3 % 16;
//low4不会再大于等于16的
System.out.println(high4 + “ “ + high3 + “ “ + high2 + “ “ + high1);
System.out.println(“answer: “ + low4 + “ “ + low3 + “ “ + low2 + “ “ + low1);
return true;
}
//转换十六进制为十进制
private static int transformToTen(String string int type) {
char[] count = string.toCharArray();
int low = count[1] - 48;
if(low > 9) {
low = count[1] - 87;
}
int high = count[0] - 48;
if(high > 9) {
high = cou
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
.CA.... 0 2019-12-08 19:46 网际实验\.me
.CA.... 1091 2019-12-08 19:46 网际实验\.me
.CA.... 561 2019-12-08 19:47 网际实验\.me
.CA.... 1405 2019-12-08 20:06 网际实验\.me
.CA.... 667 2019-12-08 20:06 网际实验\.me
.CA.... 3381 2019-12-08 20:07 网际实验\.me
.CA.... 3382 2019-12-08 20:06 网际实验\.me
.CA.... 1404 2019-12-08 20:07 网际实验\.me
.CA.... 364 2019-12-08 20:05 网际实验\.me
.CA.... 70 2019-12-08 20:07 网际实验\.me
.CA.... 215 2019-12-08 22:42 网际实验\.me
.CA.... 1475 2019-12-08 22:42 网际实验\.me
.CA.... 411348 2019-12-08 22:42 网际实验\.me
.CA.... 1 2019-12-08 22:43 网际实验\.me
.CA.... 203 2019-12-08 22:43 网际实验\.me
.CA.... 1 2019-12-08 22:43 网际实验\.me
.CA.... 884 2019-12-08 22:42 网际实验\.me
.CA.... 1386 2019-12-08 22:42 网际实验\.me
.CA.... 42 2019-12-08 20:05 网际实验\.me
.CA.... 201 2019-12-08 21:58 网际实验\.me
.CA.... 423 2019-12-08 20:06 网际实验\.me
.CA.... 107 2019-12-08 20:06 网际实验\.me
.CA.... 439 2019-12-08 22:42 网际实验\.me
.CA.... 578 2019-12-08 20:06 网际实验\.me
.CA.... 89 2019-12-08 19:46 网际实验\.me
.CA.... 58 2019-12-08 19:46 网际实验\.me
.CA.... 73 2019-12-08 22:42 网际实验\.me
.CA.... 62 2019-12-08 19:46 网际实验\.me
.CA.... 97 2019-12-08 19:46 网际实验\.me
.CA.... 194 2019-12-08 22:42 网际实验\.me
............此处省略136个文件信息
评论
共有 条评论