2uzhan.com
Advertisement
Now Place:2uzhan.com » [RESOLVED] Encryption Decryption

[RESOLVED] Encryption Decryption

Java Programming @ December 2, 2006   Views:0

pls help me!!
how do u break the loop after the program has found all 6 passwords????

Code:

import java.io.*;
import java.util.*;
import java.text.DateFormat;

public class com_security_***1 {

  /*Applies the fast modular exponentiation method of the subject guide*/
        public static long fastmodexp(long g, long a, long p)
        {
                long y=1;
                long u=g%p;
                do
                {
                        if(a%2==1) y=(y*u)%p;
                        a=(int)Math.floor(a/2);
                        u=(u*u)%p;
                }while(a!=0);
                return y;
        }

        /*Converts each string into a unique number which is the input for the one-way function*/
        static long associate(String t)
                {
                  long num=0;
                  char[] chars = t.toCharArray();
                  for (int i=0;i<t.length();i++)
                  {
                    num=(num*100)+((int)((chars[i]%100)));
                  }
                  return num;
                }

                /*Encrypts the password by first converting it into an integer value num
                  and then using the one-way function 2^n mod 922526197 */
                static long Encrypt(String p) throws IOException
                {
                  BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
                  long prime = 922526197;
                  long g = 2;
                  long num=associate(p);
                  //applies the one-way function to encrypt the password
                  long encrypt_word = fastmodexp(g,num,prime);
                  return(encrypt_word);
                }

                /*Checks input passwords with passwords in file*/
                static boolean check_password(String t) throws IOException
                {
                  int[] pswdfile;
                  int no_of_passwords=6;
                  boolean found=false;
                  //This is the password file
                  pswdfile=new int[no_of_passwords];
                  pswdfile[0]=328310406;
                  pswdfile[1]=312771985;
                  pswdfile[2]=731343680;
                  pswdfile[3]=519808427;
                  pswdfile[4]=534288373;
                  pswdfile[5]=504933131;
                  long encrypted_t=Encrypt(t);
                  for(int i=0;i<no_of_passwords;i++)
                  {

                    if(encrypted_t==pswdfile[i])
                    {
                                System.out.println(t+" is the password of user number "+(i+1));
                                System.out.println("Search successful at: "+getCurrentTimestamp());
                                found=true;

                    }

                  }
                  return found;
                }

                /*returns the current time and date*/
            public static String getCurrentTimestamp()
            {
            //get current locale settings
            DateFormat df = DateFormat.getDateTimeInstance();
            //sets dNow to the current date and time
            Date dNow = new Date();
            return df.format(dNow);
          }

    /*This is the main program.*/
  public static void main(String[] args) throws IOException
  {

    int first;
    int second;
    int third;
    int fourth;

    char[] chars = new char[62];
    chars[0] = 'a'; chars[26] = 'A'; chars[52] = '1';
    chars[1] = 'b'; chars[27] = 'B'; chars[53] = '2';
    chars[2] = 'c'; chars[28] = 'C'; chars[54] = '3';
    chars[3] = 'd'; chars[29] = 'D'; chars[55] = '4';
    chars[4] = 'e'; chars[30] = 'E'; chars[56] = '5';
    chars[5] = 'f'; chars[31] = 'F'; chars[57] = '6';
    chars[6] = 'g'; chars[32] = 'G'; chars[58] = '7';
    chars[7] = 'h'; chars[33] = 'H'; chars[59] = '8';
    chars[8] = 'i'; chars[34] = 'I'; chars[60] = '9';
    chars[9] = 'j'; chars[35] = 'J'; chars[61] = '0';
    chars[10] = 'k'; chars[36] = 'K';
    chars[11] = 'l'; chars[37] = 'L';
    chars[12] = 'm'; chars[38] = 'M';
    chars[13] = 'n'; chars[39] = 'N';
    chars[14] = 'o'; chars[40] = 'O';
    chars[15] = 'p'; chars[41] = 'P';
    chars[16] = 'q'; chars[42] = 'Q';
    chars[17] = 'r'; chars[43] = 'R';
    chars[18] = 's'; chars[44] = 'S';
    chars[19] = 't'; chars[45] = 'T';
    chars[20] = 'u'; chars[46] = 'U';
    chars[21] = 'v'; chars[47] = 'V';
    chars[22] = 'w'; chars[48] = 'W';
    chars[23] = 'x'; chars[49] = 'X';
    chars[24] = 'y'; chars[50] = 'Y';
    chars[25] = 'z'; chars[51] = 'Z';

  for (first=0; first<chars.length; first++)
    {
      for (second=0; second<chars.length; second++)
      {
        for (third=0; third<chars.length; third++)
        {
           for (fourth=0; fourth<chars.length; fourth++)
           {
          String random_pw = "" + chars[first] + chars[second] + chars[third] + chars[fourth];
          check_password(random_pw);
           }
        }
      }
    }

  }

}

--------------Solutions-------------

Isn't the point of encrypting passwords that you should never decrypt them? You store the encrypted version and compare it with the similarly encrypted versions of passwords entered for validation. That way only the password originator ever knows the unencrypted password.

From a programmer's point of view, the user is a peripheral that types when you issue a read request...
P. Williams

Tags:
© 2018 2uzhan.com Contact