Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need explanation for DES/CBC/NoPadding

843810Apr 29 2002 — edited May 2 2002
Hallo,

I have the following problem:
when I try do use the encryption mode DES/CBC/NoPadding my program fails to decrypt
first 8 bytes. (I implement my own padding)

(may be I could just before encrypting the plain text insert
in the beginning of the message 8 bits of random data, but hasnot the
the function of the API do that stuff instead of me?)

I'd like to understand just this case, so please don't advice using another scheme or PKxxx
padding.

thanks in advance!

the code I use to encrypt:
private static byte[] encryptionCBC(byte[] baToEncode, SecretKey desKey, IvParameterSpec IV) 
        throws  IllegalBlockSizeException, BadPaddingException, 
                NoSuchAlgorithmException, NoSuchPaddingException, 
                InvalidAlgorithmParameterException, 
                InvalidKeyException, ShortBufferException  
{
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, desKey, IV);
    byte[] toEncrypt = transformToPadded(baToEncode);
    byte[] encrypted = new byte[(toEncrypt.length / 64) * c.getOutputSize(64)];
    for (int i = 0; i < toEncrypt.length/64; i++) {
        byte[] encryptedBlock = new byte[c.getOutputSize(64)];
        c.update(toEncrypt,i,64,encryptedBlock);
        System.arraycopy(encryptedBlock, 0, encrypted, c.getOutputSize(64) * i,
                             c.getOutputSize(64));
    }
    
    c.doFinal();
    return encrypted;
}//encryptionCBC
the code I use to decrypt:
private static byte[] decryptionCBC(byte[] baToDecode, SecretKey desKey, IvParameterSpec IV) 
        throws  IllegalBlockSizeException, BadPaddingException, 
                NoSuchAlgorithmException, NoSuchPaddingException, 
                InvalidAlgorithmParameterException,
                InvalidKeyException, ShortBufferException  
{

    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.DECRYPT_MODE, desKey, IV);
    byte[] decrypted = new byte[64 * baToDecode.length / c.getOutputSize(64)];
    for (int i = 0; i < baToDecode.length / c.getOutputSize(64); i++) {
        byte[] decryptedBlock = new byte[64];
        c.update(baToDecode, i, c.getOutputSize(64), decryptedBlock);
        System.arraycopy(decryptedBlock, 0, decrypted, 64 * i, 64);
    }
    
    c.doFinal();
    
    boolean flag = false;
    // remove padding if exists
    for(int i = 0; i<decrypted.length; i++){
        if (flag) {
            decrypted[i] = 0x20; // padding with space
        }
        //find the end of the message = Ctrl+Z
        if (!flag && decrypted[i] == 0x1A){
                decrypted=0x20; // the end of the message reached we can 
flag = true; // pad the rest with space
}
}
// byte[] iv = IV.getIV();
// for (int i=0; i<8; i++)
// decrypted[i] = (byte)(decrypted[i] ^ iv[i]);

return decrypted;
}//decryptionCBC

// to implement simple padding
// using ctrl-z to designate the end of the cleartext
private static byte[] transformToPadded(byte[] x) {
if (x.length % 64 == 0)
return x;
else{
byte[] tail = new byte[64 - x.length % 64];
java.util.Random rand = new java.util.Random();
rand.nextBytes(tail);
tail[0] = 0x1A; // Ctrl+Z
byte[] result = new byte[x.length + tail.length];
System.arraycopy(x, 0, result, 0, x.length);
System.arraycopy(tail, 0, result, x.length, tail.length);
return result;
}
}// transform

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 30 2002
Added on Apr 29 2002
2 comments
308 views