The #encrypt Form API property allows developers to encrypt and decrypt form elements easily. You may use the #encrypt property on textfields, textareas, select fields, checkboxes, radios, and password fields. Here's an example of how to implement it:

    function my_form($form, $form_state) {
      $form = array();

      $form['textfield'] = array(
        '#type' => 'textfield',
        '#title' => t('A Sample Textfield'),
        '#default_value' => variable_get('a_text_field', ''),
        '#encrypt' => TRUE,
      );

      return $form;
    }

This will encrypt the value of that field using the site's default encryption method and key provider. Encryption happens at the beginning for the form's submit process, so you can still validate the original, encrypted value in the form's validation stage. Decryption should be handled for you automatically. You may also specify the encryption method to use when encrypting a field. It is the same as above, except that #encrypt is an array:

    #encrypt => array(
      'method' => 'mcrypt_rij_256',
    ),