0 Replies - 2800 Views - Last Post: 11 October 2012 - 03:26 PM

#1 liftthis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 08-May 05

Android crypto generating different key from same password problem

Posted 11 October 2012 - 03:26 PM

every time I run the setup method using the same password, I get a different key result. I ran the following code in java and I have no problems but in Android its an issue. Can someone show me what is the problem.


private static byte[] bytes;
	  Cipher ecipher;
	  Cipher dcipher;

	  // 8-byte Salt
	  byte[] salt = {
	      (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
	      (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
	  };

	  // Iteration count
	  int iterationCount = 19;
	  
	  public String setup(String passPhrase) 
	  {
	      String output = null;
	      try {
	          // Create the key
	          KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
	          SecretKey key = SecretKeyFactory.getInstance(
	              "PBEWithMD5AndDES").generateSecret(keySpec);
	          
	          ecipher = Cipher.getInstance(key.getAlgorithm());
	          dcipher = Cipher.getInstance(key.getAlgorithm());

	          // Prepare the parameter to the ciphers
	          AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

	          // Create the ciphers
	          ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
	          dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
	          
	          // print key
		      System.out.println("key  = " + key);
		      System.out.println("paramSpec  = " + paramSpec);
		      
		     
		      output = key.toString();
		  //    showToast("setting up key " + output);
		  //    showToast("key size " + output.length());
		      System.out.println("key Size " + output.length());
 
	      } catch (java.security.InvalidAlgorithmParameterException e) {
	      } catch (java.security.spec.InvalidKeySpecException e) {
	      } catch (javax.crypto.NoSuchPaddingException e) {
	      } catch (java.security.NoSuchAlgorithmException e) {
	      } catch (java.security.InvalidKeyException e) {
	      }
		
	      return output;
	  }






Is This A Good Question/Topic? 0
  • +

Page 1 of 1