KickJava   Java API By Example, From Geeks To Geeks.

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

java.security
Class NoSuchAlgorithmException

java.lang.Object
  extended by java.lang.Throwable
      extended by java.lang.Exception
          extended by java.security.GeneralSecurityException
              extended by java.security.NoSuchAlgorithmException
All Implemented Interfaces:
Serializable
See Also:
Top Examples, Source Code

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


[669]DesEncrypter
By nandakumar { at } pyxistechsol { dot } com on 2004/02/20 02:36:49  Rate
 
 import java.io.*; 
 import java.security.*; 
 import java.security.spec.*; 
 import javax.crypto.*; 
 import javax.crypto.spec.*; 
  
  
     public class DesEncrypter  {  
         Cipher ecipher; 
         Cipher dcipher; 
  
  
         DesEncrypter ( SecretKey key )   {  
             // Create an 8-byte initialization vector 
             byte [  ]  iv = new byte [  ]  {  
                  ( byte ) 0x8E, 0x12, 0x39,  ( byte ) 0x9C, 
                 0x07, 0x72, 0x6F, 0x5A 
              } ; 
             AlgorithmParameterSpec paramSpec = new IvParameterSpec ( iv ) ; 
             try  {  
                 ecipher = Cipher.getInstance ( "DES/CBC/PKCS5Padding" ) ; 
                 dcipher = Cipher.getInstance ( "DES/CBC/PKCS5Padding" ) ; 
  
  
                 // CBC requires an initialization vector 
                 ecipher.init ( Cipher.ENCRYPT_MODE, key, paramSpec ) ; 
                 dcipher.init ( Cipher.DECRYPT_MODE, key, paramSpec ) ; 
              }  catch  ( java.security.InvalidAlgorithmParameterException e )   {  
              }  catch  ( javax.crypto.NoSuchPaddingException e )   {  
              }  catch  ( java.security.NoSuchAlgorithmException e )   {  
              }  catch  ( java.security.InvalidKeyException e )   {  
              }  
          }  
  
  
         // Buffer used to transport the bytes from one stream to another 
         byte [  ]  buf = new byte [ 1024 ] ; 
  
  
         public void encrypt ( InputStream in, OutputStream out )   {  
             try  {  
                 // Bytes written to out will be encrypted 
                 out = new CipherOutputStream ( out, ecipher ) ; 
  
  
                 // Read in the cleartext bytes and write to out to encrypt 
                 int numRead = 0; 
                 while  (  ( numRead = in.read ( buf )  )   > = 0 )   {  
                     out.write ( buf, 0, numRead ) ; 
                  }  
                 System.out.println ( "encrypt" ) ; 
                 out.close (  ) ; 
              }  catch  ( java.io.IOException e )   {  
         System.out.println ( "Exception e in encrypt="+e ) ; 
              }  
          }  
  
  
         public void decrypt ( InputStream in, OutputStream out )   {  
             try  {  
                 // Bytes read from in will be decrypted 
                 in = new CipherInputStream ( in, dcipher ) ; 
  
  
                 // Read in the decrypted bytes and write the cleartext to out 
                 int numRead = 0; 
                 while  (  ( numRead = in.read ( buf )  )   > = 0 )   {  
                     out.write ( buf, 0, numRead ) ; 
                  }  
                 out.close (  ) ; 
              }  catch  ( java.io.IOException e )   {  
         System.out.println ( "Exception e in decrypt="+e ) ; 
              }  
          }  
         public static void main ( String args [  ]  )  
          {  
       System.out.println ( "hello" ) ; 
         try  {  
         // Generate a temporary key. In practice, you would save this key. 
         // See also e464 Encrypting with DES Using a Pass Phrase. 
         SecretKey key = KeyGenerator.getInstance ( "DES" ) .generateKey (  ) ; 
         System.out.println ( "1111" ) ; 
         // Create encrypter/decrypter class 
         DesEncrypter encrypter = new DesEncrypter ( key ) ; 
         System.out.println ( "2222" ) ; 
         // Encrypt 
         encrypter.encrypt ( new FileInputStream ( "c:\text1.txt" ) , 
           new FileOutputStream ( "c:\text2" )  ) ; 
         System.out.println ( "3333" ) ; 
         // Decrypt 
         encrypter.decrypt ( new FileInputStream ( "c:\text2.txt" ) , 
           new FileOutputStream ( "c:\text12" )  ) ; 
         System.out.println ( "4444" ) ; 
        }  catch  ( Exception e )   {  
         System.out.println ( "Exception e="+e ) ; 
        }  
  
  
  
      }  
      }  
  
  
 


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


public NoSuchAlgorithmException(String message,
                                Throwable cause)
See Also:
Throwable.getCause(), Throwable.getMessage()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1881]Sign
By vinay on 2007/05/10 01:22:28  Rate
/* 
  * Created on May 10, 2007 
  * 
  * TODO To change the template for this generated file go to 
  * Window - Preferences - Java - Code Style - Code Templates 
  */
 
  
  
 /** 
  * @author 163289 
  * 
  * TODO To change the template for this generated type comment go to 
  * Window - Preferences - Java - Code Style - Code Templates 
  */
 
 import javax.xml.crypto.dsig.*; 
 import javax.xml.crypto.dsig.keyinfo.*; 
 import javax.xml.crypto.dsig.spec.*; 
 import javax.xml.crypto.dsig.dom.*; 
 import java.util.*; 
 import java.io.*; 
 import java.security.*; 
 import java.security.cert.*; 
 import javax.xml.transform.*; 
 import javax.xml.transform.stream.*; 
 import javax.xml.transform.dom.DOMSource;  
 import javax.xml.parsers.*; 
 import org.w3c.dom.*; 
   
  
  
 public class SoapSigner 
  {      
     
     public static String sign ( String soapMessage, String referenceId, String storeLocation, String storePasswrd, String alias, String aliasPasswrd )  
      {  
      String signedMessage = null; 
         System.out.println ( "Signing SOAP fragment [ " + referenceId + " ]  in message: " + soapMessage ) ; 
         try 
       {  
         String providerName = System.getProperty ( "jsr105Provider", "org.jcp.xml.dsig.internal.dom.XMLDSigRI" ) ; 
      XMLSignatureFactory fac = XMLSignatureFactory.getInstance ( "DOM",  ( Provider )  Class.forName ( providerName ) .newInstance (  )  ) ; 
      DigestMethod digestMethod = fac.newDigestMethod  ( "http://www.w3.org/2000/09/xmldsig#sha1", null ) ; 
   
       C14NMethodParameterSpec spec = null; 
      //CanonicalizationMethod cm = fac.newCanonicalizationMethod ( "http://www.w3.org/2001/10/xml-exc-c14n#",spec ) ; 
      CanonicalizationMethod cm = fac.newCanonicalizationMethod ( "http://www.w3.org/2001/10/xml-exc-c14n#",spec ) ; 
      SignatureMethod sm = fac.newSignatureMethod ( "http://www.w3.org/2000/09/xmldsig#rsa-sha1",null ) ; 
   
      ArrayList transformList = new ArrayList (  ) ; 
      TransformParameterSpec transformSpec = null; 
      Transform envTransform = fac.newTransform ( "http://www.w3.org/2001/10/xml-exc-c14n#",transformSpec ) ; 
      Transform exc14nTransform = fac.newTransform ( "http://www.w3.org/2000/09/xmldsig#enveloped-signature",transformSpec ) ; 
      transformList.add ( envTransform ) ; 
      transformList.add ( exc14nTransform ) ;  
  
  
      Reference ref = fac.newReference ( "#_98a1df45b2c5e29deee6942001a1c5b7c413",digestMethod,transformList,null,null ) ; //3 - transformList 
      ArrayList refList = new ArrayList (  ) ; 
      refList.add ( ref ) ; 
      SignedInfo signedInfo =fac.newSignedInfo ( cm,sm,refList ) ; 
       
 // Load the KeyStore and get the signing key and certificate. 
      KeyStore ks = KeyStore.getInstance ( "JKS" ) ; 
      ks.load ( new FileInputStream ( "aol.jks" ) , "a0lv3rlz0n".toCharArray (  )  ) ; 
         PrivateKey pk =  ( PrivateKey ) ks.getKey ( "sso.verizon.net","a0lv3rlz0n".toCharArray (  )  ) ; 
      X509Certificate cert =  ( X509Certificate ) ks.getCertificate ( "sso.verizon.net" ) ; 
   
 // Create the KeyInfo containing the X509Data. 
      KeyInfoFactory kif = fac.getKeyInfoFactory (  ) ; 
      List x509Content = new ArrayList (  ) ; 
      x509Content.add ( cert.getSubjectX500Principal (  ) .getName (  )  ) ; 
      x509Content.add ( cert ) ; 
      X509Data xd = kif.newX509Data ( x509Content ) ; 
      KeyInfo keyInfo = kif.newKeyInfo ( Collections.singletonList ( xd )  ) ; 
       
   
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance (  ) ; 
      dbf.setNamespaceAware ( true ) ; 
      //Document doc = dbf.newDocumentBuilder (  ) .parse  ( new ByteArrayInputStream ( " < SOAP-ENV:Envelope xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" >  < SOAP-ENV:Header >  < /SOAP-ENV:Header >  < SOAP-ENV:Body id=\"Body-123\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" wsu:Id=\"Body-123\" >  < ns:terminateAccount xmlns:ns=\"http://netservices.verizon.net/vasipvsi/vsi_aol_webservice\" >  < terminateAccountParameters xmlns:s=\"java:net.verizon.vsi.datatype.aol\" xsi:type=\"s:TerminateAccountParameters\" >  < java:clientName xmlns:java=\"java:net.verizon.vsi.datatype\" > aa < /java:clientName >  < s:accountId > 123 < /s:accountId >  < s:reasonCode > bb < /s:reasonCode >  < s:reasonText > cc < /s:reasonText >  < s:timeStamp > 2007-03-16T13:25:21.452-05:00 < /s:timeStamp >  < /terminateAccountParameters >  < /ns:terminateAccount >  < /SOAP-ENV:Body >  < /SOAP-ENV:Envelope > ".getBytes (  )  )  ) ;//new FileInputStream ( "C:/SSL/in.xml" )  ) ; 
       
     String x = " < sp:Response ID=\"#_98a1df45b2c5e29deee6942001a1c5b7c413\" IssueInstant=\"2007-05-09T15:22:37Z\" Version=\"2.0\" xmlns:sa=\"urn:oasis:names:tc:SAML:2.0:assertion\" xmlns:sp=\"urn:oasis:names:tc:SAML:2.0:protocol\" >   < sp:Status >   < sp:StatusCode Value=\"urn:oasis:names:tc:SAML:2.0:status:Success\"/ >   < /sp:Status >   < sa:Assertion ID=\"ID001D23ABFE4566\" IssueInstant=\"2007-05-09T15:22:37Z\" Version=\"2.0\" xmlns:sa=\"urn:oasis:names:tc:SAML:2.0:assertion\" >   < sa:Issuer > sso.verizon.net < /sa:Issuer >   < sa:Subject >   < sa:NameID Format=\"urn:x-aol:names:LUID\" > 5225bc20-e813-11db-a4f7-0014227726be < /sa:NameID >   < sa:SubjectConfirmation Method=\"urn:oasis:names:tc:SAML:2.0:cm:sender-vouches\"/ >   < /sa:Subject >   < sa:Conditions NotBefore=\"2007-05-09T15:22:37Z\" NotOnOrAfter=\"2007-05-09T15:52:37Z\" >   < sa:AudienceRestriction >   < sa:Audience > aol.com < /sa:Audience >   < /sa:AudienceRestriction >   < /sa:Conditions >   < sa:AuthnStatement AuthnInstant=\"2007-05-09T15:22:37Z\" >   < sa:AuthnContext >   < sa:AuthnContextClassRef > urn:oasis:names:tc:SAML:2.0:ac:classes:Password < /sa:AuthnContextClassRef >   < /sa:AuthnContext >   < /sa:AuthnStatement >   < /sa:Assertion >   < /sp:Response > "; 
  
  
   Document doc = dbf.newDocumentBuilder (  ) .parse  ( new ByteArrayInputStream ( soapMessage.getBytes (  )  )  ) ; 
  
  
   
      DOMSignContext dsc = new DOMSignContext ( pk, doc.getDocumentElement (  )  ) ; 
   
      XMLSignature signature = fac.newXMLSignature ( signedInfo, keyInfo ) ; 
   
      signature.sign ( dsc ) ; 
      
          OutputStream os = new ByteArrayOutputStream (  ) ; 
          TransformerFactory tf = TransformerFactory.newInstance (  ) ; 
          Transformer trans = tf.newTransformer (  ) ; 
          trans.transform ( new DOMSource ( doc ) , new StreamResult ( os )  ) ; 
             signedMessage = os.toString (  ) ; 
  
  
          }  
         catch ( Exception e )  
          {  
             System.out.println ( "Unable to sign the soap message fragment with refrId [ " + referenceId + " ] ." + e ) ; 
          }  
       
         return signedMessage; 
      }  
  
  
   public static void main ( String args [  ]  )   {  
     //String saml = " < SOAP-ENV:Envelope xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" >  < SOAP-ENV:Header >  < /SOAP-ENV:Header >  < SOAP-ENV:Body id=\"Body-123\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" wsu:Id=\"Body-123\" >  < ns:terminateAccount xmlns:ns=\"http://netservices.verizon.net/vasipvsi/vsi_aol_webservice\" >  < terminateAccountParameters xmlns:s=\"java:net.verizon.vsi.datatype.aol\" xsi:type=\"s:TerminateAccountParameters\" >  < java:clientName xmlns:java=\"java:net.verizon.vsi.datatype\" > aa < /java:clientName >  < s:accountId > 123 < /s:accountId >  < s:reasonCode > bb < /s:reasonCode >  < s:reasonText > cc < /s:reasonText >  < s:timeStamp > 2007-03-16T13:25:21.452-05:00 < /s:timeStamp >  < /terminateAccountParameters >  < /ns:terminateAccount >  < /SOAP-ENV:Body >  < /SOAP-ENV:Envelope > "; 
     String saml = " < sp:Response ID=\"Body-123\" IssueInstant=\"2007-05-09T15:22:37Z\" Version=\"2.0\" xmlns:sa=\"urn:oasis:names:tc:SAML:2.0:assertion\" xmlns:sp=\"urn:oasis:names:tc:SAML:2.0:protocol\" >   < sp:Status >   < sp:StatusCode Value=\"urn:oasis:names:tc:SAML:2.0:status:Success\"/ >   < /sp:Status >   < sa:Assertion ID=\"ID001D23ABFE4566\" IssueInstant=\"2007-05-09T15:22:37Z\" Version=\"2.0\" xmlns:sa=\"urn:oasis:names:tc:SAML:2.0:assertion\" >   < sa:Issuer > sso.verizon.net < /sa:Issuer >   < sa:Subject >   < sa:NameID Format=\"urn:x-aol:names:LUID\" > 5225bc20-e813-11db-a4f7-0014227726be < /sa:NameID >   < sa:SubjectConfirmation Method=\"urn:oasis:names:tc:SAML:2.0:cm:sender-vouches\"/ >   < /sa:Subject >   < sa:Conditions NotBefore=\"2007-05-09T15:22:37Z\" NotOnOrAfter=\"2007-05-09T15:52:37Z\" >   < sa:AudienceRestriction >   < sa:Audience > aol.com < /sa:Audience >   < /sa:AudienceRestriction >   < /sa:Conditions >   < sa:AuthnStatement AuthnInstant=\"2007-05-09T15:22:37Z\" >   < sa:AuthnContext >   < sa:AuthnContextClassRef > urn:oasis:names:tc:SAML:2.0:ac:classes:Password < /sa:AuthnContextClassRef >   < /sa:AuthnContext >   < /sa:AuthnStatement >   < /sa:Assertion >   < /sp:Response > "; 
            String signedStr = SoapSigner.sign ( saml, "#_98a1df45b2c5e29deee6942001a1c5b7c413", "aol.jks", "a0lv3rlz0n", "sso.verizon.net", "a0lv3rlz0n" ) ; 
  
  
          System.out.println ( saml ) ; 
  
  
          System.out.println ( signedStr ) ; 
    }  
   
  } 


public NoSuchAlgorithmException(Throwable cause)
See Also:
Throwable.getCause()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags