Encrypt ini files

3

I need a script that compiles a .ini to avoid an easy reading. The file should be read with PHP. It seems that C # has a similar feature. The question is how do I do this and then I read .ini with PHP.

Example of file .ini :

[db_production]
host = db_production.fiber01.intraservers
type = mssql
user = db_user
pass = db_pass
namedb = db_name
    
asked by anonymous 10.12.2014 / 22:01

2 answers

0
  

If this answer is not good enough, please check my other answer to this question, rather than being negative for the answer that served the questioner and that may serve others in the future.

You can code your result at Base64 . It looks like this:

W2RiX3Byb2R1Y3Rpb25dDQpob3N0ID0gZGJfcHJvZHVjdGlvbi5maWJlcjAxLmludHJhc2VydmVycw0KdHlwZSA9IG1zc3FsDQp1c2VyID0gZGJfdXNlcg0KcGFzcyA9IGRiX3Bhc3MNCm5hbWVkYiA9IGRiX25hbWU=

Encoding Base64 in C #:

public static string Base64Encode(string plainText) {
    var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
    return System.Convert.ToBase64String(plainTextBytes);
}

Decoding in C #:

public static string Base64Decode(string base64EncodedData) {
    var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
    return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}

Encoding Base64 in PHP:

$codificada = base64_encode($string);

Decoding in PHP:

$original = base64_decode($codificada);
    
10.12.2014 / 22:07
0

Considering that my first response was much questioned, I am putting here another way to encrypt the contents of the .ini file used by the algorithm TripleDES .

The TripleDES method requires an encryption key (or a salt , common name) that should be saved somewhere. For examples, suppose a Helper named SettingsReader that reads this value from somewhere.

Note that TripleDES also uses Base64 in the algorithm.

Encoding TripleDES in C #:

public static string Encrypt(string toEncrypt, bool useHashing)
{
    byte[] keyArray;
    byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);

    System.Configuration.AppSettingsReader settingsReader = 
                                        new AppSettingsReader();

    // Obtendo a chave de segurança do arquivo de configuração.

    string key = (string)settingsReader.GetValue("SecurityKey", 
                                                     typeof(String));
    //System.Windows.Forms.MessageBox.Show(key);
    //If hashing use get hashcode regards to your key
    if (useHashing)
    {
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        hashmd5.Clear();
    }
    else
        keyArray = UTF8Encoding.UTF8.GetBytes(key);

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    tdes.Key = keyArray;

    // Há 5 modos de operação. O utilizado nessa resposta é o ECB (Electronic Code Book)
    tdes.Mode = CipherMode.ECB;
    // O padding é o método definido para trabalhar com bytes residuais.
    tdes.Padding = PaddingMode.PKCS7;

    ICryptoTransform cTransform = tdes.CreateEncryptor();
    byte[] resultArray = 
      cTransform.TransformFinalBlock(toEncryptArray, 0, 
      toEncryptArray.Length);
    tdes.Clear();

    return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}

Decoding TripleDES in C #:

public static string Decrypt(string cipherString, bool useHashing)
{
    byte[] keyArray;    
    byte[] toEncryptArray = Convert.FromBase64String(cipherString);

    System.Configuration.AppSettingsReader settingsReader = 
                                        new AppSettingsReader();
    string key = (string)settingsReader.GetValue("SecurityKey", 
                                                 typeof(String));

    if (useHashing)
    {
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
        keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
        hashmd5.Clear();
    }
    else
    {
        keyArray = UTF8Encoding.UTF8.GetBytes(key);
    }

    TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    tdes.Key = keyArray;

    tdes.Mode = CipherMode.ECB;
    tdes.Padding = PaddingMode.PKCS7;

    ICryptoTransform cTransform = tdes.CreateDecryptor();
    byte[] resultArray = cTransform.TransformFinalBlock(
                         toEncryptArray, 0, toEncryptArray.Length);           
    tdes.Clear();
    return UTF8Encoding.UTF8.GetString(resultArray);
}

Encoding TripleDES in PHP:

function encrypt($input,$ky)
{
   $key = $ky;
   $size = mcrypt_get_block_size(MCRYPT_TRIPLEDES, 'ecb');
   $input = pkcs5_pad($input, $size);
   $td = mcrypt_module_open(MCRYPT_TRIPLEDES, '', 'ecb', '');
   $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
   mcrypt_generic_init($td, $key, $iv);
   $data = mcrypt_generic($td, $input);
   mcrypt_generic_deinit($td);
   mcrypt_module_close($td);
   $data = base64_encode($data);
   $data = urlencode($data);
   return $data;
}

Decoding TripleDES in PHP:

function decrypt($crypt,$ky)
{

    $crypt = urldecode($crypt);
    $crypt = base64_decode($crypt);
    $key = $ky;
    $td = mcrypt_module_open (MCRYPT_TRIPLEDES, '', 'ecb', '');
    $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $decrypted_data = mdecrypt_generic ($td, $crypt);
    mcrypt_generic_deinit ($td);
    mcrypt_module_close ($td);
    $decrypted_data = pkcs5_unpad($decrypted_data);
    $decrypted_data = rtrim($decrypted_data);
    return $decrypted_data;
}

Additional functions to take care of padding :

function pkcs5_pad($text, $blocksize)
{
   $pad = $blocksize - (strlen($text) % $blocksize);
   return $text . str_repeat(chr($pad), $pad);
}

function pkcs5_unpad($text)
{
   $pad = ord($text{strlen($text)-1});
   if ($pad > strlen($text)) return false;
   return substr($text, 0, -1 * $pad);
}
    
11.12.2014 / 17:12