title: Encrypt data in windows phone 8 using AES if I already have the key (as a string)
body: <p>I am working on a wp8 app that requires encryption and decryption. Actually I need a way to encrypt some data using AES. I already have the key(as a string). I need a c# equivalent for the below java code can somebody help?</p>
  
  <pre><code>public static String encryptWithAES(String payload, String aesKey) {
  
      byte[] raw = aesKey.getBytes();
      SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES/ECB/PKCS5Padding");
      try {
          Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
          cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  
          byte[] encrypted;
          encrypted = cipher.doFinal(payload.getBytes());
          cipher = null;
          return Base64.encodeToString(encrypted, Base64.DEFAULT);
      } catch (Exception e) {
          System.out.println("Error in encryptWithAES!!!");
          e.printStackTrace();
      }
      return null;
  }
  </code></pre>
  
  <p>Here is what i did :</p>
  
  <pre><code>public static byte[] EncryptWithAES(string dataToEncrypt, String Key)
      {
  
          byte[] encryptedData;
  
          byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(Key);
  
  
          using (AesManaged aesEnc = new AesManaged())
          {
              aesEnc.Key = keyBytes;
              aesEnc.IV = new byte[16];
  
              //Create encryptor for converting
              ICryptoTransform encryptor = aesEnc.CreateEncryptor(aesEnc.Key, aesEnc.IV);
  
  
              using (MemoryStream memStream = new MemoryStream())
              {
                  using (CryptoStream crypStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
                  {
                      using (StreamWriter srmWriter= new StreamWriter(crypStream))
                      {
  
                          srmWriter.Write(dataToEncrypt);
                      }
                      encryptedData = memStream.ToArray();
                  }
              }
          }
          return encryptedData;
      }
  </code></pre>
  
  <p>The error I am getting is where i set the key: <code>ie aesEnc.Key= keyBytes</code>;</p>