test/compiler/7184394/TestAESBase.java
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File 8004835 Sdiff test/compiler/7184394

test/compiler/7184394/TestAESBase.java

Print this page




  37 import java.util.Arrays;
  38 
  39 abstract public class TestAESBase {
  40   int msgSize = Integer.getInteger("msgSize", 646);
  41   boolean checkOutput = Boolean.getBoolean("checkOutput");
  42   boolean noReinit = Boolean.getBoolean("noReinit");
  43   int keySize = Integer.getInteger("keySize", 128);
  44   String algorithm = System.getProperty("algorithm", "AES");
  45   String mode = System.getProperty("mode", "CBC");
  46   byte[] input;
  47   byte[] encode;
  48   byte[] expectedEncode;
  49   byte[] decode;
  50   byte[] expectedDecode;
  51   Random random = new Random(0);
  52   Cipher cipher;
  53   Cipher dCipher;
  54   String paddingStr = "PKCS5Padding";
  55   AlgorithmParameters algParams;
  56   SecretKey key;
  57   int ivLen;
  58 
  59   static int numThreads = 0;
  60   int  threadId;
  61   static synchronized int getThreadId() {
  62     int id = numThreads;
  63     numThreads++;
  64     return id;
  65   }
  66 
  67   abstract public void run();
  68 
  69   public void prepare() {
  70     try {
  71     System.out.println("\nmsgSize=" + msgSize + ", key size=" + keySize + ", reInit=" + !noReinit + ", checkOutput=" + checkOutput);
  72 
  73       int keyLenBytes = (keySize == 0 ? 16 : keySize/8);
  74       byte keyBytes[] = new byte[keyLenBytes];
  75       if (keySize == 128)
  76         keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};
  77       else
  78         random.nextBytes(keyBytes);
  79 
  80       key = new SecretKeySpec(keyBytes, algorithm);
  81       if (threadId == 0) {
  82         System.out.println("Algorithm: " + key.getAlgorithm() + "("
  83                            + key.getEncoded().length * 8 + "bit)");
  84       }
  85       input = new byte[msgSize];
  86       for (int i=0; i<input.length; i++) {
  87         input[i] = (byte) (i & 0xff);
  88       }
  89 
  90       cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
  91       dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
  92 
  93       ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0);

  94       IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]);
  95 
  96       cipher.init(Cipher.ENCRYPT_MODE, key, initVector);




  97       algParams = cipher.getParameters();
  98       dCipher.init(Cipher.DECRYPT_MODE, key, algParams);
  99       if (threadId == 0) {
 100         childShowCipher();
 101       }
 102 
 103       // do one encode and decode in preparation
 104       // this will also create the encode buffer and decode buffer
 105       encode = cipher.doFinal(input);
 106       decode = dCipher.doFinal(encode);
 107       if (checkOutput) {
 108         expectedEncode = (byte[]) encode.clone();
 109         expectedDecode = (byte[]) decode.clone();
 110         showArray(key.getEncoded()  ,  "key:    ");
 111         showArray(input,  "input:  ");
 112         showArray(encode, "encode: ");
 113         showArray(decode, "decode: ");
 114       }
 115     }
 116     catch (Exception e) {




  37 import java.util.Arrays;
  38 
  39 abstract public class TestAESBase {
  40   int msgSize = Integer.getInteger("msgSize", 646);
  41   boolean checkOutput = Boolean.getBoolean("checkOutput");
  42   boolean noReinit = Boolean.getBoolean("noReinit");
  43   int keySize = Integer.getInteger("keySize", 128);
  44   String algorithm = System.getProperty("algorithm", "AES");
  45   String mode = System.getProperty("mode", "CBC");
  46   byte[] input;
  47   byte[] encode;
  48   byte[] expectedEncode;
  49   byte[] decode;
  50   byte[] expectedDecode;
  51   Random random = new Random(0);
  52   Cipher cipher;
  53   Cipher dCipher;
  54   String paddingStr = "PKCS5Padding";
  55   AlgorithmParameters algParams;
  56   SecretKey key;

  57 
  58   static int numThreads = 0;
  59   int  threadId;
  60   static synchronized int getThreadId() {
  61     int id = numThreads;
  62     numThreads++;
  63     return id;
  64   }
  65 
  66   abstract public void run();
  67 
  68   public void prepare() {
  69     try {
  70     System.out.println("\nalgorithm=" + algorithm + ", mode=" + mode + ", msgSize=" + msgSize + ", keySize=" + keySize + ", noReinit=" + noReinit + ", checkOutput=" + checkOutput);
  71 
  72       int keyLenBytes = (keySize == 0 ? 16 : keySize/8);
  73       byte keyBytes[] = new byte[keyLenBytes];
  74       if (keySize == 128)
  75         keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};
  76       else
  77         random.nextBytes(keyBytes);
  78 
  79       key = new SecretKeySpec(keyBytes, algorithm);
  80       if (threadId == 0) {
  81         System.out.println("Algorithm: " + key.getAlgorithm() + "("
  82                            + key.getEncoded().length * 8 + "bit)");
  83       }
  84       input = new byte[msgSize];
  85       for (int i=0; i<input.length; i++) {
  86         input[i] = (byte) (i & 0xff);
  87       }
  88 
  89       cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
  90       dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
  91 
  92       if (mode.equals("CBC")) {
  93         int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0);
  94         IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]);

  95         cipher.init(Cipher.ENCRYPT_MODE, key, initVector);
  96       } else {
  97         algParams = cipher.getParameters();
  98         cipher.init(Cipher.ENCRYPT_MODE, key, algParams);
  99       }
 100       algParams = cipher.getParameters();
 101       dCipher.init(Cipher.DECRYPT_MODE, key, algParams);
 102       if (threadId == 0) {
 103         childShowCipher();
 104       }
 105 
 106       // do one encode and decode in preparation
 107       // this will also create the encode buffer and decode buffer
 108       encode = cipher.doFinal(input);
 109       decode = dCipher.doFinal(encode);
 110       if (checkOutput) {
 111         expectedEncode = (byte[]) encode.clone();
 112         expectedDecode = (byte[]) decode.clone();
 113         showArray(key.getEncoded()  ,  "key:    ");
 114         showArray(input,  "input:  ");
 115         showArray(encode, "encode: ");
 116         showArray(decode, "decode: ");
 117       }
 118     }
 119     catch (Exception e) {


test/compiler/7184394/TestAESBase.java
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File