[JSch-J2ME] AES in JSch for J2ME CDC
Status: Alpha
Brought to you by:
ymnk
|
From: Radovan S. <rad...@te...> - 2006-04-28 13:32:22
|
Hello!
For a project I'm currently developing I use latest version of JSch for J2ME
CDC (jsch-0.1.17-J2MECDC-20050224). In that version there is no support for
AES. I tried to add it by mimicing BlowfishCBC.java and TripleDESCBC.java
from com.jcraft.jsch.bc however it doesn't work. It ends up with
IndexOutOfBoundsException somewhere in Session - decryption fails probably.
As I am no expert in cryptography I cannot find the reason. Would anyone be
able to help me with this? Thank you very much.
Here's what I've put together for AES256CBC.java:
/* -*-mode:java; c-basic-offset:2; -*- */
/*
Copyright (c) 2004 ymnk, JCraft,Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
3. The names of the authors may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT,
INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcraft.jsch.bc;
import com.jcraft.jsch.Cipher;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
public class AES256CBC implements Cipher {
private static final int ivsize = 16;
private static final int bsize = 32;
private BlockCipher cipher;
public int getIVSize() {
return ivsize;
}
public int getBlockSize() {
return bsize;
}
public void init(int mode, byte[] key, byte[] iv) throws Exception {
byte[] tmp;
if (iv.length > ivsize) {
tmp = new byte[ivsize];
System.arraycopy(iv, 0, tmp, 0, tmp.length);
iv = tmp;
}
if (key.length > bsize) {
tmp = new byte[bsize];
System.arraycopy(key, 0, tmp, 0, tmp.length);
key = tmp;
}
try {
cipher = new CBCBlockCipher(new AESEngine());
CipherParameters param = new ParametersWithIV(
new KeyParameter(key), iv);
if (mode == ENCRYPT_MODE) {
cipher.init(true, param);
} else {
cipher.init(false, param);
}
} catch (Exception e) {
System.out.println(e);
}
}
public void update(byte[] foo, int s1, int len, byte[] bar, int s2)
throws Exception {
while (len >= 8) {
cipher.processBlock(foo, s1, bar, s2);
len -= 8;
s1 += 8;
s2 += 8;
}
}
}
Best regards
Radovan Skolnik
------------------------------------------------------------------
Ing. Radovan Skolnik
TEMPEST s.r.o.
Department of Enterprise Management
Plynarenska 7/B
821 09 Bratislava
Slovak Republic
E-mail: rad...@te...
www: http://www.tempest.sk
Tel: +421 (2) 502 67 174
Fax: +421 (2) 502 67 100
ITIL, eTOM, SLM, Networking, Telco, Management, Java and XML
------------------------------------------------------------------
|