KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > security > Signature

java.security
Class Signature

java.lang.Object
  extended by java.security.SignatureSpi
      extended by java.security.Signature
See Also:
Top Examples, Source Code, verify, sign, update, initSign(PrivateKey, SecureRandom), initSign(PrivateKey), initVerify

public Object clone()
             throws CloneNotSupportedException
See Also:
Cloneable, SignatureSpi
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final String getAlgorithm()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static Signature getInstance(String algorithm)
                             throws NoSuchAlgorithmException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1511]Generating and verifying a security signature
By Sandy on 2005/08/10 01:45:22  Rate
// This is a complete example - generating and verifying a Signature 
  
  
   try 
    {  
     
     // 1. Get the bytes in the Message. 
      
     String strMessage = " Hello World !"; 
     byte [  ]  bMessage = strMessage.getBytes (  ) ; 
      
     // 2. Get the PrivateKey and the PublicKey. 
      
     KeyPair keyPair = KeyPairGenerator.getInstance ( "DSA" ) .generateKeyPair (  ) ; 
      
     PrivateKey privateKey = keyPair.getPrivate (  ) ; 
     PublicKey publicKey = keyPair.getPublic (  ) ; 
      
     // Incorrect Public Key 
      
     KeyPair keyPairIncorrect = KeyPairGenerator.getInstance ( "DSA" ) .generateKeyPair (  ) ; 
     PublicKey publicKeyIncorrect = keyPairIncorrect.getPublic (  ) ; 
      
     // 3. Encrypt the Data. 
      
     Signature signature = Signature.getInstance ( "DSA" ) ; 
      
     signature.initSign (  privateKey  ) ; 
     signature.update (  bMessage  ) ; 
      
     // 4. Get the Signature, by signing the message. 
      
     byte [  ]  bSignature = signature.sign (  ) ; 
      
     // 5. Decrypt the Signature with the Public Key and get the Message Digest. 
      
     Signature signaturePublic = Signature.getInstance ( "DSA" ) ; 
      
      
     // 6. Authentication 
      
     signaturePublic.initVerify ( publicKey ) ; 
     signaturePublic.update ( bMessage ) ; 
      
     // 6. Check if the Signatures Match. 
      
     boolean b = signaturePublic.verify (  bSignature  ) ; 
      
     if ( b )  
      {  
       System.out.println ( " The Signature is Good " + b ) ;   
      }  
     else 
      {  
       System.out.println ( " The Signature is Bad " + b ) ;       
      }      
    }  
   catch  ( NoSuchAlgorithmException e )  
    {  
     e.printStackTrace (  ) ; 
    }  
   catch  ( SignatureException e )  
    {  
     e.printStackTrace (  ) ; 
    }  
   catch  ( InvalidKeyException e )  
    {  
     e.printStackTrace (  ) ; 
    }  
   catch  ( Exception e )  
    {  
     e.printStackTrace (  ) ; 
    }  
  
  
 // I hope this program is useful to the newbies in Cryptography.


public static Signature getInstance(String algorithm,
                                    String provider)
                             throws NoSuchAlgorithmException,
                                    NoSuchProviderException
See Also:
Provider, IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static Signature getInstance(String algorithm,
                                    Provider provider)
                             throws NoSuchAlgorithmException
See Also:
IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
public final Object getParameter(String param)
                          throws InvalidParameterException
See Also:
setParameter(String, Object)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final AlgorithmParameters getParameters()
See Also:
setParameter(AlgorithmParameterSpec)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final Provider getProvider()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void initSign(PrivateKey privateKey)
                    throws InvalidKeyException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void initSign(PrivateKey privateKey,
                           SecureRandom random)
                    throws InvalidKeyException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void initVerify(Certificate certificate)
                      throws InvalidKeyException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void initVerify(PublicKey publicKey)
                      throws InvalidKeyException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
public final void setParameter(String param,
                                          Object value)
                        throws InvalidParameterException
See Also:
getParameter(java.lang.String), setParameter
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[448]A simple Applet
By samir_shingre { at } hotmail { dot } com on 2003/10/09 07:08:30  Rate
import java.lang.*; 
 import java.awt.*; 
 import java.applet.*; 
 /* 
  < applet code="Parademo.class" width="300" height="150" >  
  < param name=fontname value="courier" >  
  < param name=fontsize value="14" >  
  < param name=msg value="Good Morning" >  
  < /applet >  
 */
 
 public class Parademo extends Applet 
  {  
   String msg; 
   String Fontname; 
   int Fontsize; 
 public void start (  )  
  {  
   String param; 
   Fontname=getParameter ( "fontname" ) ; 
   if ( Fontname==null ) Fontname="courier"; 
   param=getParameter ( "fontsize" ) ; 
   if ( param!=null )  
   Fontsize=Integer.parseInt ( param ) ; 
   else 
   Fontsize=12; 
    msg=setParameter ( "msg" ) ; 
  }  
  
  
 public void paint ( Graphics g )  
  {  
   //msg="Good Morning" 
   Font f=new Font ( Fontname,Font.BOLD,Fontsize ) ; 
   g.setFont ( f ) ; 
   g.drawString ( msg,50,50 ) ; 
  }  
  } 


public final void setParameter(AlgorithmParameterSpec params)
                        throws InvalidAlgorithmParameterException
See Also:
getParameters()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected static final int SIGN
See Also:
state
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final byte[] sign()
                  throws SignatureException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final int sign(byte[] outbuf,
                      int offset,
                      int len)
               throws SignatureException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected Signature(String algorithm)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected int state
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String toString()
See Also:
Object
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected static final int UNINITIALIZED
See Also:
state
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void update(byte b)
                  throws SignatureException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void update(byte[] data)
                  throws SignatureException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void update(byte[] data,
                         int off,
                         int len)
                  throws SignatureException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final void update(ByteBuffer data)
                  throws SignatureException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected static final int VERIFY
See Also:
state
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final boolean verify(byte[] signature)
                     throws SignatureException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final boolean verify(byte[] signature,
                            int offset,
                            int length)
                     throws SignatureException
See Also:
IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags