Internet Checksum (Java)

Problem Definition: Write a program in Java to calculate the Internet Checksum and verify it at the Sender’s & Receiver’s side.

import java.util.Scanner;
class Checksum{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println(“**********SENDER**********”);
System.out.print(“Enter the word: “);
String input = sc.nextLine();

//Calculation of hex string
String hex[];
if(input.length()%2 == 0){ //Even length input
hex = new String[input.length()/2];
for(int i=0;i<hex.length;i++)
hex[i] = Integer.toHexString(input.charAt(2*i))+Integer.toHexString(input.charAt(2*i+1));
}else{//odd length input
hex = new String[input.length()/2 + 1];
hex[0] = “00”+Integer.toHexString(input.charAt(0)); //For 1st char append 00 at beginning
for(int i=1;i<hex.length;i++)
hex[i] = Integer.toHexString(input.charAt(2*i-1))+Integer.toHexString(input.charAt(2*i));
}
System.out.println(“The hex strings for the input are: “);
for(int i=0;i<hex.length;i++)
System.out.print(hex[i]+” “);
System.out.println();

//Calculation of checksum
String sum = “0000”;
for(int i=0;i<hex.length;i++){
int temp = Integer.parseInt(sum,16) + Integer.parseInt(hex[i],16);
sum = Integer.toHexString(temp);
}
if(sum.length()>4){//Wrapping if length of sum is > 4
int n = sum.length()-4;
int temp = Integer.parseInt(sum.substring(0,n),16) + Integer.parseInt(sum.substring(n),16);
sum = Integer.toHexString(temp);
}
int complement = Integer.parseInt(“FFFF”,16) – Integer.parseInt(sum,16); //Finding the 1s complement
sum = Integer.toHexString(complement);
System.out.println(“The checksum is: “+sum);

System.out.println(“*********RECEIVER*********”);
System.out.print(“Enter the word: “);
input = sc.nextLine();

//Calculation of hex string
if(input.length()%2 == 0){ //Even length input
hex = new String[input.length()/2];
for(int i=0;i<hex.length;i++)
hex[i] = Integer.toHexString(input.charAt(2*i))+Integer.toHexString(input.charAt(2*i+1));
}else{//odd length input
hex = new String[input.length()/2 + 1];
hex[0] = “00”+Integer.toHexString(input.charAt(0)); //For 1st char append 00 at beginning
for(int i=1;i<hex.length;i++)
hex[i] = Integer.toHexString(input.charAt(2*i-1))+Integer.toHexString(input.charAt(2*i));
}
System.out.println(“The hex strings for the input are: “);
for(int i=0;i<hex.length;i++)
System.out.print(hex[i]+” “);
System.out.println();

//Calculation of checksum
sum = “0000”;
for(int i=0;i<hex.length;i++){
int temp = Integer.parseInt(sum,16) + Integer.parseInt(hex[i],16);
sum = Integer.toHexString(temp);
}
if(sum.length()>4){//Wrapping if length of sum is > 4
int n = sum.length()-4;
int temp = Integer.parseInt(sum.substring(0,n),16) + Integer.parseInt(sum.substring(n),16);
sum = Integer.toHexString(temp);
}
complement = Integer.parseInt(“FFFF”,16) – Integer.parseInt(sum,16); //Finding the 1s complement
sum = Integer.toHexString(complement);
System.out.println(“The checksum is: “+sum);

if(sum.equals(“0”)) System.out.println(“Checksum is valid.”);
else System.out.println(“Checksum is invalid.”);
}
}
/*Output
**********SENDER**********
Enter the word: KJSCE
The hex strings for the input are:
004b 4a53 4345
The checksum is: 721c
*********RECEIVER*********
Enter the word: KJSCEOP
The hex strings for the input are:
004b 4a53 4345 4f50
The checksum is: 22cc
Checksum is invalid.
*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s