Ahoi,
folgendes Problem: Ich möchte sensible Daten wie Passwörter etc. in einer Datenbank ablegen, davor aber noch Verschlüsseln.
	
	
		
			
	
	
	
	
	
		
	
		
	
	
	
		
			
	
	
	
	
	
		
	
		
	
Kriege die IllegalBlockSizeException mit folgendem StackTrace
	
	
		
			
	
	
	
	
	
		
	
		
	
			
			folgendes Problem: Ich möchte sensible Daten wie Passwörter etc. in einer Datenbank ablegen, davor aber noch Verschlüsseln.
		Java:
	
	import java.security.SecureRandom;
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class CSecurity
{
	static int keyLength = 16;
	static String unicode_format = "UTF-8";
	static String usedAlgorithm = "AES";
	public static String decode(String encryptedText) throws Exception
	{
		String key = generateRandomKey();
		SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(unicode_format), usedAlgorithm);
		// Instantiate the cipher
		Cipher cipher = Cipher.getInstance(usedAlgorithm);
		cipher.init(Cipher.DECRYPT_MODE, keySpec);
		byte[] encryptedTextBytes = Base64.decodeBase64(encryptedText);
		byte[] decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
		return new String(decryptedTextBytes);
	}
	public static String encode(String plainText) throws Exception
	{
		String key = generateRandomKey();
		SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(unicode_format), usedAlgorithm);
		Cipher cipher = Cipher.getInstance(usedAlgorithm);
		cipher.init(Cipher.ENCRYPT_MODE, keySpec);
		byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes(unicode_format));
		return new Base64().encodeAsString(encryptedTextBytes);
	}
	private static String generateRandomKey()
	{
		Random rnd = new SecureRandom();
		char[] characterSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789".toCharArray();
		char[] result = new char[keyLength];
		for (int i = 0; i < result.length; i++)
		{
			int randomCharIndex = rnd.nextInt(characterSet.length);
			result[i] = characterSet[randomCharIndex];
		}
		return new String(result);
	}
}
		Java:
	
						String encode = "testpasswort";
					System.out.println(CSecurity.encode(encode));
					
					String decode = encode;
					System.out.println(CSecurity.decode(decode));Kriege die IllegalBlockSizeException mit folgendem StackTrace
		Java:
	
	javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
	at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750)
	at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
	at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
	at javax.crypto.Cipher.doFinal(Cipher.java:2087)
	at CSecurity.decode(CSecurity.java:25)
	at CMain$1.run(CMain.java:18)
	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$200(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source) 
				 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		 
 
		