Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Java Code For Password Encryption


Code ResourceAuthor: srinivasaraobora  

Difficulty Level: Intermediate

Published: 23rd Apr 2009   Read: 42145 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 

This Code is useful for password encryption

 


Sample Code
  1. PasswordService.java
  2.  
  3.  
  4.  
  5. import java.io.UnsupportedEncodingException;
  6. import java.security.MessageDigest;
  7. import java.security.NoSuchAlgorithmException;
  8. //import org.myorg.SystemUnavailableException;
  9. import sun.misc.BASE64Encoder;
  10. import sun.misc.CharacterEncoder;
  11.  
  12. public final class PasswordService
  13. {
  14.   private static PasswordService instance;
  15.  
  16.   public PasswordService()
  17.   {
  18.   }
  19.  
  20.   public synchronized String encrypt(String plaintext) throws Exception
  21.   {
  22.     MessageDigest md = null;
  23.     try
  24.     {
  25.       md = MessageDigest.getInstance("SHA"); //step 2
  26.     }
  27.     {
  28.       throw new Exception(e.getMessage());
  29.     }
  30.     try
  31.     {
  32.       md.update(plaintext.getBytes("UTF-8")); //step 3
  33.     }
  34.     {
  35.       throw new Exception(e.getMessage());
  36.     }
  37.  
  38.     byte raw[] = md.digest(); //step 4
  39.     String hash = (new BASE64Encoder()).encode(raw); //step 5
  40.     return hash; //step 6
  41.   }
  42.  
  43.   public static synchronized PasswordService getInstance() //step 1
  44.   {
  45.     if(instance == null)
  46.     {
  47.        instance = new PasswordService();
  48.     }
  49.     return instance;
  50.   }
  51. }
  52.  
  53.  
  54.  
  55.  
  56. PasswordUtil.java
  57.  
  58.  
  59.  
  60. public final class PasswordUtil
  61. {
  62.          private PasswordUtil()
  63.          {
  64.          }
  65.                 public static void main(String a[]) throws Exception
  66.                 {
  67.                  PasswordService ps=new PasswordService();
  68.                  System.out.println(ps.encrypt("srinivaa"));
  69.                  }
  70. }
  71.  
  72.    
  73.  
  74.  
  75.  
Copyright GeekInterview.com


Next Article: Credit Card Number Verification


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Comments

It is very good...and super
Comment posted by: rajkumar on 2011-04-15T14:50:18

Popular Coders

# Coder NameHits
1. srinivasaraobora215046
2. iamdvr136010
3. venki_madesh38272
4. Raju30561
5. Kiran.jakkaraju22659
6. Nidhi Saxena22006
7. parmod kumar duhan20501
8. yuvanmytri14860
9. venkat_kiran12051
10. chowsys10964

Active Coders

Refined Tags