.

Thursday, 28 August 2014

ENCRYPTION AND DECRYPTION ALGORITHM


ENCRYPTION AND DECRYPTION ALGORITHM

Encryption  is a  process of coding information which could either be a file or  mail message  in into cipher text  a form unreadable without a decoding key in order to prevent anyone except the intended recipient from reading that data. Decryption is the reverse process of converting encoded data to its original un-encoded form, plaintext.
A key  in cryptography is a long sequence of bits used by encryption / decryption algorithms. For example, the following represents a hypothetical 40-bit key:

00001010 01101001 10011110 00011100 01010101
A given encryption algorithm takes the original message, and a key, and alters the original message mathematically based on the key's bits to create a new encrypted message. Likewise, a decryption algorithm takes an encrypted message and restores it to its original form using one or more keys. An Article by your Guide Bradley Mitchell
 When a user encodes a file, another user cannot decode and read the file without the decryption key. Adding a digital signature, a form of personal authentication, ensures the integrity of the original message
“To encode plaintext, an encryption key is used to impose an encryption algorithm onto the data. To decode cipher, a user must possess the appropriate decryption key. A decryption key consists of a random string of numbers, from 40 through 2,000 bits in length. The key imposes a decryption algorithm onto the data. This decryption algorithm reverses the encryption algorithm, returning the data to plaintext. The longer the encryption key is, the more difficult it is to decode. For a 40-bit encryption key, over one trillion possible decryption keys exist.
There are two primary approaches to encryption: symmetric and public-key. Symmetric encryption is the most common type of encryption and uses the same key for encoding and decoding data. This key is known as a session key. Public-key encryption uses two different keys, a public key and a private key. One key encodes the message and the other decodes it. The public key is widely distributed while the private key is secret.
Aside from key length and encryption approach, other factors and variables impact the success of a cryptographic system. For example, different cipher modes, in coordination with initialization vectors and salt values, can be used to modify the encryption method. Cipher modes define the method in which data is encrypted. The stream cipher mode encodes data one bit at a time. The block cipher mode encodes data one block at a time. Although block cipher tends to execute more slowly than stream cipher, block”
Platform Builder for Microsoft Windows CE 5.0

BACKGROUND OF ENCRYPTION AND DECRYPTION ALGORITHM  
 
CRYPTOGRAPHY is an algorithmic process of converting a plain text or clear text message to a cipher text or cipher message based on an algorithm that both the sender and receiver know, so that the cipher text message can be returned to its original, plain text form. In its cipher form, a message cannot be read by anyone but the intended receiver. The act of converting a plain text message to its cipher text form is called enciphering. Reversing that act (i.e., cipher text form to plain text message) is deciphering. Enciphering and deciphering are more commonly referred to  as encryption and decryption, respectively.

There are a number of algorithms for performing encryption and decryption, but comparatively few such algorithms have stood the test of time. The most successful algorithms use a key. A key is simply a parameter to the algorithm that allows the encryption and decryption process to occur. There are many modern key-based cryptographic techniques . These are divided into two classes: symmetric and asymmetric (also called public/private) key cryptography. In symmetric key cryptography, the same key is used for both encryption and decryption. In asymmetric key cryptography, one key is used for encryption and another, mathematically related key, is used for decryption.

TYPES OF CRYPTOGRAPHIC ALGORITHMS

There are several ways of classifying cryptographic algorithms. For purposes of this report  they will be categorized based on the number of keys that are employed for encryption and decryption, and further defined by their application and use. The following are the three types of Algorithm that are disscused
·         Secret Key Cryptography (SKC): Uses a single key for both encryption and decryption
·         Public Key Cryptography (PKC): Uses one key for encryption and another for decryption
·         Hash Functions: Uses a mathematical transformation to irreversibly "encrypt" information

            Gary c. Kessler May 1998
            (26 September 2005)


Symmetric Key Cryptography

The most widely used symmetric key cryptographic method is the Data Encryption Standard (DES) , published in 1977 by the National Bureau of Standards.  DES It is still the most widely used symmetric-key approach. It  uses a fixed length, 56-bit key and an efficient algorithm to quickly encrypt and decrypt messages. It can be easily implemented in hardware, making the encryption and decryption process even faster. In general, increasing the key size makes the system more secure. A variation of DES, called Triple-DES or DES-EDE (encrypt-decrypt-encrypt), uses three applications of DES and two independent DES keys to produce an effective key length of 168 bits [ANSI 85].
The International Data Encryption Algorithm (IDEA) was invented by James Massey and Xuejia Lai of ETH Zurich, Switzerland in 1991. IDEA uses a fixed length, 128-bit key (larger than DES but smaller than Triple-DES). It is also faster than Triple-DES. In the early 1990s, Don Rivest of RSA Data Security, Inc., invented the algorithms RC2 and RC4. These use variable length keys and are claimed to be even faster than IDEA. However, implementations may be exported from the U.S. only if they use key lengths of 40 bits or fewer.
Despite the efficiency of  symmetric key cryptography , it has a fundamental weak spot-key management. Since the same key is used for encryption and decryption, it must be kept secure. If an adversary knows the key, then the message can be decrypted. At the same time, the key must be available to the sender and the receiver and these two parties may be physically separated. Symmetric key cryptography transforms the problem of transmitting messages securely into that of transmitting keys securely. This is an improvement , because keys are much smaller than messages, and the keys can be generated beforehand. Nevertheless, ensuring that the sender and receiver are using the same key and that potential adversaries do not know this key remains a major stumbling block. This is referred to as the key management problem.

Public/Private Key Cryptography

Asymmetric key cryptography overcomes the key management problem by using different encryption and decryption key pairs. Having knowledge of one key, say the encryption key, is not sufficient enough to determine the other key - the decryption key. Therefore, the encryption key can be made public, provided the decryption key is held only by the party wishing to receive encrypted messages (hence the name public/private key cryptography). Anyone can use the public key to encrypt a message, but only the recipient can decrypt it.
RSA  is a widely used public/private key algorithm is, named after the initials of its inventors, Ronald L. Rivest, Adi Shamir, and Leonard M. Adleman [RSA 91]. It depends on the difficulty of factoring the product of two very large prime numbers. Although used for encrypting whole messages, RSA is much less efficient than symmetric key algorithms such as DES. ElGamal is another public/private key algorithm [El Gamal 85]. This uses a different arithmetic algorithm than RSA, called the discrete logarithm problem.
The mathematical relationship between the public/private key pair permits a general rule: any message encrypted with one key of the pair can be successfully decrypted only with that key's counterpart. To encrypt with the public key means you can decrypt only with the private key. The converse is also true - to encrypt with the private key means you can decrypt only with the public key.
 Salomaa 96.

Hash functions

 “Is a type of one-way function this  are fundamental for much of cryptography. A one way function - is a function that is easy to calculate but hard to invert. It is difficult to calculate the input to the function given its output. The precise meanings of "easy" and "hard" can be specified mathematically. With rare exceptions, almost the entire field of public key cryptography rests on the existence of one-way functions
In this application, functions are characterized and evaluated in terms of their ability to withstand attack by an adversary. More specifically, given a message x, if it is computationally infeasible to find a message y not equal to x such that H(x) = H(y) then H is said to be a weakly collision-free hash function. A strongly collision-free hash function H is one for which it is computationally infeasible to find any two messages x and y such that H(x) = H(y).
The requirements for a good cryptographic hash function are stronger than those in many other applications (error correction and audio identification not included). For this reason, cryptographic hash functions make good stock hash functions--even functions whose cryptographic security is compromised, such as MD5 and SHA-1. The SHA-2 algorithm, however, has no known compromises”
hash function ca also be referred to as a function  with certain additional security properties to make it suitable for use as a primitive in various information security applications, such as authentication and message integrity. It  takes a long string (or message) of any length as input and produces a fixed length string as output, sometimes termed a message digest or a digital fingerprint.

A hash function at work
A hash function at work
In various standards and applications, the two most-commonly used hash functions are MD5 and SHA-1; however, as of 2005, security flaws have been identified in both algorithms.

From Wikipedia, the free encyclopaedia.



 Cryptographic hash function
 
Hash functions (a type of one-way function) are fundamental for much of cryptography. In this application, functions are characterized and evaluated in terms of their ability to withstand attack by an adversary. More specifically, given a message x, if it is computationally infeasible to find a message y not equal to x such that H(x) = H(y) then H is said to be a weakly collision-free hash function. A strongly collision-free hash function H is one for which it is computationally infeasible to find any two messages x and y such that H(x) = H(y).
The requirements for a good cryptographic hash function are stronger than those in many other applications (error correction and audio identification not included). For this reason, cryptographic hash functions make good stock hash functions--even functions whose cryptographic security is compromised, such as MD5 and SHA-1. The SHA-2 algorithm, however, has no known compromises
 Fig 1 illustrates the proper and intended used of public/private key cryptography for sending confidential messages. In the illustration, a user, Bob, has a public/private key pair. The public portion of that key pair is placed in the public domain (for example in a Web server). The private portion is guarded in a private domain, for example, on a digital key card or in a password-protected file.
Figure 1: Proper Use of Public Key Cryptography
For Alice to send a secret message to Bob, the following process needs to be followed:
  1. Alice passes the secret message and Bob's public key to the appropriate encryption algorithm to construct the encrypted message.
  2. Alice transmits the encrypted message (perhaps via e-mail) to Bob.
  3. Bob decrypts the transmitted, encrypted message with his private key and the appropriate decryption algorithm.
Bob can be assured that Alice's encrypted secret message was not seen by anyone else since only his private key is capable of decrypting the message

Both Are Used Together

Secret key and public key systems are often used together, such as the AES secret key and the RSA public key. The secret key method provides the fastest decryption, and the public key method provides a convenient way to transmit the secret key. This is called a "digital envelope." For example, the PGP e-mail encryption program uses one of several public key methods to send the secret key along with the message that has been encrypted with that secret key (see PGP).

Get Faster - Get Stronger

 
It has been said that any encryption code can be broken given enough time to compute all permutations. However, if it takes months to break a code, the war could already be lost, or the thief could have long absconded with the money from the forged financial transaction. As computers get faster, to stay ahead of the game, encryption algorithms have to become stronger by using longer keys and more clever techniques.


Encryption and Decryption in PHP part : 2

Encryption and Decryption in PHP


<?php

$key = 'Your Key';
$string = 'My String'; // note the spaces

$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));

$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");

echo 'Encrypted:' . "\n";
var_dump($encrypted);

echo "\n";

echo 'Decrypted:' . "\n";
var_dump($decrypted); // spaces are preserved

?>

Output : 

Origional : 'My String'

Encrypted:
string 'X7zPEx/hl2aMvBrK2+V3XJtqvQssSiYUQWTFyHlbjmQ=' (length=44)
Decrypted:
string 'My String' (length=9)

Simple PHP encrypt and decrypt part : 1

/**
* simple method to encrypt or decrypt a plain text string
* initialization vector(IV) has to be the same when encrypting and decrypting
* PHP 5.4.9
*
* this is a beginners template for simple encryption decryption
* before using this in production environments, please read about encryption
*
* @param string $action: can be 'encrypt' or 'decrypt'
* @param string $string: string to encrypt or decrypt
*
* @return string
*/
function encrypt_decrypt($action, $string) {
$output = false;

$encrypt_method = "AES-256-CBC";
$secret_key = 'This is my secret key';
$secret_iv = 'This is my secret iv';

// hash
$key = hash('sha256', $secret_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 = "This is my plain text";
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";

md5 Encryption/Decryption


md5

(PHP 4, PHP 5)
md5Calculate the md5 hash of a string

Description

string md5 ( string $str [, bool $raw_output = false ] )
Calculates the MD5 hash of str using the » RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash.

Parameters

str
The string. 

raw_output
 
If the optional raw_output is set to TRUE, then the md5 digest is instead returned in raw binary format with a length of 16.

Return Values

Returns the hash as a 32-character hexadecimal number.




Example #1 A md5() example
<?php
$str 
'apple';

if (
md5($str) === '1f3870be274f6c49b3e31a0c6728957f') {
    echo 
"Would you like a green or red apple?";
}
?>

AUTO-APPROVE TIME OFF REQUESTS (phpcode)

<?php
// EXAMPLE: AUTO-APPROVE TIME OFF REQUESTS
//
// This code example illustrates how to listen for newly created requests for time off, then
// automatically approve them (this is great for organizations that have a more flexible time
// off policy). This script listens for a WebHook event, then connects to the site via the REST
// API to get the details and modify the details of the request.
//
// More on WebHooks: http://developers.tribehr.com/api/webhook-event-notification/
// More on the REST Api: http://developers.tribehr.com/api/api-introduction/
//
// PHP API Wrapper: https://github.com/TribeHR/TribeHR-PHP-Client
// Set your TribeHR credentials
define('SUBDOMAIN', ''); // Your TribeHR Sub Domain
define('USER', ''); // The username of an Administrator
define('KEY', ''); // The API KEY of the administrator above
// Include the API Wrapper
require('./TribeHR-PHP-Client/TribeHR.php');
// Let's only process the payload if we're going to actually be getting
// a LeaveRequest. Note - this logic can be easily extende to create a script that
// can handle multiple different WebHook types.
if(isset($_POST['object_id']) && isset($_POST['object']) && $_POST['object'] == 'LeaveRequest')
{
// Create my connection to the site using the API Wrapper
$TribeHRConnection = new TribeHRConnector(SUBDOMAIN, USER, KEY);
// Get the specific Leave Request, as specified by the WebHook data
$id = intval($_POST['object_id']);
$tc = $TribeHRConnection->sendRequest('/leave_requests/'.$id.'.xml', 'GET');
// Create the new request data, based on the old request. Some interesting constraints
// caused by the current API version:
// - date_start and date_end exped m/d/Y format, but the API returns Y-m-d
// - the API requires a complete data object, so all fields must be specified
// - the status APPROVED == 1
$old_request = simplexml_load_string($tc->response);
$new_request = array(
'id' => (string) $old_request->leave_request['id'],
'comments' => (string) $old_request->leave_request['comments'] . "\nAuto-Approved",
'date_start' => date('m/d/Y', strtotime($old_request->leave_request['date_start'])),
'date_end' => date('m/d/Y', strtotime($old_request->leave_request['date_end'])),
'days' => (string) $old_request->leave_request['days'],
'leave_type_id' => (string) $old_request->leave_request['leave_type_id'],
'user_id' => (string) $old_request->leave_request['user_id'],
'status' => 1,
);
// Submit the new data
$tc = $TribeHRConnection->sendRequest('/leave_requests/'.$id.'.xml', 'PUT', $new_request);
// Let's echo our the response for good measure
echo htmlentities($tc->response);
}
else
{
// Since there wasn't a valid payload, let's echo a message out
echo 'This script listens for TribeHR WebHooks to auto-approve vacation requests';
}
?>

Monday, 25 August 2014

Marquee style in different manner with PHP and MySql

marquee tag is used for more purpose, like top news publishing with marquee scrolling and more frequently publishing news or some other all things are scroll in a particular area, for that here we are using the marquee tag.

STOP / START

Marquee start and stop is the one main function most of us searching this thing, how is it happens on mouse hover. see the code.
<marquee behavior="scroll" onmouseover="this.stop();" onmouseout="this.start();">
</
marquee>

onmouseover="this.stop();" for stop the marquee content on on mouse over, onmouseout="this.start();" for start the marquee tag content on mouse out.

MARQUEE DIRECTION 

Marquee direction is for make the marquee move direction, the moving of marquee tag is move towards to which direction, like left to right, or right to left like that. for that we are using that marquee direction tag. see the code.
<marquee behavior="scroll" direction="right/left/up/down" onmouseover="this.stop();" onmouseout="this.start();">
</marquee>

choose any one direction in the above direction like left or right or up or down any one direction.

MARQUEE SPEED

Marquee  speed how reduce and increase the speed of marquee tag moving. see the code.
<marquee behavior="scroll" scrollamount="5" direction="right" onmouseover="this.stop();" onmouseout="this.start();">
</marquee>

that is scrollamount="5" we can give any number value for that, when you increase the value of scrollamount the speed will be increased, when you are reducing the scrollamount the speed will be decreased.

PHP and Mysql FETCH FROM DATABASE

<?php
$con=mysql_connect('localhost','root','');
$db=mysql_select_db('2my4edge',$con);
?>

Database name --> 2my4edge
table name --> marquee
column names --> id, feeds

<div class="main">
<?php
$fetch=mysql_query("select * from marquee limit 5");
if(mysql_num_rows($fetch))
{
?>
<marquee behavior="scroll" scrollamount="2" direction="up" onmouseover="this.stop();" onmouseout="this.start();">
<ul class="ulclass" >
<?php
while($row=mysql_fetch_array($fetch))
{
$id=$row['id'];
$feed=$row['feeds'];
?>
<li class="liclass">
<?php echo $feed; ?>
<?php } ?>
</li>
<?php } ?>
</ul>

</marquee>
</div>
you can clear with the above coding, just fetch data from the database, which is from marquee table, * astrick is for select all the field from the table name. and make the coding with simple.

Praphull's Marquees

Type this:

<MARQUEE>Your text</MARQUEE>

And you get this.

Your text



Type this:

<MARQUEE DIRECTION=RIGHT>Your text</MARQUEE>

And you get this.

Your text



Type this:

<MARQUEE BEHAVIOR=ALTERNATE>Your text</MARQUEE>

And you get this.

Your text



Type this:

<CENTER><MARQUEE BEHAVIOR=ALTERNATE WIDTH="40%">Your text</MARQUEE></CENTER>

And you get this.

Your text




Type this.

<MARQUEE DIRECTION=DOWN HEIGHT=60>Your text</MARQUEE>

And you get this.

Your text



Type this.

<MARQUEE DIRECTION=UP HEIGHT=50>Your text</MARQUEE>

And you get this.

Your text



Type this.

<MARQUEE DIRECTION=DOWN HEIGHT=50 BEHAVIOR=ALTERNATE>Your text</MARQUEE>

And you get this.

Your text

Replace text with animation or pictures.


Type this.

<MARQUEE><IMG SRC="Your picture URL"></MARQUEE>

And you get this.

DOG



Type this.

<MARQUEE DIRECTION=UP HEIGHT=70 BEHAVIOR=ALTERNATE><IMG SRC="Your picture URL here"> </MARQUEE>

And you get this.

ball



Type this.

<MARQUEE DIRECTION=RIGHT WIDTH="50%"><IMG SRC="Your picture URL here"></MARQUEE> <MARQUEE WIDTH="50%"><IMG SRC="Your picture URL here"></MARQUEE>

And you get this.

gif gif

Extra Stuff


Use the BGCOLOR attribute to change the background color of a marquee, specify the color by name or hexadecimal value for the color you want to use.

Type this:

<MARQUEE BEHAVIOR="SCROLL" DIRECTION="LEFT" SCROLLAMOUNT="2" SCROLLDELAY="60" BGCOLOR="RED">Your text here</MARQUEE>

And you get this.

Your text here

.

Popular Posts

Powered by Blogger.