Search This Blog

Thursday, April 26, 2018

Nodejs - Decrypt for AES-256-CBC with no padding.

var crypto = require ( 'crypto' );
algorithm  = 'aes-256-cbc';

function decrypt ( key, data )
{
    var sha256 = crypto.createHash ( 'sha256' );
    sha256.update ( key );

    var input      = new Buffer ( data, 'base64' ),
        iv         = Buffer.alloc(16);
        ciphertext = input,
        decipher   = crypto.createDecipheriv( algorithm, sha256.digest (), iv ),
        plaintext  = decipher.update ( ciphertext );
    plaintext += decipher.final ();

    return plaintext
};

var deCodeValue = new Buffer ( "encrypted-content-place-here", 'base64' ).toString ( 'utf-8' );
console.log(deCodeValue);
var p           = decrypt ( 'password', deCodeValue );
console.log ( p );

PHP curl to upload a FILE with POST data



$ch = curl_init();

$fname = 'd:/temp/xxx.jpg';   
//windows
$cfile = new CURLFile(realpath($fname));
//linux
//$cfile = new CURLFile(realpath($fname), 'application/octet-stream', 'filename.pdf');

$data = array('param' => 'value', 
'param2' => 'value2',
'param3' => '{"jsonkey1":"jsonvalue1","jsonkey2":"jsonvalue2"}',
'file' => $cfile
);

curl_setopt($ch, CURLOPT_URL, 'https://api-url');
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('cache-control: no-cache','Content-Type: multipart/form-data'));
//curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$response  = curl_exec($ch);

$err = curl_error($ch);

//echo "INfo : ".implode(',',curl_getinfo($ch));

curl_close($ch);

if ($err) {
  echo "cURL Error #: " . $err;
} else {
  echo "Success : ".$response;
}

?>

Thursday, March 15, 2018

Ruby Rails Rest-Client with POST Parameters, Authorization, Headers

response = RestClient::Request.new({
      method: :post,
      url: 'https://infovijay.com,
      user: 'vijay',
      password: 'dr12345',
      payload: { key1: 'value1', key2: 'value2' },
      headers: { :accept => :json, content_type: :json }
    }).execute do |response, request, result|
      case response.code
      when 400
        [ :error, parse_json(response.to_str) ]
      when 200
        [ :success, parse_json(response.to_str) ]
      else
        fail "#{response.to_str}."
      end
    end

Thursday, March 8, 2018

How to take from Custom/Local Lib - Maven

1. create a folder
2. in maven tag specify the local path
3.  what ever the jar you kept inside the folder. just add those jar in like how u r specifying normally.
4. Maven will pick in local repo, while compile or build [ whenever it perform ]





Wednesday, March 7, 2018

PHP - Aes-256-cbc Encrypt Decrypt with IV vector


function encrypt_decrypt($action, $string) {
    $output = false;
    $encrypt_method = "aes-256-cbc";
    $secret_key = 'abcdefghi1234567890';
    $secret_iv = '';
 
// hash
    $key = hash('sha256', $secret_key);
    echo "

".$key;

    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hash('sha256', $secret_iv), 0, 16);
    if ( $action == 'encrypt' ) {
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
    } else if( $action == 'decrypt' ) {
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }
    return $output;
}
$plain_txt = "I am vijay";
echo "

Plain Text =" .$plain_txt. "\n";
$encrypted_txt = encrypt_decrypt('encrypt', $plain_txt);
echo "

Encrypted Text = " .$encrypted_txt. "\n";

$decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt);
echo "

Decrypted Text =" .$decrypted_txt. "\n";

if ( $plain_txt === $decrypted_txt ) echo "

SUCCESS";
else echo "

FAILED";
echo "\n";

?>



Output

Plain Text =I am vijay

681448fb66d8704c7d52f6770f6087882db21c899fe48d21012a587bbe70cdd3

Encrypted Text = THRtUkJYWFUzb1NzUFRoOUw5T3Q2UT09

681448fb66d8704c7d52f6770f6087882db21c899fe48d21012a587bbe70cdd3

Decrypted Text =I am vijay

SUCCESS

PHP - Encrypt / Decrypt with out IV Vector with AES-256-CBC



const PASSWORD = 'abcdefghi1234567890';
const CIPHER_METHOD = 'AES-256-CBC';

function encrypt($str) {
    $val = openssl_encrypt($str, CIPHER_METHOD, hash('sha256', PASSWORD, true));
    return $val;    
}

function decrypt($str) {
   return openssl_decrypt($str, CIPHER_METHOD, hash('sha256', PASSWORD, true));
}

$plain_data = 'I am vijay';

$encrypted = encrypt($plain_data);
echo "Encrypted : ".$encrypted."

";


$decrypted = decrypt($encrypted);
echo "Decrypted : ".$decrypted."

";


?>


Output

: openssl_encrypt(): Using an empty Initialization Vector (iv) is potentially insecure and not recommended in D:\php\test.php >

Encrypted :
zmuNrqZ+GwtQC5B0riJoMw==

Decrypted : I am vijay


Hit Counter


View My Stats