Hi,
 I am attaching the code to encrypt a password & to decrypt the same....

Regards,
aruniima

____________________________________________________________________________
_____________________________________________
import java.util.*;

/*
 *The encryption logic is also available in this class(commented) for
 *reference and changes.
 */
public class Encryptor
{
            private static char inc='0';
            private static char dec='1';

            public static String decryptPass(String pass) throws
IllegalArgumentException
            {
                        char[] chars = pass.toCharArray();
                        if((chars.length-1)%2 != 0)
                        {
                                    throw new
IllegalArgumentException("Incorrect password");
                        }
                        char[] result = new char[(chars.length-1)/2];
                        Character eo = new Character(chars[0]);
                        if(!Character.isDigit(chars[0]))
                        {
                                    throw new
IllegalArgumentException("Incorrect password");
                        }
                        int evodd = Integer.parseInt(eo.toString());
                        Character thisChar;
                        for(int i=1; i<chars.length; i+=2)
                        {
                                    char incdec = chars[i];
                                    thisChar = new Character(chars[i+1]);
                                    Character comparisonChar = null;
                                    int val=0;

                                    if( incdec!=inc && incdec!=dec )
                                    {
                                                throw new
IllegalArgumentException("Password format incorrect");
                                    }
                                    if(
!Character.isLetterOrDigit(chars[i+1]) )
                                    {
                                                throw new
IllegalArgumentException("The password can only contain letters and digits :
"+pass);
                                    }

                                    if( (i/2)%2 == evodd%2 )
                                    {
                                                if( incdec == inc )
                                                            result[(i-1)/2]
= (char)(chars[i+1] - 2);
                                                else
                                                            result[(i-1)/2]
= (char)(chars[i+1] + 2);
                                    }
                                    else
                                    {
                                                if( incdec == inc )
                                                            result[(i-1)/2]
= (char)(chars[i+1] - 1);
                                                else
                                                            result[(i-1)/2]
= (char)(chars[i+1] + 1);
                                    }
                        }
                        return new String(result);
            }//end of method decryptPass
/*
            //Encyption logic:
            //1. generate a random number between 0 and 9(inclusive)
            //2. Set this to be the first character of the new string
            //3. For every input character:
            //3.a. Check if it is a digit, uppercase char or lowercase char
            //3.b. Assign a comparisonChar based on this check
            //                      '5' for digits
            //                      'M' for uppercase chars
            //                      'm' for lowercase chars
            //3.c. Compare the input character with the appropriate
comparison char
            //3.d. If it is less, set the next char in encrypted string to
'0' else set to '1'.
            //3.e. If the index of this char mod 2 equals the random number
mod 2,
            //                      increment the char by two if char added
was '0'.
            //                      decrement the char by two if char added
was '1'.
            //             else
            //                      increment the char by one if char added
was '0'.
            //                      decrement the char by one if char added
was '1'.
            //3.f. Add this modified char to the encrypted string.
            */

            public static String encryptPass(String pass) throws
IllegalArgumentException
            {
                        char[] chars = pass.toCharArray();
                        char[] result = new char[chars.length*2+1];
                        int evodd = (new Random()).nextInt(10);
                        result[0] =
Integer.toString(evodd).charAt(0);//(char)evodd;
                        Character thisChar;
                        for(int i=0; i<chars.length; i++)
                        {
                                    thisChar = new Character(chars[i]);
                                    Character comparisonChar = null;
                                    if( Character.isDigit(chars[i]) )
                                    {
                                                comparisonChar = new
Character('5');
                                    }//end of if( thisChar.isDigit() )
                                    else if( Character.isLetter(chars[i]) &&
Character.isUpperCase(chars[i]) )
                                    {
                                                comparisonChar = new
Character('M');
                                    }//end of if( thisChar.isDigit() )
                                    else if( Character.isLetter(chars[i]) &&
Character.isLowerCase(chars[i]) )
                                    {
                                                comparisonChar = new
Character('m');
                                    }//end of if( thisChar.isDigit() )
                                    else
                                    {
                                                throw new
IllegalArgumentException("The password can only contain letters and digits :
"+pass);
                                    }
                                    if( i%2 == evodd%2 )
                                    {
                                                if(
thisChar.compareTo(comparisonChar) < 0 )
                                                {
                                                            result[i*2+1] =
inc;
                                                            result[i*2+2] =
(char)(chars[i] + 2);
                                                }
                                                else
                                                {
                                                            result[i*2+1] =
dec;
                                                            result[i*2+2] =
(char)(chars[i] - 2);
                                                }
                                    }
                                    else
                                    {
                                                if(
thisChar.compareTo(comparisonChar) < 0 )
                                                {
                                                            result[i*2+1] =
inc;
                                                            result[i*2+2] =
(char)(chars[i] + 1);
                                                }
                                                else
                                                {
                                                            result[i*2+1] =
dec;
                                                            result[i*2+2] =
(char)(chars[i] - 1);
                                                }
                                    }

                        }
                        return new String(result);
            }

            /*
                        the password received & is checked with the one
stored in the database
                        The one in the database is encrypted.
                        Every encryption generates a different key for the
same letter hence
                        The method receives 2 arguments, one is simple
String from the user &
                        the other is a encrypted String stored in the
database
                        The encryption is done when storing the password in
the
                        database using the method "encryptPass(String)"

            */

            public static boolean comparePass(String pswd, String dbpswd)
            {
                        boolean flag = false;
                        try
                        {
                                    String enpass = decryptPass(dbpswd);

                                    if(enpass.equals(pswd))
                                    {
                                                flag = true;
                                    }
                                    else
                                    {
                                                flag = false;
                                    }
                        }
                        catch(Exception e)
                        {
                                    System.out.println(e);
                        }
                        finally
                        {
                                    return flag;
                        }
            }
____________________________________________________________________________
_____________________________________________


-----Original Message-----
From: srinivas tadikonda [mailto:[EMAIL PROTECTED]]
Sent: Thursday, July 04, 2002 2:08 PM
To: [EMAIL PROTECTED]
Subject: Password encryption

Hi,
 How can I do password encryption and decryption. Please sned code for
encryption and decryption.
Regards
Srinivas






   _____

MSN Photos is the easiest way to share and print your photos: Click Here
<http://g.msn.com/1HM1ENIN/c156??PI=44344>
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set
JSP-INTEREST DIGEST". Some relevant FAQs on JSP/Servlets can be found at:
http://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp http://www.jspinsider.com
----------------------------------------------------------------------------

This message contains privileged and confidential information and is
intended only for the individual named. If you are not the intended
recepient you should not disseminate, distribute, store, print, copy or
deliver this message. Please notify the sender immediately by e-mail if you
have received this e-mail by mistake and immediately delete this e-mail from
your system.


E-mail transmission cannot be guaranteed to be secure or error-free as
information could be intercepted, corrupted, lost, destroyed, arrive late or
incomplete, or contain viruses. The sender therefore does not accept
liability for any errors or omissions in the contents of this message which
arise as a result of e-mail transmission.  If verification is required
please request a hard-copy version.


--------------------------------------------------------------------------

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to