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!

javax.crypto.BadPaddingException: Given final block not properly padded

843811May 22 2008 — edited Jan 29 2009
Hello All,

not sure if someone had the same problem as I having with crypto utility.
This code if compiled on Java 1.4 works just fine in Linux and Windows enviroments; However, lately I've recompiled it on Java 1.5 and I have no problem with it on Windows, and it is failed in Linux

If any one knows what is wrong and had similar problem before, please help
Thank you in advance,

Oleg

Here the explanation what is going on....

I have 4 methods:

public String decodeCC(String encodedString)
--- this failing with BadPaddingException
public String decodePassword(String encoded_password)
--- failing with BadPaddingException
public String encodeCC(String cardNumber)
---- produced incorrect results
public String encodePassword(String password)
---- produced incorrect results

INFO: Loading XML bean definitions from file [root/olegs_test/MAAS/./conf/security.xml]
May 21, 2008 5:51:55 PM com.mixonic.security.action.MixonicCryptoImpl decodeCC
SEVERE: javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA12275)
at javax.crypto.Cipher.doFinal(DashoA12275)
at com.mixonic.security.action.MixonicCryptoImpl.decodeCC(MixonicCryptoImpl.java:49)

here is the code:
public interface ICrypto {
	public String encodePassword(String  password);
	public String decodePassword (String encoded_password);
	public String decodeCC(String encodedString);
	public String encodeCC(String cardNumber);
}
import java.security.SecureRandom;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.mixonic.security.interfaces.IMixonicCrypto;
import com.mixonic.util.GOPTUtil;
import com.sun.crypto.provider.SunJCE;




public class CryptoImpl implements ICrypto {
    private static final Log logger = LogFactory.getLog(CryptoImpl.class);

    private static final String xform = "DES/CBC/PKCS5Padding";
    private static final String s1 = "m1x3ef";

    private static byte[] iv =
    { 0x0a, 0x01, 0x02, 0x03, 0x04, 0x0b, 0x0c, 0x0d };

    private static BASE64Encoder base64Encoder = new BASE64Encoder();
    private static BASE64Decoder base64Decoder = new BASE64Decoder(); 


public String decodeCC(String encodedString) {
        SecretKey key = getKey(xform);
        byte[] outBytes = null;
        try {
            byte--] inpBytes = base64Decoder.decodeBuffer(encodedString);--
--            Cipher cipher = Cipher.getInstance(xform, "SunJCE");--
--            IvParameterSpec ips = new IvParameterSpec(iv);--
--            cipher.init(Cipher.DECRYPT_MODE, key, ips);--
--            outBytes = cipher.doFinal(inpBytes);--
--        } catch (Exception e) {--
--            logger.error(GOPTUtil.getStackTraceAsString(e));--
--            return null;--
--        }--
--        return (new String(outBytes));--
--    }--

--/**--
--     *  Decodes a password using the two way encryption hash RC4. This encoded--
--     *  password is used for persistant storage in the database.--
--     *--
--     * @param String password - The encoded password string--
--     * @return String - the decoded password string--
--     *--
--     */--
--    public String decodePassword(String encoded_password) {--
--        SecretKey key = getKey(xform);--
--        byte[-- outBytes = null;
        try {
            byte[] inpBytes = base64Decoder.decodeBuffer(encoded_password);
            Cipher cipher = Cipher.getInstance(xform, "SunJCE");
            IvParameterSpec ips = new IvParameterSpec(iv);
            cipher.init(Cipher.DECRYPT_MODE, key, ips);
            outBytes = cipher.doFinal(inpBytes);
        } catch (Exception e) {
            logger.error(GOPTUtil.getStackTraceAsString(e));
            return null;
        }
        return (new String(outBytes));
    }

public String encodeCC(String cardNumber) {
        SecretKey key = getKey(xform);
        byte[] inpBytes = cardNumber.getBytes();
        byte--] outBytes = null;--
--        try {--
--            Cipher cipher = Cipher.getInstance(xform, "SunJCE");--
--            IvParameterSpec ips = new IvParameterSpec(iv);--
--            cipher.init(Cipher.ENCRYPT_MODE, key, ips);--
--            outBytes = cipher.doFinal(inpBytes);--
--            // return (new String(CodecsDF.encode(outBytes)));--
--        } catch (Exception e) {--
--            logger.error(GOPTUtil.getStackTraceAsString(e));--
--            return null;--
--        }--
--        return (base64Encoder.encode(outBytes));--
--    }--

--/**--
--     *  Encodes a password using the two way encryption hash RC4. This encoded--
--     *  password is used for persistant storage in the database.--
--     *--
--     * @param String password - The password string to encode--
--     * @return String - the encoded password.--
--     *--
--     */--
--    public String encodePassword(String password) {--
--        SecretKey key = getKey(xform);--
--        byte[-- inpBytes = password.getBytes();
        byte[] outBytes = null;
        try {
            Cipher cipher = Cipher.getInstance(xform, "SunJCE");
            IvParameterSpec ips = new IvParameterSpec(iv);
            cipher.init(Cipher.ENCRYPT_MODE, key, ips);
            outBytes = cipher.doFinal(inpBytes);
            // return (new String(CodecsDF.encode(outBytes)));
        } catch (Exception e) {
            logger.error(GOPTUtil.getStackTraceAsString(e));
            return null;
        }
        return (base64Encoder.encode(outBytes));
    }
private SecretKey getKey(String algorithm) {
        SecretKey key=null;
        try {
//            Security.addProvider(new com.sun.crypto.provider.SunJCE());   
            Security.addProvider(new SunJCE());
            SecureRandom securerandom = new SecureRandom(s1.getBytes());
            KeyGenerator kg = KeyGenerator.getInstance("DES");
            kg.init(56, securerandom); // 56 is the keysize. Fixed for DES
            key = kg.generateKey();
        } catch (Exception e){
            logger.error(GOPTUtil.getStackTraceAsString(e));
            return null;
        }
        return key;
    }
   }
public class CryptoTest extends TestCase {
	private static final Log 	logger = LogFactory.getLog(CryptoTest.class);

	private static final String CIPHER_CC		= 	"0kYfp1dLEDX+8XREmmkJh9CDNcPtNo6O";
	private static final String DECODED_CC		=	"5401683016228691";
	private static final String DECODED_PSSWD1	=	"m1xD3v";
	private static final String CIPHER_PSSWD1	=	"7BwvCKOTo+k=";
	private static final String DECODED_PSSWD2	=	"m1x0n1c";
	private static final String CIPHER_PSSWD2	=	"OqafmxbAXuE=";

	private ICrypto testCrypto;
	private BeanFactory factory;

	public CryptoTest() {
		factory = SpringUtil.getBeanFactory();
	}

	/**
	 * @param name
*/*
*	public CryptoTest(String name) {*
*		super(name);*
*		factory = SpringUtil.getBeanFactory();*
*	}*

*	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
*/*
*	protected void setUp() throws Exception {*
*		super.setUp();*
*		testCrypto = (ICrypto)factory.getBean("crypto");*
*	}*

*	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
		testCrypto = null;
	}


	public void testDecodeCC() {
		String cc_card = testCrypto.decodeCC(CryptoTest.CIPHER_CC);
		logger.info("testDecodeCCString: \nEncoded: " + CryptoTest.CIPHER_CC + "\n" +
				"Decoded: " + cc_card + "\n");
		assertEquals(CryptoTest.DECODED_CC, cc_card);
	}


	public void testDecodePassword() {
		String decodedPsswd = testCrypto.decodePassword(CryptoTest.CIPHER_PSSWD1);
		logger.info("testDecodePassword: \nCipher password: " + CryptoTest.CIPHER_PSSWD1 + "\n" +
				"Encoded password: " + decodedPsswd + "\n");
		assertEquals(CryptoTest.DECODED_PSSWD1, decodedPsswd);
	}


	public void testEncodeCC() {
		String cipherCC = testCrypto.encodeCC(CryptoTest.DECODED_CC);
		logger.info("testEncodeCCString: \nDecoded ccard: " + CryptoTest.DECODED_CC + "\n" + 
				"Encoded cipher: " + cipherCC + "\n");
		assertEquals(CryptoTest.CIPHER_CC, cipherCC);
	}


	public void testEncodePassword() {
		String encodedPass = testCrypto.encodePassword(CryptoTest.DECODED_PSSWD2);
		logger.info("testEncodePassword: \nPassword: " + CryptoTest.DECODED_PSSWD2 + "\n" +
				"Cipher: " + encodedPass + "\n");
		assertEquals(CryptoTest.CIPHER_PSSWD2, encodedPass);
	}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 26 2009
Added on May 22 2008
10 comments
3,029 views