aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/spongycastle/core/src/main/j2me/org/spongycastle/crypto/examples/MIDPTest.java
blob: 32908a67e79684d7861de72229b32eeff93f0788 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package org.spongycastle.crypto.examples;

import java.io.*;
import java.lang.*;

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

import org.spongycastle.util.test.*;
import org.spongycastle.util.encoders.*;

import org.spongycastle.crypto.*;
import org.spongycastle.crypto.paddings.*;
import org.spongycastle.crypto.engines.*;
import org.spongycastle.crypto.modes.*;
import org.spongycastle.crypto.params.*;

/**
 * MIDP is a simple graphics application for the J2ME CLDC/MIDP.
 * 
 * It has hardcoded values for the key and plain text. It also performs the
 * standard testing for the chosen cipher, and displays the results.
 * 
 * This example shows how to use the light-weight API and a symmetric cipher.
 * 
 */
public class MIDPTest extends MIDlet
{
    private Display             d           = null;

    private boolean             doneEncrypt = false;

    private String              key         = "0123456789abcdef0123456789abcdef";
    private String              plainText   = "www.bouncycastle.org";
    private byte[]              keyBytes    = null;
    private byte[]              cipherText  = null;
    private BufferedBlockCipher cipher      = null;

    private String[]            cipherNames = {"DES", "DESede", "IDEA", "Rijndael", "Twofish"};

    private Form                output      = null;

    public void startApp()
    {
        Display.getDisplay(this).setCurrent(output);
    }

    public void pauseApp()
    {

    }

    public void destroyApp(boolean unconditional)
    {

    }

    public MIDPTest()
    {
        output = new Form("BouncyCastle");
        output.append("Key: " + key.substring(0, 7) + "...\n");
        output.append("In : " + plainText.substring(0, 7) + "...\n");

        cipherText = performEncrypt(Hex.decode(key.getBytes()), plainText);
        String ctS = new String(Hex.encode(cipherText));

        output.append("\nCT : " + ctS.substring(0, 7) + "...\n");

        String decryptText = performDecrypt(Hex.decode(key.getBytes()), cipherText);

        output.append("PT : " + decryptText.substring(0, 7) + "...\n");

        if (decryptText.compareTo(plainText) == 0)
        {
            output.append("Success");
        }
        else
        {
            output.append("Failure");
            message("[" + plainText + "]");
            message("[" + decryptText + "]");
        }

    }

    private byte[] performEncrypt(byte[] key, String plainText)
    {
        byte[] ptBytes = plainText.getBytes();

        cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(getEngineInstance()));

        String name = cipher.getUnderlyingCipher().getAlgorithmName();
        message("Using " + name);

        cipher.init(true, new KeyParameter(key));

        byte[] rv = new byte[cipher.getOutputSize(ptBytes.length)];

        int oLen = cipher.processBytes(ptBytes, 0, ptBytes.length, rv, 0);
        try
        {
            cipher.doFinal(rv, oLen);
        }
        catch (CryptoException ce)
        {
            message("Ooops, encrypt exception");
            status(ce.toString());
        }
        return rv;
    }

    private String performDecrypt(byte[] key, byte[] cipherText)
    {
        cipher.init(false, new KeyParameter(key));

        byte[] rv = new byte[cipher.getOutputSize(cipherText.length)];

        int oLen = cipher.processBytes(cipherText, 0, cipherText.length, rv, 0);
        try
        {
            cipher.doFinal(rv, oLen);
        }
        catch (CryptoException ce)
        {
            message("Ooops, decrypt exception");
            status(ce.toString());
        }
        return new String(rv).trim();
    }

    private int whichCipher()
    {
        return 4; // DES
    }

    private BlockCipher getEngineInstance()
    {
        // returns a block cipher according to the current
        // state of the radio button lists. This is only
        // done prior to encryption.
        BlockCipher rv = null;

        switch (whichCipher())
        {
            case 0 :
                rv = new DESEngine();
                break;
            case 1 :
                rv = new DESedeEngine();
                break;
            case 2 :
                rv = new IDEAEngine();
                break;
            case 3 :
                rv = new RijndaelEngine();
                break;
            case 4 :
                rv = new TwofishEngine();
                break;
            default :
                rv = new DESEngine();
                break;
        }
        return rv;
    }

    public void message(String s)
    {
        System.out.println("M:" + s);
    }

    public void status(String s)
    {
        System.out.println("S:" + s);
    }

}