title: IVParameter Spec Problem in Java
body: <p>I am currently devloping a Windows application using C# and looking at developing a mobile app using Java. </p>
  
  <p>The windows software and the mobile app will work together, the windows app will store information and encrypt certain information before storing it in an online database. </p>
  
  <p>The mobile app will pull the information from the online database and will need to decrypt the encrypted string that is retrieved from the datbase.</p>
  
  <p>The encryption method I am using in C# is below</p>
  
  <pre><code>        byte[] clearTextBytes = Encoding.UTF8.GetBytes(encryptionString);
  
          SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
  
          MemoryStream ms = new MemoryStream();
          byte[] rgbIV = Encoding.ASCII.GetBytes("ryojvlzmdalyglrj");
  
          byte[] key = Encoding.ASCII.GetBytes("hcxilkqbbhczfeultgbskdmaunivmfuo");
          CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(key, rgbIV), CryptoStreamMode.Write);
  
          cs.Write(clearTextBytes, 0, clearTextBytes.Length);
  
          cs.Close();
  
          return Convert.ToBase64String(ms.ToArray());
  </code></pre>
  
  <p>The Windows method works fine.</p>
  
  <p>The code I am using in Java is as follows:</p>
  
  <pre><code>KeySpec ks = new DESKeySpec("hcxilkqbbhczfeultgbskdmaunivmfuo".getBytes("UTF-8"));
              SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(ks);
  
          String ivString = "ryojvlzmdalyglrj";
          byte[] ivByte = ivString.getBytes("UTF-8");
          IvParameterSpec iv = new IvParameterSpec(ivByte);
          //RC5ParameterSpec iv = new RC5ParameterSpec(ivByte);
  
          Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
  
          cipher.init(Cipher.ENCRYPT_MODE, key, iv);
  
          byte[] encoded = cipher.doFinal(Base64.decodeBase64("iNtaFme3B/e6DppNSp9QLg=="));
  
          Log.d("Decoded Password", encoded.toString());
  </code></pre>
  
  <p>As both methods need to encrypt and decrypt the same string together it has to use the same key and IV. The only problem that I am having is in the java method the IVParameterSpec is going into the catch with an error that says IV must be 8 bytes long. </p>
  
  <p>How can I resolve this to ensure that I have the same IV as C#. </p>
  
  <p>Thanks for the help</p>