1f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson/*
2f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * Copyright (C) 2009 The Android Open Source Project
3f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson *
4f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * you may not use this file except in compliance with the License.
6f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * You may obtain a copy of the License at
7f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson *
8f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
9f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson *
10f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * See the License for the specific language governing permissions and
14f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson * limitations under the License.
15f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson */
16f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
17f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonpackage tests.security;
18f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
19f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.InvalidKeyException;
20f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.Key;
21f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport java.security.NoSuchAlgorithmException;
22f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport javax.crypto.BadPaddingException;
23f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport javax.crypto.Cipher;
24f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport javax.crypto.IllegalBlockSizeException;
25f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport javax.crypto.NoSuchPaddingException;
26f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonimport junit.framework.Assert;
27f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
28f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilsonpublic abstract class CipherHelper<T> extends TestHelper<T> {
29f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
30f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    private final String algorithmName;
31f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    private final String plainData;
32f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    private final int mode1;
33f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    private final int mode2;
34f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
35f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    public CipherHelper(String algorithmName, String plainData, int mode1, int mode2) {
36f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        this.algorithmName = algorithmName;
37f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        this.plainData = plainData;
38f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        this.mode1 = mode1;
39f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        this.mode2 = mode2;
40f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    }
41f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
42420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root    public void test(Key encryptKey, Key decryptKey) throws Exception {
43420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root        Cipher cipher = Cipher.getInstance(algorithmName);
44420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root        cipher.init(mode1, encryptKey);
45420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root        byte[] encrypted = cipher.doFinal(plainData.getBytes());
46f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
47420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root        cipher.init(mode2, decryptKey);
48420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root        byte[] decrypted = cipher.doFinal(encrypted);
49f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson        String decryptedString = new String(decrypted);
50f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson
51420ea38aecdef0f5895c5e82751ebabe26bc0bd5Kenny Root        Assert.assertEquals("transformed data does not match", plainData, decryptedString);
52f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson    }
53f979bbd1277c77ca945ad981e7864fb4e9f6ae05Jesse Wilson}
54