TripleDes.java
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.engines.DESedeEngine;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
/**
*
* @author Pradeep
*/
public class TripleDes {
PaddedBufferedBlockCipher encryptCipher;
PaddedBufferedBlockCipher decryptCipher;
// Buffers used to transport the bytes from one stream to another
byte[] key = null; //secret key for secure encryption and decryption
public TripleDes() {
key = "MYTESTTRIPLEDESENCRYPTO".getBytes();
// calling the InitCiphers() method to initilize the PaddedBufferedBlockCipher
InitCiphers();
}
/**
*
* @param keyBytes Key value
*/
public TripleDes(byte[] keyBytes) {
// TripleDES Key
key = new byte[keyBytes.length];
System.arraycopy(keyBytes, 0, key, 0, keyBytes.length);
// calling the InitCiphers() method to initilize the PaddedBufferedBlockCipher
InitCiphers();
}
/**
*
* Encrypt
*
* This method encrypt the plain text and returns the encrypted data in base64
*
* @param plainText
* @return String
* @throws Exception
*/
public String encrypt(String plainText) throws Exception {
byte[] input = plainText.getBytes();
encryptCipher.init(true, new KeyParameter(key));
byte[] cipherText = new byte[encryptCipher.getOutputSize(input.length)];
int outputLen = encryptCipher.processBytes(input, 0, input.length, cipherText, 0);
try {
encryptCipher.doFinal(cipherText, outputLen);
} catch (CryptoException ce) {
System.err.println(ce);
throw new Exception(ce.toString());
} catch (Exception ce) {
System.err.println(ce);
throw new Exception(ce.toString());
} finally {
ResetCiphers();
}
return new String(Base64Coder.encode(cipherText)).trim();
}
/**
* decrypt
* Decrypt the encrypted string
* @param enctryptedText
* @return
* @throws Exception
*/
public String decrypt(String enctryptedText) throws Exception {
byte[] input = Base64Coder.decode(enctryptedText);
decryptCipher.init(false, new KeyParameter(key));
byte[] cipherText = new byte[decryptCipher.getOutputSize(input.length)];
int outputLen = decryptCipher.processBytes(input, 0, input.length, cipherText, 0);
try {
decryptCipher.doFinal(cipherText, outputLen);
} catch (CryptoException ce) {
System.err.println(ce);
throw new Exception(ce.toString());
} catch (Exception ce) {
System.err.println(ce);
throw new Exception(ce.toString());
} finally {
ResetCiphers();
}
return bytes2String(cipherText);
}
/**
* Initilize the ciphers for encryption and decryption
*
*/
private void InitCiphers() {
//Creates encryptCipher Object DESEDE
encryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());
// encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine())); // DESEDE CBC MODE CIPHER
//set Key parameters and Encrypt mode
encryptCipher.init(true, new KeyParameter(key));
//Creates decryptCipher Object
decryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());
// decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine())); // DESEDE CBC MODE CIPHER
//set Key parameters and Encrypt mode
decryptCipher.init(false, new KeyParameter(key));
}
/**
* Reset the cipher Objects
*/
private void ResetCiphers() {
if (encryptCipher != null) {
encryptCipher.reset();
}
if (decryptCipher != null) {
decryptCipher.reset();
}
}
/**
* bytes2String
* convert byte array to string
* @param bytes
* @return
*/
private static String bytes2String(byte[] bytes) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
stringBuffer.append((char) bytes[i]);
}
return stringBuffer.toString();
}
}
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.engines.DESedeEngine;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
/**
*
* @author Pradeep
*/
public class TripleDes {
PaddedBufferedBlockCipher encryptCipher;
PaddedBufferedBlockCipher decryptCipher;
// Buffers used to transport the bytes from one stream to another
byte[] key = null; //secret key for secure encryption and decryption
public TripleDes() {
key = "MYTESTTRIPLEDESENCRYPTO".getBytes();
// calling the InitCiphers() method to initilize the PaddedBufferedBlockCipher
InitCiphers();
}
/**
*
* @param keyBytes Key value
*/
public TripleDes(byte[] keyBytes) {
// TripleDES Key
key = new byte[keyBytes.length];
System.arraycopy(keyBytes, 0, key, 0, keyBytes.length);
// calling the InitCiphers() method to initilize the PaddedBufferedBlockCipher
InitCiphers();
}
/**
*
* Encrypt
*
* This method encrypt the plain text and returns the encrypted data in base64
*
* @param plainText
* @return String
* @throws Exception
*/
public String encrypt(String plainText) throws Exception {
byte[] input = plainText.getBytes();
encryptCipher.init(true, new KeyParameter(key));
byte[] cipherText = new byte[encryptCipher.getOutputSize(input.length)];
int outputLen = encryptCipher.processBytes(input, 0, input.length, cipherText, 0);
try {
encryptCipher.doFinal(cipherText, outputLen);
} catch (CryptoException ce) {
System.err.println(ce);
throw new Exception(ce.toString());
} catch (Exception ce) {
System.err.println(ce);
throw new Exception(ce.toString());
} finally {
ResetCiphers();
}
return new String(Base64Coder.encode(cipherText)).trim();
}
/**
* decrypt
* Decrypt the encrypted string
* @param enctryptedText
* @return
* @throws Exception
*/
public String decrypt(String enctryptedText) throws Exception {
byte[] input = Base64Coder.decode(enctryptedText);
decryptCipher.init(false, new KeyParameter(key));
byte[] cipherText = new byte[decryptCipher.getOutputSize(input.length)];
int outputLen = decryptCipher.processBytes(input, 0, input.length, cipherText, 0);
try {
decryptCipher.doFinal(cipherText, outputLen);
} catch (CryptoException ce) {
System.err.println(ce);
throw new Exception(ce.toString());
} catch (Exception ce) {
System.err.println(ce);
throw new Exception(ce.toString());
} finally {
ResetCiphers();
}
return bytes2String(cipherText);
}
/**
* Initilize the ciphers for encryption and decryption
*
*/
private void InitCiphers() {
//Creates encryptCipher Object DESEDE
encryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());
// encryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine())); // DESEDE CBC MODE CIPHER
//set Key parameters and Encrypt mode
encryptCipher.init(true, new KeyParameter(key));
//Creates decryptCipher Object
decryptCipher = new PaddedBufferedBlockCipher(new DESedeEngine());
// decryptCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine())); // DESEDE CBC MODE CIPHER
//set Key parameters and Encrypt mode
decryptCipher.init(false, new KeyParameter(key));
}
/**
* Reset the cipher Objects
*/
private void ResetCiphers() {
if (encryptCipher != null) {
encryptCipher.reset();
}
if (decryptCipher != null) {
decryptCipher.reset();
}
}
/**
* bytes2String
* convert byte array to string
* @param bytes
* @return
*/
private static String bytes2String(byte[] bytes) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
stringBuffer.append((char) bytes[i]);
}
return stringBuffer.toString();
}
}
Note:Triple Des (DESede) Encryption in J2me
Source code Download Link