Information and Network Security (INS)

Implementing Substitution Algorithm

/*
	Program to implement substitution cipher
	Author: PrascPedia		www.pracspedia.com
*/
import java.io.*;
class Substitution
{
	public static void main(String args[])throws Exception
	{
		DataInputStream in=new DataInputStream(System.in);

		System.out.println("Enter your message");
		String message=in.readLine();	//original message
		int m=message.length();	//length of message
		
		char cipher[]=new char[m];	//cipher array
		char msg[]=message.toCharArray();	//msg array
		int key[]=new int[m];
		
		for(int i=0;i < m;i++)
		key[i]=(int)msg[i];	//ascii value
		
		for(int i=0;i < m;i++)
		key[i]=(key[i]-96+3)%26+96;	//modified ascii value
		
		for(int i=0;i < m;i++)
		cipher[i]=(char)key[i];	//cipher text array

		System.out.println("Encrypted message");
		for(int i=0;i < m;i++)
		System.out.print(cipher[i]);
	}
}

								
Download Source Code Program List