os/security/crypto/weakcryptospi/test/tcryptospi/testdata/symmetricdatacheck0001/gendat.java
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/testdata/symmetricdatacheck0001/gendat.java	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,145 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +   import java.security.*;
    1.23 +   import javax.crypto.*;
    1.24 +   import javax.crypto.spec.*;
    1.25 +   import java.io.*;
    1.26 +   import java.io.*;
    1.27 +   import java.security.spec.*;
    1.28 +   import javax.net.ssl.SSLServerSocket;
    1.29 +   import javax.net.ssl.SSLServerSocketFactory;
    1.30 +   import javax.net.ssl.SSLSocket;
    1.31 +   
    1.32 +   /**
    1.33 +   * This program generates a AES key, retrieves its raw bytes, and
    1.34 +   * then reinstantiates a AES key from the key bytes.
    1.35 +   * The reinstantiated key is used to initialize a AES cipher for
    1.36 +   * encryption and decryption.
    1.37 +   */
    1.38 +
    1.39 +   public class GENDAT {
    1.40 +
    1.41 +     /**
    1.42 +     * Turns array of bytes into string
    1.43 +     *
    1.44 +     * @param buf	Array of bytes to convert to hex string
    1.45 +     * @return	Generated hex string
    1.46 +     */
    1.47 +     public static String asHex (byte buf[]) {
    1.48 +      StringBuffer strbuf = new StringBuffer(buf.length * 2);
    1.49 +      int i;
    1.50 +
    1.51 +      for (i = 0; i < buf.length; i++) {
    1.52 +       if (((int) buf[i] & 0xff) < 0x10)
    1.53 +	    strbuf.append("0");
    1.54 +
    1.55 +       strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
    1.56 +      }
    1.57 +
    1.58 +      return strbuf.toString();
    1.59 +     }
    1.60 +
    1.61 +     public static void main(String[] args) throws Exception {
    1.62 +    String algo="";
    1.63 +    String oper="";
    1.64 +    String pad="";
    1.65 +    String composite="";
    1.66 +    
    1.67 +    String inContents="";
    1.68 +    String outContents="";
    1.69 +    String keyContents="";
    1.70 +    String ivContents="";
    1.71 +    
    1.72 +    String inPath = args[0];
    1.73 +    String outPath = args[1];
    1.74 +    String keyPath = args[2];
    1.75 +    String ivPath = "";
    1.76 +    
    1.77 +	algo = args[3];
    1.78 +	composite = args[4];
    1.79 +
    1.80 +    if(args.length>=6)
    1.81 +    {
    1.82 +    	ivPath = args[5];
    1.83 +    	
    1.84 +        BufferedReader ivFile = new BufferedReader(new FileReader(ivPath));
    1.85 +        ivContents = ivFile.readLine();
    1.86 +        ivFile.close();
    1.87 +    }
    1.88 +
    1.89 +	
    1.90 +    // read in the file contents
    1.91 +    BufferedReader inFile = new BufferedReader(new FileReader(inPath));
    1.92 +    inContents = inFile.readLine();
    1.93 +    inFile.close();
    1.94 +    
    1.95 +    BufferedReader keyFile = new BufferedReader(new FileReader(keyPath));
    1.96 +    keyContents = keyFile.readLine();
    1.97 +    keyFile.close();
    1.98 +    
    1.99 +
   1.100 +
   1.101 +
   1.102 +    // Get the KeyGenerator
   1.103 +	//	KeyGenerator kgen = KeyGenerator.getInstance(algo);
   1.104 +	//	kgen.init(56); // 192 and 256 bits may not be available
   1.105 +
   1.106 +
   1.107 +       // Generate the secret key specs.
   1.108 +   //    SecretKey skey = kgen.generateKey();
   1.109 +     //  byte[] raw = skey.getEncoded();
   1.110 +
   1.111 +		byte[] raw = keyContents.getBytes();
   1.112 +		
   1.113 +       SecretKeySpec skeySpec = new SecretKeySpec(raw, algo);
   1.114 +
   1.115 +       Cipher cipher = Cipher.getInstance(composite);
   1.116 +       
   1.117 +       byte[] ivBytes = ivContents.getBytes();
   1.118 +       
   1.119 +       if(ivBytes.length > 0)
   1.120 +       {
   1.121 +    	   if(algo.equals("RC2"))
   1.122 +    	   {
   1.123 +    		   RC2ParameterSpec rc2Spec = new RC2ParameterSpec(64, ivBytes);
   1.124 +    	       cipher.init(Cipher.ENCRYPT_MODE, skeySpec, rc2Spec );
   1.125 +    	   }
   1.126 +    	   else
   1.127 +    	   {     
   1.128 +    		   AlgorithmParameterSpec paramSpec = new IvParameterSpec(ivBytes);
   1.129 +    		   cipher.init(Cipher.ENCRYPT_MODE, skeySpec, paramSpec );
   1.130 +    	   }
   1.131 +       }
   1.132 +       else
   1.133 +       {
   1.134 +           cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
   1.135 +       }
   1.136 +       FileOutputStream outFile = new FileOutputStream(outPath);
   1.137 +       PrintStream p = new PrintStream( outFile );
   1.138 +       
   1.139 +       byte[] encrypted =
   1.140 +         cipher.doFinal(inContents.getBytes());
   1.141 +       System.out.print("encrypted string: " + asHex(encrypted));
   1.142 +       p.print(asHex(encrypted));
   1.143 +       
   1.144 +       p.close();
   1.145 +    
   1.146 +
   1.147 +     }
   1.148 +   }