sl@0: /* sl@0: * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: import java.security.*; sl@0: import javax.crypto.*; sl@0: import javax.crypto.spec.*; sl@0: import java.io.*; sl@0: import java.io.*; sl@0: import java.security.spec.*; sl@0: import javax.net.ssl.SSLServerSocket; sl@0: import javax.net.ssl.SSLServerSocketFactory; sl@0: import javax.net.ssl.SSLSocket; sl@0: sl@0: /** sl@0: * This program generates a AES key, retrieves its raw bytes, and sl@0: * then reinstantiates a AES key from the key bytes. sl@0: * The reinstantiated key is used to initialize a AES cipher for sl@0: * encryption and decryption. sl@0: */ sl@0: sl@0: public class GENDAT { sl@0: sl@0: /** sl@0: * Turns array of bytes into string sl@0: * sl@0: * @param buf Array of bytes to convert to hex string sl@0: * @return Generated hex string sl@0: */ sl@0: public static String asHex (byte buf[]) { sl@0: StringBuffer strbuf = new StringBuffer(buf.length * 2); sl@0: int i; sl@0: sl@0: for (i = 0; i < buf.length; i++) { sl@0: if (((int) buf[i] & 0xff) < 0x10) sl@0: strbuf.append("0"); sl@0: sl@0: strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); sl@0: } sl@0: sl@0: return strbuf.toString(); sl@0: } sl@0: sl@0: public static void main(String[] args) throws Exception { sl@0: String algo=""; sl@0: String oper=""; sl@0: String pad=""; sl@0: String composite=""; sl@0: sl@0: String inContents=""; sl@0: String outContents=""; sl@0: String keyContents=""; sl@0: String ivContents=""; sl@0: sl@0: String inPath = args[0]; sl@0: String outPath = args[1]; sl@0: String keyPath = args[2]; sl@0: String ivPath = ""; sl@0: sl@0: algo = args[3]; sl@0: composite = args[4]; sl@0: sl@0: if(args.length>=6) sl@0: { sl@0: ivPath = args[5]; sl@0: sl@0: BufferedReader ivFile = new BufferedReader(new FileReader(ivPath)); sl@0: ivContents = ivFile.readLine(); sl@0: ivFile.close(); sl@0: } sl@0: sl@0: sl@0: // read in the file contents sl@0: BufferedReader inFile = new BufferedReader(new FileReader(inPath)); sl@0: inContents = inFile.readLine(); sl@0: inFile.close(); sl@0: sl@0: BufferedReader keyFile = new BufferedReader(new FileReader(keyPath)); sl@0: keyContents = keyFile.readLine(); sl@0: keyFile.close(); sl@0: sl@0: sl@0: sl@0: sl@0: // Get the KeyGenerator sl@0: // KeyGenerator kgen = KeyGenerator.getInstance(algo); sl@0: // kgen.init(56); // 192 and 256 bits may not be available sl@0: sl@0: sl@0: // Generate the secret key specs. sl@0: // SecretKey skey = kgen.generateKey(); sl@0: // byte[] raw = skey.getEncoded(); sl@0: sl@0: byte[] raw = keyContents.getBytes(); sl@0: sl@0: SecretKeySpec skeySpec = new SecretKeySpec(raw, algo); sl@0: sl@0: Cipher cipher = Cipher.getInstance(composite); sl@0: sl@0: byte[] ivBytes = ivContents.getBytes(); sl@0: sl@0: if(ivBytes.length > 0) sl@0: { sl@0: if(algo.equals("RC2")) sl@0: { sl@0: RC2ParameterSpec rc2Spec = new RC2ParameterSpec(64, ivBytes); sl@0: cipher.init(Cipher.ENCRYPT_MODE, skeySpec, rc2Spec ); sl@0: } sl@0: else sl@0: { sl@0: AlgorithmParameterSpec paramSpec = new IvParameterSpec(ivBytes); sl@0: cipher.init(Cipher.ENCRYPT_MODE, skeySpec, paramSpec ); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: cipher.init(Cipher.ENCRYPT_MODE, skeySpec); sl@0: } sl@0: FileOutputStream outFile = new FileOutputStream(outPath); sl@0: PrintStream p = new PrintStream( outFile ); sl@0: sl@0: byte[] encrypted = sl@0: cipher.doFinal(inContents.getBytes()); sl@0: System.out.print("encrypted string: " + asHex(encrypted)); sl@0: p.print(asHex(encrypted)); sl@0: sl@0: p.close(); sl@0: sl@0: sl@0: } sl@0: }