Tuesday, 17 September 2013

Basic Encryption Example

Today we'll learn how programs are used in real life. In applications related to providing security to information encryption is used, for those who don't know what is encryption it's the process of hiding your  data by changing it's form like substituting alphabet b with the number 2 and alphabet z with 26 and so on. One hidden fact about encryption is that. Many coders uses prime numbers in their algorithm wherever  possible, as prime numbers are very difficult to break. The following program gives just a basic idea about encryption.

Program-
import java.io.*;
import java.lang.*;
class Encrypt
{
void main()throws IOException
{
BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
int a;
do
{
System.out.println("Enter the number of sentences you want to insert (Less than 9)");
a=Integer.parseInt(r.readLine());
}while(a>9);
System.out.println("Enter your sentences");
String s[]=new String[a];
for(int i=0;i<a;i++)
{
s[i]=r.readLine();
}
System.out.println("Entered sentences are");
for(int i=0;i<a;i++)
{
System.out.println(s[i]);
}
int l,c;
char c1;
for(int i=0;i<a;i++)
{l=s[i].length();
if(i%2==0)
{
for(int j=0;j<l-1;j++)
{c1=s[i].charAt(j);
if(c1==32)
{
System.out.print(" ");}
if(c1!=32)
{
if(c1==121)
{
c1='a';
}else
if (c1==122)
{c1='b';}
else
if(c1==90)
c1='B';
else
if(c1==89)
c1='A';
else{
c=((int)s[i].charAt(j));
c+=2;
c1=((char)c);
}
System.out.print(c1);
}

}System.out.println(".");}

else

{
int t=0,t2=0;
for(int j=0;j<l;j++)
{if((s[i].charAt(j))==32)
t++;
}
String s1[]=new String[t+1];
int t3=0,t4=0;
for(int j=0;j<l;j++)
{if(s[i].charAt(j)==32)
{s1[t4]=s[i].substring(t3,j);

t4++;
t3=j+1;
}s1[t4]=s[i].substring(t3,l-1);
}
for(int j=t;j>=0;j--)
{System.out.print(s1[j]+" " );
}
System.out.println(".");}}}}

This is just a basic program, real life programs apply various logics and algorithms to encrypt sensitive data. It is decrypted, that is brought back to original form using the right Key. Key is nothing but list of the techniques applied to encrypt the data, those techniques are applied in reverse logic to decrypt the data.
So the key is important thing required to convert your data back into normal form.

No comments:

Post a Comment

Know Jugaad. Powered by Blogger.