title: How to Encrypt and Decrypt a Android Studio and php same String?
body: <p>How to encrypt and decrypt string in the same method use Android Studio in Java and PHP. I am using PHP this code.</p>
  <p>PHP script code</p>
  <pre><code>&lt;?
  $plaintext = 'abcdefghijklmnopqrstuvwxyz';
  $password = '112233';
  
  $method = 'aes-256-cbc';
  
  $key = substr(hash('SHA256', $password, true), 0, 32);
  
  $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
  
  $encrypted = base64_encode(openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA,$iv));
  
  $decrypted = openssl_decrypt(base64_decode($encrypted), $method, $key, OPENSSL_RAW_DATA,$iv);
  
   'plaintext=' . $plaintext . &quot;\n&quot;;
   'cipher=' . $method . &quot;\n&quot;;
  echo 'encrypted to: ' . $encrypted . &quot;\n&quot;;
  echo 'decrypted to: ' . $decrypted . &quot;\n\n&quot;;
  ?&gt;
  </code></pre>
  <p>And Android Studio in java using this code.</p>
  <p>Java code</p>
  <pre><code>import androidx.appcompat.app.AppCompatActivity;
  
  import android.os.Bundle;
  import android.util.Base64;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  
  import java.security.MessageDigest;
  
  import javax.crypto.Cipher;
  import javax.crypto.SecretKey;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;
  
  public class encryptanddecrypt extends AppCompatActivity {
  
      EditText nameet,nameet2,etresult;
      Button encryptbt,decryptbt;
      String outputString;
      String AES = &quot;AES&quot;,IV=&quot;112233&quot;;
      String secretKey;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_encryptanddecrypt);
          nameet=findViewById(R.id.editTextTextPersonName);
          nameet2=findViewById(R.id.editTextTextPersonName2);
          etresult=findViewById(R.id.editTextTextPersonName3);
          encryptbt=findViewById(R.id.button6);
          decryptbt=findViewById(R.id.button7);
  
          encryptbt.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  try {
  
  outputString = encrypt(nameet.getText().toString(),nameet2.getText().toString());
  etresult.setText(outputString);
  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          });
  
          decryptbt.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  try {
                     String outputStrings=decrypt(nameet.getText().toString(),nameet2.getText().toString());
                      etresult.setText(outputStrings);
                      Log.d(&quot;outputString&quot;,outputStrings);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
  
              }
          });
      }
  
      private String decrypt(String outputString, String Password) throws Exception{
          SecretKeySpec key=generateKey(Password);
          Cipher c =Cipher.getInstance(AES);
          c.init(Cipher.DECRYPT_MODE,key);
          byte[] decodeValue = Base64.decode(outputString,Base64.DEFAULT);
          byte[] decValue = c.doFinal(decodeValue);
          String decryptedValue = new String(decValue);
          return decryptedValue;
      }
      private SecretKeySpec generateKey(String password) throws Exception{
          final MessageDigest digest = MessageDigest.getInstance(&quot;SHA256&quot;);
          byte[] bytes = password.getBytes(&quot;UTF-8&quot;);
          digest.update(bytes, 0,bytes.length);
          byte[] key = digest.digest();
          SecretKeySpec secretKeySpec = new SecretKeySpec(key,&quot;AES&quot;);
          return secretKeySpec;
      }
  private String encrypt(String Data, String Password) throws Exception{
          SecretKeySpec key=generateKey(Password);
          Log.d(&quot;checkkey&quot;, String.valueOf(key));
          Cipher c =Cipher.getInstance(AES);
          c.init(Cipher.ENCRYPT_MODE,key);
          byte[] encVal = c.doFinal(Data.getBytes());
          String encryptedValue= Base64.encodeToString(encVal,Base64.DEFAULT);
          Log.d(&quot;encryptedValue&quot;, encryptedValue);
          return encryptedValue;
      }
  }
  </code></pre>
  <p>but some character is match and lasts some character is not matched please help please provide me php and android studio java code</p>