Home | History | Annotate | Download | only in security
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package tests.security;
     18 
     19 import java.security.AlgorithmParameters;
     20 import java.security.InvalidAlgorithmParameterException;
     21 import java.security.InvalidKeyException;
     22 import java.security.KeyPair;
     23 import java.security.KeyPairGenerator;
     24 import java.security.NoSuchAlgorithmException;
     25 import java.util.Arrays;
     26 import javax.crypto.BadPaddingException;
     27 import javax.crypto.Cipher;
     28 import javax.crypto.IllegalBlockSizeException;
     29 import javax.crypto.NoSuchPaddingException;
     30 import junit.framework.Assert;
     31 
     32 public class AlgorithmParameterAsymmetricHelper extends TestHelper<AlgorithmParameters> {
     33 
     34     private static final String plainData = "some data to encrypt and decrypt";
     35     private final String algorithmName;
     36 
     37     public AlgorithmParameterAsymmetricHelper(String algorithmName) {
     38         this.algorithmName = algorithmName;
     39     }
     40 
     41     @Override
     42     public void test(AlgorithmParameters parameters) {
     43 
     44         KeyPairGenerator generator = null;
     45         try {
     46             generator = KeyPairGenerator.getInstance(algorithmName);
     47         } catch (NoSuchAlgorithmException e) {
     48             Assert.fail(e.getMessage());
     49         }
     50 
     51         generator.initialize(1024);
     52 
     53         KeyPair keyPair = generator.generateKeyPair();
     54 
     55 
     56         Cipher cipher = null;
     57         try {
     58             cipher = Cipher.getInstance(algorithmName);
     59         } catch (NoSuchAlgorithmException e) {
     60             Assert.fail(e.getMessage());
     61         } catch (NoSuchPaddingException e) {
     62             Assert.fail(e.getMessage());
     63         }
     64 
     65         try {
     66             cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic(), parameters);
     67         } catch (InvalidKeyException e) {
     68             Assert.fail(e.getMessage());
     69         } catch (InvalidAlgorithmParameterException e) {
     70             Assert.fail(e.getMessage());
     71         }
     72 
     73         byte[] bs = null;
     74         try {
     75             bs = cipher.doFinal(plainData.getBytes());
     76         } catch (IllegalBlockSizeException e) {
     77             Assert.fail(e.getMessage());
     78         } catch (BadPaddingException e) {
     79             Assert.fail(e.getMessage());
     80         }
     81 
     82         try {
     83             cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate(), parameters);
     84         } catch (InvalidKeyException e) {
     85             Assert.fail(e.getMessage());
     86         } catch (InvalidAlgorithmParameterException e) {
     87             Assert.fail(e.getMessage());
     88         }
     89 
     90         byte[] decrypted = null;
     91         try {
     92             decrypted = cipher.doFinal(bs);
     93         } catch (IllegalBlockSizeException e) {
     94             Assert.fail(e.getMessage());
     95         } catch (BadPaddingException e) {
     96             Assert.fail(e.getMessage());
     97         }
     98 
     99         Assert.assertTrue(Arrays.equals(plainData.getBytes(), decrypted));
    100     }
    101 }
    102