Friday 8 July 2011

Generating Random Passwords to Different users.

Hai friends,

I explored on 'How to Generate Passwords Randomly for different users'.

In Apex , we have Crypto class to support encrypt and decrypt mechanism.

Here is the simple logic to generate AES key using crypto class . This key usually uses to encrypt and decrypt the data .

                           Blob blobKey = crypto.generateAesKey(128);
                           String key = EncodingUtil.convertToHex(blobKey);
                           System.debug(key);

Below is the code to generate AES key and sending email to entered email address . 
Controller:-

  public class PasswordGenerator 
{
        public String comments { get; set; }
        public String EmployeeNumber { get; set; }
        public String company { get; set; }
        public String Qualification { get; set; }
        public String Email { get; set; }
        public String Name { get; set; }
         string strPassword ='';
       public List<SelectOption> getQual() {
        List<SelectOption> options = new List<SelectOption>();
       options.add(new SelectOption('--NONE--','------NONE-----'));
        options.add(new SelectOption('MBA','MBA'));
        options.add(new SelectOption('MCA','MCA'));
        options.add(new SelectOption('DEGREE','DEGREE'));
        options.add(new SelectOption('INTER','INTER'));
        options.add(new SelectOption('SCHOOLING','SCHOOLING'));
        return options;
        }
    public static String getPassword(Integer len)
     {
        Blob blobKey = crypto.generateAesKey(128);
        String key = EncodingUtil.convertToHex(blobKey);
        System.debug(key);
        return key.substring(0,len);
     }
    
     public pagereference save()
      {
        String password =PasswordGenerator.getPassword(12);
        Blog_Members__c obj=new Blog_Members__c();
        obj.Name=Name;
        obj.company__c=company ;
        obj.Email__c=Email;
        obj.Employee_Number__c=EmployeeNumber ;
        obj.Comments__c=comments;
        obj.Qualification__c=Qualification ;    
        obj.Password__c = password;
        insert obj;
        sendpassword(Email,password);
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Login Details has been mail to your Registered mail Address .')); 
         company ='';
         Qualification ='';
         EmployeeNumber ='';
         comments ='';
         Email ='';
         Name ='';
         return null; 
      }
      
      public void sendpassword(String Email,String Password)
      {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[]{Email}; 
        String[] ccAddresses = new String[] {'haribabu.amudalapalli@gmail.com'};
        String Password1 =Password;
        System.debug('hari Testing'+Password1+'Email address'+toAddresses );
        mail.setToAddresses(toAddresses);
        mail.setCcAddresses(ccAddresses);
        mail.setReplyTo('support@acme.com');
        mail.setSenderDisplayName('Salesforce Blog ');
        mail.setSubject('Pass Word From Salesforce Blog');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setPlainTextBody('Your UserName: ' + Email +';/n Pass Word '+Password1 );
        mail.setHtmlBody('Your UserName: ' + Email +'\n Pass Word '+Password1);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });     
      } 
  }

Page:-

<apex:page showHeader="false" sidebar="false" Controller="PasswordGenerator" >
<apex:pagemessages />
<apex:form >
<apex:pageBlock tabStyle="Contact" >
<apex:pageBlockButtons location="Bottom">
<apex:commandButton value="Submit" action="{!save}" onclick="success();"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Salesforce Blog Registartion Form" >
Name:<apex:inputtext value="{!Name}"/>
<br/>
Email:<apex:inputtext value="{!Email}" id="email"/>
<br/>
Company:<apex:inputtext value="{!company}"/>
<br/>
EmployeeNumber:<apex:inputtext value="{!EmployeeNumber}"/>
<br/>
Comments about Blog:<apex:inputtextarea value="{!comments}" cols="27" rows="6"/>
<br/>
</apex:pageBlockSection>
</apex:pageBlock> 
</apex:form>              
</apex:page>

Check this once.It's working fine.
      




1 comment: