Showing posts with label aes. Show all posts
Showing posts with label aes. Show all posts

Saturday, June 28, 2014

Encrypt & Decrypt String Using AES Algorithm & jQuery


<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="crypto-js/core-min.js"></script>
<script src="crypto-js/enc-utf16-min.js"></script>
<script src="crypto-js/enc-base64-min.js"></script>
<script src="crypto-js/aes.js"></script>
<script>
 $(function() {
  $('#test').on('submit', function() {
   var plaintext = $('#text').val();
   var secret = $('#secret').val();
   var encrypted = '' + CryptoJS.AES.encrypt(plaintext, secret);
   $("#output").prepend("<br/>Encrypted: " + encrypted);
   var decrypted = CryptoJS.AES.decrypt(encrypted, secret);
   $("#output").prepend("<br/><br/> Original From Encrypted: " + decrypted.toString(CryptoJS.enc.Utf8));

   return false;
  });  
 });   
</script>
</head> 
<body lang="en">  
    <form id="test">
        <p>
            <label>Secret</label><br>
            <input id="secret" value="Secret Passphrase">
        </p>
        <p>
            <label>Text</label><br>
            <textarea id="text">This is the secret message</textarea>
        </p>
        <input type="submit" id="submit">
    </form>
    <div id="output"></div>  
</body>
</html>


Thursday, November 22, 2012

Encrypt and decrypt string using AES 128 and PKCS7 algorithm using Php

Encrypt and decrypt string using AES algorithm executed by PHP script is given below:


<?php
function encrypt($key, $to_encrypt)
{
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM);
    $pad = $iv_size - (strlen($to_encrypt) % $iv_size);
    $to_encrypt = str_pad($to_encrypt, ($iv_size * (floor(strlen($to_encrypt) / $iv_size) + 1)), chr($pad));
    return base64_encode($iv . mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $to_encrypt, MCRYPT_MODE_CBC, $iv));
}

function decrypt($key, $to_decrypt)
{
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = substr($to_decrypt, 0, $iv_size);
    $to_decrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, substr($to_decrypt, $iv_size), MCRYPT_MODE_CBC, $iv);
    $pad = ord($to_decrypt[strlen($to_decrypt) - 1]);
    return substr($to_decrypt, 0, -$pad);
}

$key = "1111111111111111";

$str = "Value to be encrypt";
$enc = encrypt($key, $str);
$dec = decrypt($key, base64_decode($enc));

echo "Original=" . $str;
echo "<BR>Encrypted=" . $enc;
echo "<BR>Decrypted=" . $dec;

Below is output:

Original=Value to be encrypt
Encrypted=fyqhmBCGLVXcT5tvq38G/MAlpgP2FYq29wgC7KIMT8jt6Z45cq63oJeY8kGgv9QP
Decrypted=Value to be encrypt
 
 

Encrypt and decrypt string using AES algorithm using Java

Encrypt and decrypt string or bytes using AES algorithm performed by Java code. below is the code snippet. 

package com.pkm;

import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptUtils {
    private static String algorithm = "AES";
    private static byte[] keyValue = "1111111111111111".getBytes();
    private static SecureRandom iv = new SecureRandom("192837465UTYELBN".getBytes());
    
    public static void main(String[] args) throws Exception {
        String valueToEncrypt = "Some value";
        String encryptedValue = encrypt(valueToEncrypt);
        System.out.println("Encrypted=" + encryptedValue);
        
        String afterDecrypt = decrypt(encryptedValue);
        System.out.println("Decrypted=" + afterDecrypt);
    }

    static String encrypt(String value) throws Exception {
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);
        byte [] encByte = cipher.doFinal(value.getBytes());
        String encString = new BASE64Encoder().encode(encByte);
        return encString;
    }

    static String decrypt(String value) throws Exception {
        Key key = generateKey();
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte [] decByte64 = new BASE64Decoder().decodeBuffer(value);
        byte [] decByte = cipher.doFinal(decByte64);
        String decString = new String(decByte);
        return decString;
    }

    static private Key generateKey() throws Exception {
        return new SecretKeySpec(keyValue, algorithm);
    }
}


Below is output:

Original=Some value
Encrypted=i4he5mR49eRkWmQRVh12yg==
Decrypted=Some value