Tuesday, 27 November 2012

Knapsack Cryptosystem: Decryption (Java)


Knapsack Cryptosystem: Decryption


import java.util.*;
class KSD
{
public int inverse(int a,int n)
{
int r1,r2,r,q,t1,t2,t;
r1=n;r2=a;
t1=0;t2=1;
while(r2>0)
{
q=r1/r2;
   r=r1-q*r2;
   t=t1-q*t2;

   r1=r2;r2=r;
   t1=t2;t2=t;
}
 if(r1==1)
  return t1;
 else
  return 0;
}
public void invKnapSack(int a[],int s,int p[])
{
int s2,sum=0;
int x[]=new int[7];
int t[]=new int[7];

System.out.println("\n\n i\ta\ts\tx\ts\n");
for(int h=6;h>=0;h--)
{
if(s>=a[h])
{
x[h]=1;
}
s2=s-(a[h]*x[h]);
System.out.println(h+"\t"+a[h]+"\t"+s+"\t"+x[h]+"\t"+s2);
s=s2;
}

/*for(int h=0;h<7;h++)
System.out.print(x[h]);*/
for(int h=0;h<7;h++)
t[h]=x[p[h]-1];
System.out.println();
for(int h=0;h<7;h++)
{
int temp;
if(t[h]==1)
{
temp=(int)(Math.round((Math.pow(2,7-h-1))));
sum+=temp;
}
}
char ch=(char)sum;
System.out.println("The plain text is: "+ch);
}
}
class KnapSackDecrypt
{
public static void main(String args[])
{
int b,n,r;
int a[]=new int[7];
int p[]=new int[7];
Scanner se=new Scanner(System.in);
System.out.println("Enter the super increasing tuple: ");
for(int h=0;h<7;h++)
a[h]=se.nextInt();
System.out.println("Enter the permutation tuple: ");
for(int h=0;h<7;h++)
p[h]=se.nextInt();
System.out.println("Enter the cipher text (sum) : ");
b=se.nextInt();
se.nextLine();
System.out.println("Enter the N: ");
n=se.nextInt();
se.nextLine();
System.out.println("Enter the R: ");
r=se.nextInt();
//System.out.println(b);
KSD ksd=new KSD();
int s=ksd.inverse(r,n);
s*=b;
s%=n;
System.out.println(s);
ksd.invKnapSack(a,s,p);
}

}

Download the code from here

Multiplicative Cipher (C++)

Multiplicative Cipher : Encryption

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
class MulCiph
{
  public:
  char str[100];
  int key;
  void getkey();
  void gettext();
  void convert();
};
void MulCiph :: gettext()
{
  cout<<"\nEnter the string: ";
  cin>>str;
}
void MulCiph :: getkey()
{
  cout<<"\nEnter the multiplicative cipher key: ";
  cin>>key;
}
void MulCiph :: convert()
{
  int h=strlen(str);
  for(int i=0;i<h;i++)
  {
    char c=(((toascii(str[i])-97)*key)%26)+97;
    cout<<str[i]<<"\t"<<((toascii(str[i])-97)*key)%26<<"\t"<<c<<endl;
  }
}
void main()
{
  clrscr();
  MulCiph a;
  a.gettext();
  a.getkey();
  a.convert();
  getch();
}

Download the code from here

Additive Cipher (C++)


Encryption using Additive Cipher in C++


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
class AddCiph
{
  public:
  char str[100];
  int key;
  void getkey();
  void gettext();
  void convert();
};
void AddCiph :: gettext()
{
  cout<<"\nEnter the plain string: ";
  cin>>str;
}
void AddCiph :: getkey()
{
  cout<<"\nEnter the additive cipher key: ";
  cin>>key;
}
void AddCiph :: convert()
{
  int h=strlen(str);
  for(int i=0;i<h;i++)
  {
    char c=(((toascii(str[i])-97)+key)%26)+97;
    cout<<str[i]<<"\t"<<((toascii(str[i])-97)+key)%26<<"\t"<<c<<endl;
  }
}
void main()
{
  clrscr();
  AddCiph a;
  a.gettext();
  a.getkey();
  a.convert();
  getch();
}

Download code from here


How to use clrscr() in Netbeans

How to use clrscr() in NetBeans



This is specially for NetBeans users. Sometimes you might be facing some problems when you have to use "clrscr()" in C/C++ in NetBeans.
The reason is the NetBeans IDE does not supports conio.h header file and "clrscr()" is included in that library.
So this is the how you can use clrscr() alternate and clear the screen.

IPv6 internet addressing now available in India

Indian Registry for Internet Names and Numbers (IRINN) has started issuing next version of internet addresses 'IPv6', which would make it easy for security agencies to identify each internet user. 
The internet addresses under the present version IPv4 (Internet Protocol version 4), are limited and service providers often assign single IP address to many users, making it difficult to identify the end user. 
"The number of IPv6 addresses available is enormous. ISPs (Internet Service Providers) can allocate an IP address to their users. People can be easily identified if they are using IPv6," APNIC Director (Services and Operations) Sanjaya said at the roadshow for new version IPv6 here today. 
APNIC, which is one of the five authorised bodies for issuing internet addresses, has recognised Indian Registry for Internet Names and Numbers (IRINN) for issuing IP addresses in India. 
"We are issuing IPv6 addresses at up to 60 per cent less than prevailing rates in the ongoing soft launch period. This is to test compatibility of hardware and softwares that has to be in place. In next couple of months we will launch industrial grade of IPv6," National Internet Exchange of India (NIXI) Chief Executive Govind said. 
IRINN has been set-up under the state-run NIXI. On new version IPv6, Internet Service Providers Association of India President Rajesh Charia said that the new addresses will be multiple times cheaper for companies than IPv4 addresses. 
IRINN is issuing initial set-of IPv6 addresses in price range starting at Rs 21,999 compared to prevalent rate of around Rs 66,000 in Asia Pacific region. 
On addressing security issues with the help of IPv6, Charia said, "Government will have to ensure that the new equipment and devices that are produced or imported in the country are at least IPv6 enabled." 
At present, there is no import restriction on devices and equipment that do not comply with IPv6 standards.


Thursday, 8 November 2012

Knapsack Cryptosystem: Encryption (Java)


import java.util.*;
class KnapSack
{
public void calculateKnapSack(char ch,int b[],int p[],int n,int r)
{
int pt=ch;
int sum=0;
String str=Integer.toBinaryString(pt);
int t[]=new int[7];
int a[]=new int[7];
int x[]=new int[7];
for(int h=0;h<7;h++)
t[h]=(b[h]*r)%n;
System.out.print("\nTemparory Tuple:\t\t\t");
for(int h=0;h<7;h++)
System.out.print(t[h]+",");
for(int h=0;h<7;h++)
a[h]=t[p[h]-1];
System.out.print("\nAfter Permuting Temporary Tuple:\t");
for(int h=0;h<7;h++)
System.out.print(a[h]+",");
for(int h=0;h<7;h++)
x[h]=str.charAt(h)-48;
System.out.print("\nAfter (b*n)%r:\t\t\t\t");
for(int h=0;h<7;h++)
System.out.print(a[h]*x[h]+",");
for(int h=0;h<7;h++)
sum+=a[h]*x[h];
System.out.println("\n\nThe Sum and the Cipher Text is:"+sum);
System.out.println();
}
public static void main(String args[])
{
String ch;
Scanner se=new Scanner(System.in);
System.out.println("\nEnter the plain text: ");
ch=se.nextLine();
char pt=ch.charAt(0);
int b[]=new int[7];
int p[]=new int[7];
System.out.print("Enter the value of N: \n");
int n=se.nextInt();
System.out.print("Enter the superincreasing tuple upto 7: \n");
for(int i=0;i<7;i++)
b[i]=se.nextInt();
System.out.print("Enter the permutation tuple upto 7: \n");
for(int i=0;i<7;i++)
p[i]=se.nextInt();
KnapSack l=new KnapSack();
l.calculateKnapSack(pt,b,p,n,41);
}
}



Download the code from here

GCD using Euclidian Method (C++)


#include<iostream.h>
#include<conio.h>
class GCD
{
  public:
    int a,b,q,r,tempa,tempb;
    void getdata();
    void calculate();
};
void GCD :: getdata()
{
  cout<<"\nEnter the value of A: ";
  cin>>a;
  tempa=a;
  cout<<"\nEnter the value of B: ";
  cin>>b;
  tempb=b;
}
void GCD :: calculate()
{
  while(tempb>0)
  {
    q=tempa/tempb;
    r=tempa-(q*tempb);
    cout<<q<<"\t"<<tempa<<"\t"<<tempb<<"\t"<<r<<"\n";
    tempa=tempb;
    tempb=r;
  }
  cout<<"GCD("<<a<<","<<b<<") is"<<tempa<<"\n";
}
void main()
{
  clrscr();
  GCD g;
  g.getdata();
  g.calculate();
  getch();
}


Download the CPP file from here