Solved

How to create a SHA1, base64 encoding password in java?

Posted on 2008-10-13
1
4,340 Views
Last Modified: 2008-10-23
I am trying to create a password that is first converted to uppercase, then Unicode it in little-endian UTF16,
then SHA1 it, then base64 encode it.  I will be displaying the password as a string.

Currently, I am getting output that looks like I have done it correctly, but am still unable to connect via https and it returns an invalid username/password..  So, I wanted to ensure my password encryption was correct.  I know Java doesn't support base64 encoding in the standard API, but found a routine that is suppose to do the trick.  See the attached code and let me know if this is correct.




static final String baseTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

 

// encrypt password

            String sPassword = "AVI5580!";

            byte[] unicodeValue = sPassword.getBytes( "utf-16" );

            md = MessageDigest.getInstance( "SHA1" );

            md.update( unicodeValue );

            byte[] encodedPassword = md.digest();

            String encodedString = encode( encodedPassword );

 

 

// encode method

 public static String encode( byte[] bytes )

    {

        StringBuffer tmp = new StringBuffer();

        int i = 0;

        byte pos;

        for (i = 0; i < bytes.length - bytes.length % 3; i += 3) {

            pos = (byte) (bytes[i] >> 2 & 63);

            tmp.append( baseTable.charAt( pos ) );

            pos = (byte) (((bytes[i] & 3) << 4) + (bytes[i + 1] >> 4 & 15));

            tmp.append( baseTable.charAt( pos ) );

            pos = (byte) (((bytes[i + 1] & 15) << 2) + (bytes[i + 2] >> 6 & 3));

            tmp.append( baseTable.charAt( pos ) );

            pos = (byte) (bytes[i + 2] & 63);

            tmp.append( baseTable.charAt( pos ) );

            // Add a new line for each 76 chars.

            // 76*3/4 = 57

            if ((i + 2) % 56 == 0) {

                tmp.append( "\r\n" );

            }

        }

        if (bytes.length % 3 != 0) {

            if (bytes.length % 3 == 2) {

                pos = (byte) (bytes[i] >> 2 & 63);

                tmp.append( baseTable.charAt( pos ) );

                pos = (byte) (((bytes[i] & 3) << 4) + (bytes[i + 1] >> 4 & 15));

                tmp.append( baseTable.charAt( pos ) );

                pos = (byte) ((bytes[i + 1] & 15) << 2);

                tmp.append( baseTable.charAt( pos ) );

                tmp.append( "=" );

            } else if (bytes.length % 3 == 1) {

                pos = (byte) (bytes[i] >> 2 & 63);

                tmp.append( baseTable.charAt( pos ) );

                pos = (byte) ((bytes[i] & 3) << 4);

                tmp.append( baseTable.charAt( pos ) );

                tmp.append( "==" );

            }

        }

        return tmp.toString();

    }

Open in new window

0
Comment
Question by:cindymccartney
1 Comment
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 500 total points
ID: 22722684
You can use commons-codec library to encode it http://commons.apache.org/codec
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hibernate Not Rolling Back When something fails in a stored procedure 2 50
Securing Jmx Console and web console 2 64
Java Loop 6 48
Arrays.asList  VS  ArrayList 4 44
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now