os/security/crypto/weakcryptospi/test/tcryptospi/testdata/symmetricdatacheck0001/gendat.java
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
   import java.security.*;
sl@0
    20
   import javax.crypto.*;
sl@0
    21
   import javax.crypto.spec.*;
sl@0
    22
   import java.io.*;
sl@0
    23
   import java.io.*;
sl@0
    24
   import java.security.spec.*;
sl@0
    25
   import javax.net.ssl.SSLServerSocket;
sl@0
    26
   import javax.net.ssl.SSLServerSocketFactory;
sl@0
    27
   import javax.net.ssl.SSLSocket;
sl@0
    28
   
sl@0
    29
   /**
sl@0
    30
   * This program generates a AES key, retrieves its raw bytes, and
sl@0
    31
   * then reinstantiates a AES key from the key bytes.
sl@0
    32
   * The reinstantiated key is used to initialize a AES cipher for
sl@0
    33
   * encryption and decryption.
sl@0
    34
   */
sl@0
    35
sl@0
    36
   public class GENDAT {
sl@0
    37
sl@0
    38
     /**
sl@0
    39
     * Turns array of bytes into string
sl@0
    40
     *
sl@0
    41
     * @param buf	Array of bytes to convert to hex string
sl@0
    42
     * @return	Generated hex string
sl@0
    43
     */
sl@0
    44
     public static String asHex (byte buf[]) {
sl@0
    45
      StringBuffer strbuf = new StringBuffer(buf.length * 2);
sl@0
    46
      int i;
sl@0
    47
sl@0
    48
      for (i = 0; i < buf.length; i++) {
sl@0
    49
       if (((int) buf[i] & 0xff) < 0x10)
sl@0
    50
	    strbuf.append("0");
sl@0
    51
sl@0
    52
       strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
sl@0
    53
      }
sl@0
    54
sl@0
    55
      return strbuf.toString();
sl@0
    56
     }
sl@0
    57
sl@0
    58
     public static void main(String[] args) throws Exception {
sl@0
    59
    String algo="";
sl@0
    60
    String oper="";
sl@0
    61
    String pad="";
sl@0
    62
    String composite="";
sl@0
    63
    
sl@0
    64
    String inContents="";
sl@0
    65
    String outContents="";
sl@0
    66
    String keyContents="";
sl@0
    67
    String ivContents="";
sl@0
    68
    
sl@0
    69
    String inPath = args[0];
sl@0
    70
    String outPath = args[1];
sl@0
    71
    String keyPath = args[2];
sl@0
    72
    String ivPath = "";
sl@0
    73
    
sl@0
    74
	algo = args[3];
sl@0
    75
	composite = args[4];
sl@0
    76
sl@0
    77
    if(args.length>=6)
sl@0
    78
    {
sl@0
    79
    	ivPath = args[5];
sl@0
    80
    	
sl@0
    81
        BufferedReader ivFile = new BufferedReader(new FileReader(ivPath));
sl@0
    82
        ivContents = ivFile.readLine();
sl@0
    83
        ivFile.close();
sl@0
    84
    }
sl@0
    85
sl@0
    86
	
sl@0
    87
    // read in the file contents
sl@0
    88
    BufferedReader inFile = new BufferedReader(new FileReader(inPath));
sl@0
    89
    inContents = inFile.readLine();
sl@0
    90
    inFile.close();
sl@0
    91
    
sl@0
    92
    BufferedReader keyFile = new BufferedReader(new FileReader(keyPath));
sl@0
    93
    keyContents = keyFile.readLine();
sl@0
    94
    keyFile.close();
sl@0
    95
    
sl@0
    96
sl@0
    97
sl@0
    98
sl@0
    99
    // Get the KeyGenerator
sl@0
   100
	//	KeyGenerator kgen = KeyGenerator.getInstance(algo);
sl@0
   101
	//	kgen.init(56); // 192 and 256 bits may not be available
sl@0
   102
sl@0
   103
sl@0
   104
       // Generate the secret key specs.
sl@0
   105
   //    SecretKey skey = kgen.generateKey();
sl@0
   106
     //  byte[] raw = skey.getEncoded();
sl@0
   107
sl@0
   108
		byte[] raw = keyContents.getBytes();
sl@0
   109
		
sl@0
   110
       SecretKeySpec skeySpec = new SecretKeySpec(raw, algo);
sl@0
   111
sl@0
   112
       Cipher cipher = Cipher.getInstance(composite);
sl@0
   113
       
sl@0
   114
       byte[] ivBytes = ivContents.getBytes();
sl@0
   115
       
sl@0
   116
       if(ivBytes.length > 0)
sl@0
   117
       {
sl@0
   118
    	   if(algo.equals("RC2"))
sl@0
   119
    	   {
sl@0
   120
    		   RC2ParameterSpec rc2Spec = new RC2ParameterSpec(64, ivBytes);
sl@0
   121
    	       cipher.init(Cipher.ENCRYPT_MODE, skeySpec, rc2Spec );
sl@0
   122
    	   }
sl@0
   123
    	   else
sl@0
   124
    	   {     
sl@0
   125
    		   AlgorithmParameterSpec paramSpec = new IvParameterSpec(ivBytes);
sl@0
   126
    		   cipher.init(Cipher.ENCRYPT_MODE, skeySpec, paramSpec );
sl@0
   127
    	   }
sl@0
   128
       }
sl@0
   129
       else
sl@0
   130
       {
sl@0
   131
           cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
sl@0
   132
       }
sl@0
   133
       FileOutputStream outFile = new FileOutputStream(outPath);
sl@0
   134
       PrintStream p = new PrintStream( outFile );
sl@0
   135
       
sl@0
   136
       byte[] encrypted =
sl@0
   137
         cipher.doFinal(inContents.getBytes());
sl@0
   138
       System.out.print("encrypted string: " + asHex(encrypted));
sl@0
   139
       p.print(asHex(encrypted));
sl@0
   140
       
sl@0
   141
       p.close();
sl@0
   142
    
sl@0
   143
sl@0
   144
     }
sl@0
   145
   }