Tue Jul 22, 2008 3:42 am
import java.io.*;
import org.bouncycastle.crypto.*;
import org.bouncycastle.crypto.engines.*;
import org.bouncycastle.crypto.modes.*;
import org.bouncycastle.crypto.params.*;
public class Encryptor {
private BufferedBlockCipher cipher;
private KeyParameter key;
// Initialize the cryptographic engine.
// The key array should be at least 16 characters long.
public Encryptor ( byte[] key , byte cipherType){
BlockCipher theCipher = new RijndaelEngine ();
cipher = new CTSBlockCipher(new CBCBlockCipher (theCipher)) ;
this.key = new KeyParameter ( key );
}
// Initialize the cryptographic engine.
// The string should be at least 16 characters long.
public Encryptor ( String key , byte cipherType){
this ( key.getBytes (), cipherType );
}
// Private routine that does the gritty work.
private byte[] callCipher ( byte[] data )
throws CryptoException {
int size =
cipher.getOutputSize ( data.length );
byte[] result = new byte[ size ];
int olen = cipher.processBytes ( data, 0,
data.length, result, 0 );
olen += cipher.doFinal ( result, olen );
if( olen < size ){
byte[] tmp = new byte[ olen ];
System.arraycopy (
result, 0, tmp, 0, olen );
result = tmp;
}
return result;
}
// Encrypt arbitrary byte array, returning the
// encrypted data in a different byte array.
public synchronized byte[] encrypt ( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return new byte[0];
}
cipher.init ( true, key );
return callCipher ( data );
}
// Encrypts a string.
public byte[] encryptString ( String data )
throws CryptoException {
if( data == null || data.length () == 0 ){
return new byte[0];
}
byte[] result = null;
try{
result = data.getBytes ("UTF-8");
}catch (UnsupportedEncodingException e){
result = data.getBytes () ;
}
return encrypt ( result );
}
public String encryptStrings ( String data )
throws CryptoException {
if( data == null || data.length () == 0 ){
return new String ();
}
byte[] result = null;
try{
result = data.getBytes ("UTF-8");
}catch (UnsupportedEncodingException e){
result = data.getBytes () ;
}
byte[] encrypted = encrypt ( result );
return new String (encrypted);
}
// Decrypts arbitrary data.
public synchronized byte[] decrypt ( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return new byte[0];
}
cipher.init ( false, key );
return callCipher ( data );
}
// Decrypts a string that was previously encoded
// using encryptString.
public String decryptString ( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return "";
}
String result = null;
try{
result = new String ( decrypt ( data ),"UTF-8" );
}catch (UnsupportedEncodingException e){
result = new String ( decrypt ( data ));
}
return result;
}
}
Tue Jul 22, 2008 2:56 pm
Tue Jul 22, 2008 4:49 pm
Tue Jul 22, 2008 10:28 pm
Wed Jul 23, 2008 3:41 am
Sat Jul 26, 2008 6:08 pm
import java.io.*;
import org.bouncycastle.crypto.*;
import org.bouncycastle.crypto.engines.*;
import org.bouncycastle.crypto.modes.*;
import org.bouncycastle.crypto.params.*;
public class Encryptor {
private BufferedBlockCipher cipher;
private KeyParameter key;
// Initialize the cryptographic engine.
// The key array should be at least 16 characters long.
public Encryptor ( byte[] key , byte cipherType){
BlockCipher theCipher = new RijndaelEngine ();
cipher = new CTSBlockCipher(new CBCBlockCipher (theCipher)) ;
this.key = new KeyParameter ( key );
}
// Initialize the cryptographic engine.
// The string should be at least 16 characters long.
public Encryptor ( String key , byte cipherType){
this ( key.getBytes (), cipherType );
}
// Private routine that does the gritty work.
private byte[] callCipher ( byte[] data )
throws CryptoException {
int size =
cipher.getOutputSize ( data.length );
byte[] result = new byte[ size ];
int olen = cipher.processBytes ( data, 0,
data.length, result, 0 );
olen += cipher.doFinal ( result, olen );
if( olen < size ){
byte[] tmp = new byte[ olen ];
System.arraycopy (
result, 0, tmp, 0, olen );
result = tmp;
}
return result;
}
// Encrypt arbitrary byte array, returning the
// encrypted data in a different byte array.
public synchronized byte[] encrypt ( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return new byte[0];
}
cipher.init ( true, key );
return callCipher ( data );
}
// Encrypts a string.
public byte[] encryptString ( String data )
throws CryptoException {
if( data == null || data.length () == 0 ){
return new byte[0];
}
byte[] result = null;
try{
result = data.getBytes ("UTF-8");
}catch (UnsupportedEncodingException e){
result = data.getBytes () ;
}
return encrypt ( result );
}
public String encryptStrings ( String data )
throws CryptoException {
if( data == null || data.length () == 0 ){
return new String ();
}
byte[] result = null;
try{
result = data.getBytes ("UTF-8");
}catch (UnsupportedEncodingException e){
result = data.getBytes () ;
}
byte[] encrypted = encrypt ( result );
return new String (encrypted);
}
// Decrypts arbitrary data.
public synchronized byte[] decrypt ( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return new byte[0];
}
cipher.init ( false, key );
return callCipher ( data );
}
// Decrypts a string that was previously encoded
// using encryptString.
public String decryptString ( byte[] data )
throws CryptoException {
if( data == null || data.length == 0 ){
return "";
}
String result = null;
try{
result = new String ( decrypt ( data ),"UTF-8" );
}catch (UnsupportedEncodingException e){
result = new String ( decrypt ( data ));
}
return result;
}
}
hi arcrueid i have tried your code there seems to be a problem everytime i execute the program on my phone when i encrypt, the text message seems to be working fine. but when i decrypt the message the original text message seems to be lacking some parts. did you encounter this error too?
Mon Jul 28, 2008 7:34 am
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.