Search This Blog

Tuesday, October 26, 2021

Ubuntu Linux - Connect Postgresql from Psql and also PGadmin Remotely

 Step 1 :

> sudo su

> vi /etc/postgresql/10/main/postgresql.conf

#listen_addresses = 'localhost'
to 
listen_addresses = '*'

>vi /etc/postgresql/10/main/pg_hba.conf
# IPv4 local connections:
host all all 127.0.0.1/32 md5

to
host all all 0.0.0.0/0 md5


>sudo ufw allow 5432/tcp
>sudo systemctl restart postgresql


>psql -h localhost -d databasename -U userrolename
...enterpasswordfor-userrolename






Wednesday, October 13, 2021

Security : Java PHP encryption decryption for AES-128-CTR or AES/CTR/NoPadding

 Java

package com.security;

import java.util.Base64;


import javax.crypto.Cipher;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;


/**

 * @author drvijay

 * @date 13-0ct-2021

 */


public class EncryptDecrypt

{

private static String encrypt ( String data, String cipherType, String key, String iv )

{

String encrypted = "";


try

{

Cipher encryptionCipher = Cipher.getInstance ( cipherType );

IvParameterSpec ivv = new IvParameterSpec ( iv.getBytes ( "UTF-8" ), 0, encryptionCipher.getBlockSize () );

encryptionCipher.init ( Cipher.ENCRYPT_MODE, new SecretKeySpec ( key.getBytes (), "AES" ), ivv );

// encrypt

byte [] cipherText = encryptionCipher.doFinal ( data.getBytes () );


encrypted = new String ( Base64.getEncoder ().encode ( cipherText ) );

}

catch ( Exception e )

{

throw new IllegalStateException ( e );

}

return encrypted;

}


private static String decrypt ( String encryptData, String cipherType, String key, String iv )

{

String decrypted = "";

try

{


Cipher decryptionCipher = Cipher.getInstance ( cipherType );

IvParameterSpec ivv = new IvParameterSpec ( iv.getBytes ( "UTF-8" ), 0, decryptionCipher.getBlockSize () );


SecretKeySpec secretKeySpec = new SecretKeySpec ( key.getBytes (), "AES" );

decryptionCipher.init ( Cipher.DECRYPT_MODE, secretKeySpec, ivv );

// decrypt

byte [] finalCipherText = decryptionCipher.doFinal ( Base64.getDecoder ().decode ( encryptData ) );

// converting to string

String finalDecryptedValue = new String ( finalCipherText );

decrypted = finalDecryptedValue;

}

catch ( Exception e )

{

throw new IllegalStateException ( e );

}

return decrypted;

}


public static void main ( String [] args )

{

String cipherType = "AES/CTR/NoPadding";

String valueToEncrypt = "hello, vijay";

String key = "0123456789abcdef";

String iv = "2208250639374785";


String encrypted = encrypt ( valueToEncrypt, cipherType, key, iv );

String decrypted = decrypt ( encrypted, cipherType, key, iv );


//System.out.println(Base64.getDecoder ().decode ( "ycbnIE8vn2lTUi/9F/FEa+5v86qzOU09yjdxfGDc8wA=" ));

// END OF ENCODE CODE

System.out.println ( "encrypted and saved as Base64 : " + encrypted );


System.out.println ( "decrypted from Base64->aes128 : " + decrypted );

// END OF DECRYPT CODE


}

}




PHP code

<!DOCTYPE html>

<html>

<body>

<?php

$simple_string = "hello, vijay";

echo ($simple_string .'<br/>');

        $ciphering = 'AES-128-CTR';

        $iv_length = openssl_cipher_iv_length($ciphering);

        $options = 0;

        // Non-NULL Initialization Vector for encryption

        $iv = '2208250639374785';


       

        $key = '0123456789abcdef';


                $encryption = openssl_encrypt($simple_string, $ciphering,

                    $key, $options, $iv); 

                    echo($encryption .'<br/>');

                    

                  $decryption=openssl_decrypt ($encryption, $ciphering,

                $key, $options, $iv);

                echo($decryption .'<br/>');

                

?>


</body>

</html>



output

hello, vijay
0iQoUVC/QZG3cM20
hello, vijay

Monday, October 11, 2021

Java to Do ImageMagick Call

 package com.product.zilly;


import java.io.File;

import java.util.Map;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;


/**

 * @author drvijay

 * @description img class

 * @version 1.0

 */


@SuppressWarnings ("all")

public class TestModules implements Constants

{

private static final Logger logger = LoggerFactory.getLogger ( TestModules.class );


public static void main ( String args[] )

{

ImageHelper imageHelper = new ImageHelper ();

String renditionPath = "/home/drvijay/images"; 

String rendtionPathCategory = "/home/drvijay/images"; 

String startingMainPath = renditionPath + PATH_SEPARATOR + SkuRendition.main;

String startingCategoryMainPath = rendtionPathCategory + PATH_SEPARATOR + SkuRendition.main; // for category images main path

imageHelper.loadSkuImageProperties ();

boolean imageOverride = false; 

File rotated = null;

File file = new File ( startingMainPath + PATH_SEPARATOR + "sample.png" );

if ( file.isFile () && file.exists () )

{

Map <SkuRendition, Map <Resolution, DimInfo>> dimensions = imageHelper.getAllDimensions ();

logger.debug ( dimensions.toString () );

for ( SkuRendition skuRendition : dimensions.keySet () )

{

try

{

Map <Resolution, DimInfo> resolutions = dimensions.get ( skuRendition );

for ( Resolution resolution : resolutions.keySet () )

{

DimInfo dimInfo = resolutions.get ( resolution );


File isExist = new File ( renditionPath + PATH_SEPARATOR + skuRendition.name () + PATH_SEPARATOR + resolution.name () + UNDER_SCORE + file.getName () );

if ( ( imageOverride && isExist.isFile () && isExist.exists () ) || ( !isExist.isFile () && !isExist.exists () ) )

{

rotated = imageHelper.processImage ( file, imageHelper.getOutputDir ( renditionPath, skuRendition.name () ), dimInfo, resolution.name () );

logger.debug ( "Generated : " + rotated.getAbsolutePath () );

}


}

}

catch ( Exception e )

{

logger.error ( "Error : " + e );

}

finally

{


/*

* if ( rotated != null )

* {

* rotated.delete ();

* }

*/


}

}

}

}

}


//Iamge Process

public File processImage ( File inFile, File outputDir, DimInfo dimInfo, String resolution ) throws Exception

{

String fileName = resolution + UNDER_SCORE + inFile.getName ();

// String fileNameWithOutExt = FilenameUtils.removeExtension(fileName);

// String fileNameExt = FilenameUtils.getExtension ( fileName );


// check for windows and set the image magick home manually to avoid error.

String OS = System.getProperty ( "os.name" ).toLowerCase ();

if ( ( OS.indexOf ( "win" ) >= 0 ) )

{

// linux - "/usr/lib/mime/packages/imagemagick-6.q16";

String myPath = "C:/Program Files/ImageMagick-6.9.2-Q16"; // globalMaster.getSystemProperties ().get ( KEY_PATH_HOME_IMAGE_MAGICK ).getValue ();

ProcessStarter.setGlobalSearchPath ( myPath );

}

ConvertCmd convert = new ConvertCmd ();

IMOperation operation = new IMOperation ();

String input = inFile.getAbsolutePath ();

int tmbWidth = dimInfo.getWidth ();

int tmbHeight = dimInfo.getHeight ();


operation.addImage ( input );

operation.gravity ( LocalProperties.getPropertyValue ( "render.image.gravity" ) );

operation.thumbnail ( tmbHeight, tmbWidth );

operation.units ( LocalProperties.getPropertyValue ( "render.image.units" ) );

operation.density ( Integer.parseInt ( LocalProperties.getPropertyValue ( "render.image.density" ) ) );

operation.strip ();

operation.interlace ( LocalProperties.getPropertyValue ( "render.image.interlace" ) );

operation.gaussianBlur ( Double.parseDouble ( LocalProperties.getPropertyValue ( "render.image.gaussian.blur" ) ) );

operation.quality ( Double.parseDouble ( LocalProperties.getPropertyValue ( "render.image.quality" ) ) );


File retVal = new File ( outputDir, fileName );

operation.addImage ( retVal.getAbsolutePath () );

convert.run ( operation );


logger.debug ( "Preview file: " + retVal.getAbsolutePath () );

return retVal;

}


Install Latest ImageMagick to support PNG, JPG convertions

 #These are the steps required in order to Install ImageMagick with JPG, PNG and TIFF delegates.

sudo apt-get update 

#Install Build-Essential in order to configure and make the final Install

sudo apt-get install build-essential 

#libjpg62-dev required in order to work with basic JPG files

sudo apt-get install -y libjpeg62-dev 

#libtiff-dev is required in order to work with TIFF file format

sudo apt-get install -y libtiff-dev 

#libpng-dev required in order to work with basic PNG files

sudo apt-get install -y libpng-dev

#Download ImageMagick

wget https://www.imagemagick.org/download/ImageMagick.tar.gz 

#Untar Imagemagick

tar xvzf ImageMagick.tar.gz 

#Access the working directory

cd ImageMagick-[version_number] 

#Configure and make sure to disable the "shared" option

./configure --disable-shared

#Make

sudo make

#Install

sudo make install

#Final Check

sudo make check

Hit Counter


View My Stats