Sunday, January 28, 2018

MD5 Hash Generator using Visual Studio

Every developers build series of application system but they forget the concept of cryptology.


Cryptology the study of codes, or the art of writing and solving them. it is a mathematics such as number theory, application of algorithms that building cryptography and cryptanalysis.

Cryptanalysis is the study of analyzing information in order to study the hidden aspects of the system, it is used to breach security system and access to the contents, even if the cryptographic key is undefined. The concepts are complex and high specialized.


Syntax


Imports System
Imports System.Security.Cryptography
Imports System.Text

Public Class Form1

    Function GetMd5Hash(ByVal md5Hash As MD5, ByVal input As String) As String
        Dim data As Byte() = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input))
        Dim sBuilder As New StringBuilder()
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i
        Return sBuilder.ToString()

    End Function

    Function VerifyMd5Hash(ByVal md5Hash As MD5, ByVal input As String, ByVal hash As String) As Boolean
        Dim hashOfInput As String = GetMd5Hash(md5Hash, input)
        Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
        If 0 = comparer.Compare(hashOfInput, hash) Then
            Return True
        Else
            Return False
        End If

    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim [source] As String = TextBox1.Text
        Using md5Hash As MD5 = MD5.Create()

            Dim hash As String = GetMd5Hash(md5Hash, source)

            Label2.Text = hash

            If VerifyMd5Hash(md5Hash, [source], hash) Then
                MsgBox("The result are same.", MsgBoxStyle.Information, "MD5 Hash Generator")
            Else
                MsgBox("The result are not same.", MsgBoxStyle.Critical, "MD5 Hash Generator")
            End If
        End Using

    End Sub
End Class


Result/Image




Saturday, January 27, 2018

Simple Encrypt and Decrypt Function using Visual Studio

Let's create our own encryption code without using the existing encryption method like md5 or hash.

The Code below shows a simple trick on how to handle the Encryption using the loop, chr, asc, and mid method.


ChrW method returns a String containing the Unicode character except on platforms where Unicode is not supported, in which case, the behavior is identical to the Chr function. Chr() method that works in Visual Basic but I don't know in C# code.

AscW function returns the Unicode character code except on platforms where Unicode is not supported, in which case, the behavior is identical to the Asc function.

Mid function to return a specified number of characters from a string.


Encrypt
Function Encrypt(ByVal Inputtxt As String)
        Encrypt = ""
        Dim CharCount As Integer
        For CharCount = 1 To Len(Inputtxt)
            Encrypt = Encrypt + Chr(Asc(Mid(Inputtxt, CharCount, 1)) + 15)
        Next CharCount
    End Function

Decrypt
Function Decrypt(ByVal Inputtxt As String)
        Decrypt = ""
        Dim CharCount As Integer
        For CharCount = 1 To Len(Inputtxt)
            Decrypt = Decrypt + Chr(Asc(Mid(Inputtxt, CharCount, 1)) - 15)
        Next CharCount
    End Function




Image below show's how to use this code





Syntax




Result






Sunday, November 20, 2016

Image Upload with preview

HTML SCRIPT

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Attached Prescription</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="../bootstrap.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#images').on('change',function(){
$('#multiple_upload_form').ajaxForm({
target:'#images_preview',
beforeSubmit:function(e){
$('.uploading').show();
},
success:function(e){
$('.uploading').hide();
},
error:function(e){
}
}).submit();
});
});
</script>
</head>
<body style="">
<div style="margin-left:80px;">
<div class="upload_div">
    <form method="post" name="multiple_upload_form" id="multiple_upload_form" enctype="multipart/form-data" action="upload.php">
    <input type="hidden" name="image_form_submit" value="1" />
<div style="padding:20px;"></div>
        <input type="file" name="images[]" id="images" multiple class="btn btn-success "  style="margin-top:30">
        <div class="uploading none">
            <label>&nbsp;</label>
            <img src="uploading.gif"/>
        </div>
    </form>
<div class="gallery" id="images_preview" ></div>
<input type="button" onclick="SetAttachement()" value="Attached" class="btn btn-success " style="margin-top:20px">
    </div>
</div>
<script type="text/javascript">
function SetAttachement() {
if (window.opener != null && !window.opener.closed) {
var attachement = window.opener.document.getElementById("imgurl");
var attachement1 = window.opener.document.getElementById("pimgurl");
attachement.value = document.getElementById("cimgurl").value;
attachement1.value = document.getElementById("cimgurl").value;
}
window.close();
alert("Successfully attached.");
}
</script>
</body>
</html>



PHP SCRIPT


<?php
if($_POST['image_form_submit'] == 1)
{
$site_add ="https://store.nostlgc.com/catalog/view/theme/journal2/template/product/Prescription/UploadImage/";
$images_arr = array();
foreach($_FILES['images']['name'] as $key=>$val){
$image_name = $_FILES['images']['name'][$key];
$tmp_name = $_FILES['images']['tmp_name'][$key];
$size = $_FILES['images']['size'][$key];
$type = $_FILES['images']['type'][$key];
$error = $_FILES['images']['error'][$key];
$target_dir = "uploads/"; //Image location that are uploaded
$target_file = $target_dir.$_FILES['images']['name'][$key];
if(move_uploaded_file($_FILES['images']['tmp_name'][$key],$target_file)){
$images_arr[] = $target_file;
}
}
//Generate images view
if(!empty($images_arr)){ $count=0;
foreach($images_arr as $image_src){ $count++?>
<ul class="reorder_ul reorder-photos-list">
<input type="hidden" id="cimgurl" name="cimgurl" value="<?php echo $site_add . $image_src; ?>" >
            <li id="image_li_<?php echo $count; ?>" class="ui-sortable-handle">
                <a href="javascript:void(0);" style="float:none;" class="image_link"><img src="<?php echo $image_src; ?>" alt=""></a>
                </li>
          </ul>

<?php }
}
}
?>

Monday, October 24, 2016

Form Data Submission using JQUERY and AJAX

<script>
$(function(){
    $('#submitinfo').on('submit', function(e){
        e.preventDefault();
        $.ajax({
            url: "https://localhost/submit.php",
            type: 'POST', //or POST
            data: $('#submitinfo').serialize(),
            success: function(data){
                 alert('successfully submitted');
$('#submitinfo').hide();
            }
        });
    });
});
</script>

<form id="submitinfo">
<table>
<thead>
<h4 id="review-title">Customer information</h4>
</thead>
<tr>
<label class="control-label" for="input-name">Complete Name</label>
<input type="text" name="name" value="" id="input-name" class="form-control" required>
</tr>
<tr>
<label class="control-label" for="input-name">Phone Number</label>
<input type="text" name="guest_
</tr>
<tr>
<label class="control-label" for="input-name">Email Address</label>
<input type="text" name="email" value="" id="input-name" class="form-control" required>
</tr>
</table>
   <div class="modal-footer" STYLE="margin-top:30%;" >
       <button name="btn_submit" id="btn_submit" type="submit" class="btn btn-success">Submit</button>
   </div>
</form>

Sunday, May 29, 2016

Exporting & Importing App Pools and Websites configuration between multiple IIS instances

Using IIS server in a Cluster, Server Farm or other Load Balanced Environment will take a lot of work to setup, transferring a site from old IIS server to a new IIS server can encounter different issue in the .net framework and etc. When upgrading IIS7 (Windows Server 2008) TO IIS 8 (Windows Server 2012 R2).

appcmd is a commandline utility who can export the entire IIS Website and App. pools configuration in XML format and also import the xml into another IIS instance.

Export the Application Pools Using appcmd Command line

%windir%\system32\inetsrv\appcmd list apppool /config /xml > c:\apppools.xml

This command line will export all your application pools including the default in your webserver. Therefore you need to edit your appools.xml and remove the following contents that you do'nt need to import.
· DefaultAppPool
· Classic .NET AppPool
· SecurityTokenServiceApplicationPool
· .NET v2.0
· .NET v2.0 Classic
· .NET v4.5
· .NET v4.5 Classic
Import the Application Pools Using appcmd Command line
Copy the appools.xml file to your webserver and run this following command line. after that All the AppPools in the xml will be created on your second IIS server.

%windir%\system32\inetsrv\appcmd list apppool /config /xml > c:\apppools.xml

Export All Website Using appcmd Command line

%windir%\system32\inetsrv\appcmd list site /config /xml > c:\websites.xml

This command line will export all website on you IIS Server, after exporting you need to edit the websites.xml and remove the Default Website as well as any other website you don't want to export to you new IIS Server or already exist on the target IIS instance, otherwise the import command line won't work

Import All Website Using appcmd Command line
Just like you did with the App Pools file, copy the websites.xml file to your webserver and run this following command line. after that All the website in the xml file will be created on your target IIS server.

%windir%\system32\inetsrv\appcmd add site /in < c:\websites.xml



Monday, May 23, 2016

Zip File Upploader and Extracting using PHP Script

<?php

if($_FILES["fileToUpload"]["name"]) {
    $file = $_FILES["fileToUpload"];
    $filename = $file["name"];
    $tmp_name = $file["tmp_name"];
    $type = $file["type"];
   
    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');

    if(in_array($type,$accepted_types)) {
        $okay = true;
    }
   
    $continue = strtolower($name[1]) == 'zip' ? true : false;

    if(!$continue) {
        $message = "The file is not .zip file. Please try again.";
    }


        $ran = $name[0]."-".time()."-".rand(1,time());
        $targetdir = "files/".$ran;
        $targetzip = "files/".$ran.".zip";

    if(move_uploaded_file($tmp_name, $targetzip)) {
     
        $zip = new ZipArchive();
        $x = $zip->open($targetzip);
        if ($x === true) {
            $zip->extractTo($targetdir);
            $zip->close();
   
            unlink($targetzip);
        }
        $message = " <strong>{$ran}.zip</strong> file was uploaded and extracted.";

    } else {  
        $message = "Error!Uploaded file. Please try again.";
    }
}
echo $message;

?>