java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
javax.crypto.CipherOutputStream
- All Implemented Interfaces:
- Closeable, Flushable
- See Also:
- Top Examples, Source Code,
Cipher ,
CipherInputStream
protected CipherOutputStream(OutputStream os) - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[440]Generate Symmetric Key, encryption, and decryption By nils { dot } schneider { at } drkw { dot } com on 2003/10/07 07:35:17 Rate
package enc; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.Cipher; import javax.crypto.CipherOutputStream; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.IvParameterSpec; import java.security.SecureRandom; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.InvalidAlgorithmParameterException; import java.security.Security; import java.security.Provider; import java.security.Key; import java.security.UnrecoverableKeyException; import java.security.KeyFactory; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.InvalidKeyException; import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.util.Iterator; import java.util.Set; import java.util.List; import java.util.ArrayList; import java.util.Collections; import java.io.*; import com.sun.crypto.*; import com.sun.crypto.provider.DESedeCipher; public class JavaCipher { public JavaCipher ( ) { super ( ) ; } static final String keystore = "C:\\Zertifikate\\jks_keystore.jks"; static final String password = "diplom"; static final String cipherfile = "C:\\Zertifikate\\cipherfile.txt"; static final String cipherfile2 = "C:\\Zertifikate\\cipherfile2.txt"; static String inputfile = "C:\\Zertifikate\\deal.txt"; // static String inputfile = "C:\\Programme\\eclipse\\workspace\\XML Security Suite\\deal.xml"; static String decrypted_file = "C:\\Zertifikate\\decrypted_file.txt"; static String korrekt = "C:\\Zertifikate\\korrekt.txt"; static String logfile = "C:\\Zertifikate\\logfile.txt"; static File f = new File ( logfile ) ; static FileWriter fw = null; static { try { fw = new FileWriter ( f ) ; } catch ( IOException io ) { fw = null; System.out.println ( "Could not initialize LogFile." ) ; } } public static void main ( String [ ] args ) { try { JavaCipher.getProviderInformation ( ) ; SecretKey key = ( SecretKey ) JavaCipher.generateSymmetricKey ( "DESede", "FlexiCore" ) ; JavaCipher.encryption ( "DESede", "FlexiCore", key ) ; JavaCipher.decryption ( "DESede", "FlexiCore", key ) ; } catch ( Exception e ) { writeLogfile ( "Exception "+e ) ; } } public static Key generateSymmetricKey ( String algorithm, String provider ) { writeLogfile ( "\nKeyGenerator - Start symmetric KeyGenerator" ) ; Key key = null; try { KeyGenerator kg = null; try { kg = KeyGenerator.getInstance ( algorithm, provider ) ; } catch ( NoSuchProviderException nspe ) { kg = KeyGenerator.getInstance ( algorithm ) ; } // kg.init ( new SecureRandom ( ) ) ; kg.init ( 168 ) ; key = kg.generateKey ( ) ; List keys = new ArrayList ( kg.getProvider ( ) .keySet ( ) ) ; Collections.sort ( keys ) ; Iterator keysIterator = keys.iterator ( ) ; do { String key_prov = ( String ) keysIterator.next ( ) ; if ( kg.getProvider ( ) .getProperty ( key_prov ) ==key.getAlgorithm ( ) ) writeLogfile ( "KeyGenerator - "+key_prov+": "+kg.getProvider ( ) .getProperty ( key_prov ) ) ; } while ( keysIterator.hasNext ( ) ) ; writeLogfile ( "KeyGenerator - Generated Key Informations:" ) ; writeLogfile ( "KeyGenerator - Algorithm: "+key.getAlgorithm ( ) ) ; writeLogfile ( "KeyGenerator - Format: "+key.getFormat ( ) ) ; writeLogfile ( "KeyGenerator - HashCode: "+key.hashCode ( ) ) ; writeLogfile ( "KeyGenerator - Provider: "+kg.getProvider ( ) ) ; writeLogfile ( "KeyGenerator - Class: "+kg.getClass ( ) ) ; writeLogfile ( "KeyGenerator - Serial Version UID: "+key.serialVersionUID ) ; writeLogfile ( "KeyGenerator - End Symmetric KeyGenerator\n" ) ; } catch ( NoSuchAlgorithmException nsae ) { writeLogfile ( "NoSuchAlgorithmException: "+nsae.getMessage ( ) ) ; } return key; } public static void getProviderInformation ( ) { java.security.Provider [ ] provList = java.security.Security.getProviders ( ) ; writeLogfile ( "\nInformation - List of JCA/JCE registered Service Providers:\n " ) ; for ( int i = 0; i < provList.length; i++ ) { writeLogfile ( "Information - "+i+". provider: name= "+provList [ i ] .getName ( ) +", version="+ provList [ i ] .getVersion ( ) +", information= "+ provList [ i ] .getInfo ( ) ) ; List keyList = new ArrayList ( provList [ i ] .keySet ( ) ) ; Collections.sort ( keyList ) ; Iterator keyIterator = keyList.iterator ( ) ; while ( keyIterator.hasNext ( ) ) { String key = ( String ) keyIterator.next ( ) ; writeLogfile ( "Information - \t"+key+": "+provList [ i ] .getProperty ( key ) ) ; } writeLogfile ( "\n" ) ; } Set AlgorithmSet = Security.getAlgorithms ( "Cipher" ) ; writeLogfile ( "Information - registered service providers:\n" ) ; for ( Iterator it=AlgorithmSet.iterator ( ) ; it.hasNext ( ) ; ) { writeLogfile ( "Information - "+it.next ( ) .toString ( ) ) ; } } public static void encryption ( String algorithm_alias, String provider, SecretKey key ) throws Exception { writeLogfile ( "\nEncryption - start encryption" ) ; Cipher ciph = Cipher.getInstance ( algorithm_alias, provider ) ; writeLogfile ( "Encrpytion - algorithm: "+ciph.getAlgorithm ( ) +", provider:"+ciph.getProvider ( ) +",block-size:"+ciph.getBlockSize ( ) ) ; // System.out.wait ( 100 ) ; // ciph.init ( Cipher.ENCRYPT_MODE, cert ) ; // ciph.init ( Cipher.ENCRYPT_MODE, key_from_store ) ; FileInputStream fis = new FileInputStream ( inputfile ) ; FileOutputStream fos = new FileOutputStream ( cipherfile ) ; FileOutputStream fos2 = new FileOutputStream ( cipherfile2 ) ; FileOutputStream test = new FileOutputStream ( korrekt ) ; PrintWriter pw = new PrintWriter ( System.out, true ) ; BufferedWriter bw = new BufferedWriter ( pw ) ; CipherOutputStream cos = new CipherOutputStream ( fos, ciph ) ; CipherOutputStream cos2 = new CipherOutputStream ( fos2, ciph ) ; byte [ ] buffer = new byte [ 512 ] ; ByteArrayOutputStream baos = new ByteArrayOutputStream ( buffer.length ) ; // byte [ ] iv= new byte [ 64 ] ; // // IvParameterSpec spec = new IvParameterSpec ( iv ) ; // fos.write ( iv ) ; // ciph.init ( Cipher.ENCRYPT_MODE, key, spec ) ; ciph.init ( Cipher.ENCRYPT_MODE, key ) ; writeLogfile ( "Encryption - CipherObject initialized" ) ; fis.read ( buffer ) ; System.out.println ( "buffer.length: "+buffer.length ) ; for ( int j = 0; j < buffer.length; j++ ) { if ( buffer [ j ] !=0 ) cos2.write ( buffer [ j ] ) ; test.write ( buffer [ j ] ) ; System.out.println ( "buffer [ "+j+" ] = "+buffer [ j ] ) ; } int length = 0; while ( ( length=fis.read ( buffer ) ) !=-1 ) { System.out.println ( "encryption length="+length ) ; cos.write ( buffer, 0, length ) ; // test.write ( buffer, 0, length ) ; } cos.flush ( ) ; int i = 0; // System.out.println ( "buffer.length: "+buffer.length ) ; writeLogfile ( "Encryption - buffer-length: "+buffer.length ) ; writeLogfile ( "Encryption - End Encryption\n" ) ; fis.close ( ) ; fos.close ( ) ; cos.close ( ) ; } public static void decryption ( String algorithm_alias, String provider, SecretKey key ) throws Exception { writeLogfile ( "\nDecryption - start decryption" ) ; Cipher unciph = Cipher.getInstance ( algorithm_alias, provider ) ; FileInputStream fis_encrypted = new FileInputStream ( cipherfile2 ) ; FileOutputStream fos_decrypted = new FileOutputStream ( decrypted_file ) ; CipherOutputStream cos_decrypted = new CipherOutputStream ( fos_decrypted, unciph ) ; byte [ ] buffer_encrypted = new byte [ 8192 ] ; // byte [ ] iv= new byte [ 64 ] ; // fos_decrypted.write ( iv ) ; // IvParameterSpec spec = new IvParameterSpec ( iv ) ; // fis_encrypted.read ( iv ) ; unciph.init ( Cipher.DECRYPT_MODE, key ) ; int length = 0; while ( ( length=fis_encrypted.read ( buffer_encrypted ) ) !=-1 ) { cos_decrypted.write ( buffer_encrypted, 0, length ) ; } writeLogfile ( "Decryption - algorithm: "+unciph.getAlgorithm ( ) +", provider:"+unciph.getProvider ( ) +", block-size: "+unciph.getBlockSize ( ) ) ; writeLogfile ( "Decryption - end decryption\n" ) ; } private static void writeLogfile ( String text ) { System.out.println ( text ) ; PrintWriter pw = new PrintWriter ( fw ) ; pw.println ( text ) ; } }
public CipherOutputStream(OutputStream os,
Cipher c) - Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void close()
throws IOException - See Also:
FilterOutputStream.out , FilterOutputStream.flush() , Closeable
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void flush()
throws IOException - See Also:
FilterOutputStream.out , Flushable
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void write(byte[] b)
throws IOException - See Also:
write(byte[], int, int) , NullPointerException, FilterOutputStream
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void write(byte[] b,
int off,
int len)
throws IOException - See Also:
FilterOutputStream.write(int)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void write(int b)
throws IOException - See Also:
- FilterOutputStream
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
| Popular Tags |