*/
Are you blogging on PH? Get your free blog.
*/

View Digital Envelope in Java (UDP Networking)\Server\Rsa.java

Digital Envelope v1.0

Submitted By: ParvezMI
Rating: starstarstarstarstar (Rate It)


//import java.io.*;
class Rsa
{
                public static long n,e,d;
                Rsa()
                {
                 RSAKeyGen();
                }
                //-----------------------------------------------------
                public static boolean isPrime(long n)
                {
                 boolean b=true;
                 long i;
                 for(i=2;i<n;i++)
                  if(n%i==0) { b=false; break; }
                 return b;
                }
                //-----------------------------------------------------
                public static long Power(long x,long y)
                {
                 long ans=1,i;
                 if(x==1) return x;
                 for(i=1;i<=y;i++)
                   ans*=x;
                 return(ans);
                }
                //-----------------------------------------------------
                public static String ToBin(long x)
                {
                 String ans="",ans1="";
                 int i=0;
                 while(x>=1)
                 {
                  if(x%2==1) ans=ans+"1"; else ans=ans+"0";
                  x/=2; i++;
                 }
                 if(x==1) { ans=ans+"1"; i++; }
                        for(i=ans.length()-1;i>=0;i--)
                           ans1=ans1+ans.charAt(i);
                 return(ans1);
                }
                //-----------------------------------------------------
                public static long Crypt(long x,long key,long n) // (x^y)%z //Method 2
                {
                 String B="";
                 int i;
                 B=ToBin(key);
                 long ans=1;
                 int c=1;
                 for(i=0;i<B.length();i++)
                 {
                   c=2*c;
                   ans=(ans*ans)%n;
                   if(B.charAt(i)=='1')
                   {
                        c=c+1;
                        ans=(ans*x)%n;
                   }
                 }
                 return(ans);
                }
                //-----------------------------------------------------
                void RSAKeyGen()
                {
                  /* K E Y   G E N E R A T I O N */
                 long p=1,q=2,n1,x;
                 // Reading 2 random prime-numbers from 20 to 50
                                 while(true)
                                 {
                                         p=(long)(Math.floor((Math.random())*150))+2;
                                         q=(long)(Math.floor((Math.random())*150))+2;
                                         if(p>2 && q>2 && p!=q && isPrime(p) && isPrime(q))
                                         {
                                                                 n = p * q;
                                                                n1 = (p-1) * (q-1);
                                                                x=n1+1;
                                                                 for(e=2;e<=x/2;e++)
                                                                 {
                                                                    if(x%e==0) break;
                                                                 }
                                                                  d=x/e;
                                       if(e!=1 && d!=1 && e!=d && q!=e && q!=d && p!=e && p!=d) break;
                                         }

                                 }
                      /*           System.out.println("p             = "+p);
                                         System.out.println("q             = "+q);
                                         System.out.println("n =(p*q)      = "+n);
                                         System.out.println("n1=(p-1)*(q-1)= "+n1);
                                         System.out.println("e        = "+e);
                                         System.out.println("d        = "+d);
                                         System.out.println("Public Key  : { "+e+","+n+" }");
                                         System.out.println("Private Key : { "+d+","+n+" }");*/

                } //keygen function ends here
}//RSA class ends here
/*
public class Rsa1
{
                public static void main(String []args)
                {
                                 long P1=127,P2,Cypher,i;
                                 Rsa rs=new Rsa();
                                         Cypher=rs.Crypt(P1,rs.e,rs.n);  //Encrypting using public-key  'e'
                                         P2=rs.Crypt(Cypher,rs.d,rs.n);//Decrypting using private key 'd'

                                         System.out.println("PlainText Before Encryption is : "+P1);
                                         System.out.println("Cypher is : "+Cypher);
                                         System.out.println("PlainText After Decryption is  : "+P2);
                                         System.out.println("----------------------------------------");
                }// main ends here
}//"Rsa1.java" Class Ends here
*/

corner
© 1996-2008 CommunityHeaven LLC. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
North American business development: Nicolai Wadstrom. Publisher: Lars Hagelin.