Sunteți pe pagina 1din 6

Password Based Encryption in J2ME

May 1, 2006

Technical Article

Password Based Encryption in J2ME


By Motocoder Staff assword Based Encryption, also called PBE, is widely used for storing security information. Even if the handset is stolen and all the files are read out, the information protected by PBE like bank account and transaction records are still safe without the password. PBE is also used to store the private key in the handset.

Basic Mechanism In PBE, the key is stored in the human brain rather than storage media. First, the password is processed by some message-digest method like Secure Hash Algorithm (SHA-1), generating the hash value as the symmetric key. Next, the key and plain text are input to the symmetric encryption algorithm like Blowfish generating a cipher. There are many kinds of PBE methods, depending on the message digest algorithm and the symmetric encrypt algorithm. Message digest algorithm

Password

Hash value as symmetric key

Plain Text

Symmetric encrypt algorithm

Cipher

Figure 1: PBE encryption flowchart For the PBE, the original password is carefully protected. In fact, only the hash value of the password is really stored and checked.

Salt The major defect of PBE is the password length, which is usually no more than eight characters. Eight characters is simply not long enough to generate a good symmetric key. However, longer passwords become harder to remember, so forcing the user to remember longer passwords will decrease the system usability. Using a salt gives you a solution to this bind, allowing you to programmatically append a more complex set of data to the password. This also allows shorter, easier to remember passwords, and harder to break encryptions. Salt data makes dictionary attacks much less effective in breaking password security. Message digest algorithm

Salt

Password

Hash value as symmetric key

Iteration for many times Plain Text Symmetric encrypt algorithm

Salt

Cipher

Figure 2: PBE encryption flowchart with salt

Password

Salt

Password

Message digest algorithm

Hash value as symmetric key

Iteration for many times Salt Cipher Cipher Symmetric decrypt algorithm

Plain Text

Figure 3: PBE decryption flowchart with salt

Dictionary attack is a way of trying "every word in the dictionary" as a possible password for encrypted text. A dictionary attack is generally more efficient, because users typically generate all the possible hash values for the eight-character password before trying to break the cipher. Salt is a random byte array used to increase the symmetric key complexity so as to protect dictionary attack. The random byte array is combined with the password before the message is digested and written at the top of the cipher after encryption. This random byte array is called salt because theres one for each file or data block, which changes the flavor of the hash code pattern and makes it hard to be identified. With an eight-byte salt, the amount of possible symmetric keys increases 2^32 times. The difficulty of exhaustive search on any particular users password is unchanged by salting, since the salt is given in plain text in the head of cipher. So the PKCS5 standard recommends at least 1000 iteration times for the hash value calculation, thus any exhaustive search attack will have to complete the same 1000 iterations to be successful. Sample Code Below are some sample code based on the bouncycastle library: //Salt generation public PBEEngine(){

for(int j=0;j<=1;j++){ Random rd = new Random(); int i = rd.nextInt(); salt[0+j*4] = (byte)(i>>24); salt[1+j*4] = (byte)(i>>16); salt[2+j*4] = (byte)(i>>8); salt[3+j*4] = (byte)i; } } //Get symmetric key public CipherParameters getKey(String password) throws Exception{ char[] pass = password.toCharArray(); PBEParametersGenerator generator = new PKCS5S2ParametersGenerator(); generator.init(PBEParametersGenerator.PKCS5PasswordToBytes(pass), salt, mixingCount); CipherParameters key = generator.generateDerivedParameters(128); return key; } //AES encryption public String encrypt(String text, String password)throws Exception { key = getKey(password); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESLightEngine())); cipher.init(true,key); byte[] textByteArray = text.getBytes(); byte[] cipherByteArray = new byte[cipher.getOutputSize(textByteArray.length)]; int length = cipher.processBytes(textByteArray,0,textByteArray.length,cipherByteArray,0); cipher.doFinal(cipherByteArray,length); return new String(Base64.encode(cipherByteArray)); } //AES decryption public String decrypt(String cipherBase64String, String password)throws Exception { key = getKey(password); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESLightEngine())); cipher.init(false,key); byte[] cipherByteArray = Base64.decode(cipherBase64String); byte[] textByteArray = new byte[cipher.getOutputSize(cipherByteArray.length)]; int length = cipher.processBytes(cipherByteArray,0,cipherByteArray.length,textByteArray,0); cipher.doFinal(textByteArray,length); return new String(textByteArray); } Conclusion The memory and computation resources for mobile handsets are limited, so it is important to balance security and performance. For example, using lower iteration times for salting could save resource

consumption. In such case, experiment is necessary to determine whether reasonable parameters are attained.

References http://www.bouncycastle.org Professional Java Security, Jess Garms & Deniel Somerfield
HTU UTH

S-ar putea să vă placă și