Hi!
I don`t have a good notice... I have insert a new method in the class. With this method, I want to encrypt any string. I think that the class encrypts correctly any string whose length is 8bytes. This is the reason of the new method, I want to divide any string in blocks of 8 bytes to be encrypted later. If the length of the string is not multiple of 8, I insert a character in the end to fill in. The method doesn`t is correct but some of the blocks to encrypt, when I try to decrypt, are not ok....
I think that the class is not ok in all....Some strings are encrypted correctly...other no......
(( . Can anybody give me some explanation?
Code:
This is the code:
package util;
//import com.sun.kvem.midp.pim.formats.Base64Encoding;
import java.util.Vector;
import org.bouncycastle.crypto.*;
import org.bouncycastle.crypto.engines.*;
import org.bouncycastle.crypto.modes.*;
import org.bouncycastle.crypto.params.*;
import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.encoders.Base64Encoder;
//A simple example that uses the Bouncy Castle
//lightweight cryptography API to perform DES
//encryption of arbitrary data.
public class Encryptor {
private BufferedBlockCipher cipher;
private KeyParameter key;
// Initialize the cryptographic engine.
// The key array should be at least 8 bytes long.
public Encryptor( byte[] key ){
cipher = new PaddedBlockCipher(
new CBCBlockCipher(
new DESEngine() ) );
this.key = new KeyParameter( key );
}
// Initialize the cryptographic engine.
// The string should be at least 8 chars long.
/**
public Encryptor( String key ){
this( key.getBytes() );
}
*/
// 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];
}
try{
return encrypt( data.getBytes() );
}catch(Exception e){
e.printStackTrace();
return null;
}
}
// 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 "";
}
return new String( decrypt( data ) );
}
public String encryptAnyString(String chain)throws Exception{
// CONTROL BYTES (SUBDIVISION IN 8)
while (!(chain.length() % 8 == 0)){
chain = chain + "|";
}
System.out.println("Full chain: " + chain);
Vector bytesChain = new Vector();
//division in 8 bytes blocks
int i = 0;
while (chain.length() > 0){
String subchain = chain.substring(0, 8);
System.out.println("Subchain: " + subchain );
bytesChain.addElement(subchain);
i++;
chain = chain.substring(8, chain.length());
}
//encrypt all the 8 bytes blocks
for (int j = 0; j < bytesChain.size(); j++){
String chaneToEmcrypt = (String)bytesChain.elementAt(j);
bytesChain.removeElementAt(j);
String encryptedChain = new String(this.encryptString(chaneToEmcrypt));
System.out.println("encrypted chain: " + encryptedChain);
bytesChain.insertElementAt( encryptedChain, j);
System.out.println("decrypted chain: " + decryptString(encryptedChain.getBytes()));
}
String finalChain = new String();
for (int j = 0; j < bytesChain.size(); j++){
if (j > 0 ){
//I want to separate the differents encrypted strings with "////"
finalChain = finalChain + "////";
}
finalChain = finalChain + (String)bytesChain.elementAt(j);
}
System.out.println("the final chain: " + finalChain);
///////////////////////////////////////////////////////
return finalChain;
}
}