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!

using AES with java program and mysql

801571May 4 2010 — edited May 5 2010
hello. I am trying to use the function of the equivalent aes_encrypt() of mysql with my java program. I would like to get data, encrypt it using AES from my program then decrypt it from my mysql database using aes_decrypt(). I have come across certain resources that help me encrypt my data from my program but I cannot decrypt it from the database because I have no key as required by the mysql aes implementation. The mysql function of aes_decrypt() takes parameters of the data and the key to decrypt it, i.e aes_decrypt("encrypted data","1234"). However, with the java implementation of aes_encrypt, I cannot find the key i can use for decryption. Here is a sample of my program encyption code for the java encryption.
public class AES {
	public static void main(String[] args) {
		
		String strDataToEncrypt = new String();
		String strCipherText = new String();
		String strDecryptedText = new String();
		
		try{
		/**
		 *  Step 1. Generate an AES key using KeyGenerator
		 *  		Initialize the keysize to 128 
		 * 
		 */
		KeyGenerator keyGen = KeyGenerator.getInstance("AES");
		keyGen.init(128);
		SecretKey secretKey = keyGen.generateKey();
		
		/**
		 *  Step2. Create a Cipher by specifying the following parameters
		 * 			a. Algorithm name - here it is AES
		 */
		
		Cipher aesCipher = Cipher.getInstance("AES");
		
		/**
		 *  Step 3. Initialize the Cipher for Encryption 
		 */
		
		aesCipher.init(Cipher.ENCRYPT_MODE,secretKey);
		
		/**
		 *  Step 4. Encrypt the Data
		 *  		1. Declare / Initialize the Data. Here the data is of type String
		 *  		2. Convert the Input Text to Bytes
		 *  		3. Encrypt the bytes using doFinal method 
		 */
		
		strDataToEncrypt = "database";
		byte[] byteDataToEncrypt = strDataToEncrypt.getBytes();
		byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt); 
		strCipherText = new BASE64Encoder().encode(byteCipherText);
		System.out.println("Cipher Text generated using AES is " +strCipherText);
		
		/**
		 *  Step 5. Decrypt the Data
		 *  		1. Initialize the Cipher for Decryption 
		 *  		2. Decrypt the cipher bytes using doFinal method 
		 */
		
		aesCipher.init(Cipher.DECRYPT_MODE,secretKey,aesCipher.getParameters());
		byte[] byteDecryptedText = aesCipher.doFinal(byteCipherText);
		strDecryptedText = new String(byteDecryptedText);
		System.out.println(" Decrypted Text message is " +strDecryptedText);
		}
		
		catch (NoSuchAlgorithmException noSuchAlgo)
		{
			System.out.println(" No Such Algorithm exists " + noSuchAlgo);
		}
		
		catch (NoSuchPaddingException noSuchPad)
		{
			System.out.println(" No Such Padding exists " + noSuchPad);
		}
		
		catch (InvalidKeyException invalidKey)
		{
			System.out.println(" Invalid Key " + invalidKey);
		}
				
		catch (BadPaddingException badPadding)
		{
			System.out.println(" Bad Padding " + badPadding);
		}
				
		catch (IllegalBlockSizeException illegalBlockSize)
		{
			System.out.println(" Illegal Block Size " + illegalBlockSize);
		}
				
		catch (InvalidAlgorithmParameterException invalidParam)
		{
			System.out.println(" Invalid Parameter " + invalidParam);
		}
	}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 2 2010
Added on May 4 2010
7 comments
1,532 views