1/*
2 * Copyright (C) 2008 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 */
16package org.apache.harmony.crypto.tests.javax.crypto.func;
17
18
19import java.security.KeyPair;
20import java.security.KeyPairGenerator;
21
22import javax.crypto.Cipher;
23
24public class CipherRSAThread extends CipherThread {
25
26    CipherRSAThread(String name, int[] keys, String[] modes, String[] paddings) {
27        super(name, keys, modes, paddings);
28    }
29
30    @Override
31    public void crypt() throws Exception {
32        byte[] output = new byte[256];
33        byte[] decrypted = new byte[256];
34        int dataBlock = 20;
35        byte[] input  =  getData().substring(0, dataBlock).getBytes();
36        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
37        kpg.initialize(getKeyLength());
38        KeyPair kp = kpg.generateKeyPair();
39
40        Cipher cip = Cipher.getInstance(getAlgName() + "/" + getMode() + "/" +
41                getPadding());
42        cip.init(Cipher.ENCRYPT_MODE, kp.getPublic());
43        cip.doFinal(input, 0, input.length, output);
44        int outputSize = cip.getOutputSize(input.length);
45        cip.init(Cipher.DECRYPT_MODE, kp.getPrivate());
46        cip.doFinal(output, 0, outputSize, decrypted);
47
48        if ("NOPADDING".equals(getPadding())) {
49            checkPaddedEncodedData(input, decrypted, outputSize - input.length);
50        } else {
51            checkEncodedData(input, decrypted);
52        }
53    }
54}
55