Javascript Encryption (rijndael-256, cbc)

  Client Side Encryption (rijndael-256, cbc)


 

Javascript Source

<script src="/js/crypt/rijndael.js"></script>
<script src="/js/crypt/mcrypt.js"></script>
<script src="/js/crypt/cryptojs-core.js"></script>
<script src="/js/crypt/lib-typedarrays.js"></script>
<script src="/js/crypt/md5.js"></script>
<script>
function encrypt_me(){
	var str = $('#str').val();
	var key1 = $('#key1').val();
	var key2 = $('#key2').val();
	key1 = md5(key1);
	key2 = md5(key2);
 
	var ks = mcrypt.get_key_size('rijndael-256', 'cbc');
	var kpart1 = key1.substr(0, ks/2);
	var substr1 = key2.toUpperCase();
	var substr2 = Math.round(key2.length/2);
	var substr3 = ks/2;
	var kpart2 = substr1.substr(substr2,substr3);
	var key = kpart1.concat(kpart2);
 
	var join1 = key.concat(key1);
	var join2 = join1.concat(key2);
	var join3 = join2.concat(key1.toUpperCase());
	key = join3.substr(0,ks);
 
	$('#iv').val(String(CryptoJS.lib.WordArray.random(16)));
	var iv  = $('#iv').val();
	$('#key').val(key);
	$('#length').val(str.length);
	$('#enc_str').val(btoa(mcrypt.Encrypt(str, iv, key, 'rijndael-256', 'cbc')));
}
function decrypt_me(){
	var enc_str = atob($('#enc_str').val());
	var key1 = $('#key1').val();
	var key2 = $('#key2').val();
	key1 = md5(key1);
	key2 = md5(key2);
 
	var ks = mcrypt.get_key_size('rijndael-256', 'cbc');
	var kpart1 = key1.substr(0, ks/2);
	var substr1 = key2.toUpperCase();
	var substr2 = Math.round(key2.length/2);
	var substr3 = ks/2;
	var kpart2 = substr1.substr(substr2,substr3);
	var key = kpart1.concat(kpart2);
 
	var join1 = key.concat(key1);
	var join2 = join1.concat(key2);
	var join3 = join2.concat(key1.toUpperCase());
	key = join3.substr(0,ks);
 
	var iv  = $('#iv').val();
	var length = $('#length').val();
	$('#decrypted').val(mcrypt.Decrypt(enc_str, iv, key, 'rijndael-256', 'cbc').substring(0,length));
}
</script>

PHP Source

function get_decrypted(){
	$key1 = $this->input->post('key1');
	$query = $this->db->from('secure')->limit(1)->get();
	if ($query->num_rows()>0){
		$row = $query->row();
		$iv = $row->secure_iv;
		$encrypted = $row->secure_data;
		// $length = $row->secure_len;
	}
	$key2 = '9fio6gRXtubLheVtGfDOuf2jqetMxXE8';
	$td = mcrypt_module_open('rijndael-256', '', 'cbc', '');
	$ks = mcrypt_enc_get_key_size($td);
	$key1 = md5($key1);
	$key2 = md5($key2);
	$key = substr($key1, 0, $ks/2) . substr(strtoupper($key2), (round(strlen($key2) / 2)), $ks/2);
	$key = substr($key.$key1.$key2.strtoupper($key1),0,$ks);
	mcrypt_generic_init($td, $key, $iv);
	$decrypted = mdecrypt_generic($td, $encrypted);
	mcrypt_generic_deinit($td);
	mcrypt_module_close($td);
	// echo 'Text: '.substr($decrypted,0,$length);
	if (strpos($decrypted,'TRUE')===0){
		echo 'Text: '.rtrim(ltrim($decrypted,'TRUE'),"");
	}else{
		echo 'Wrong Password!';
	}
}
function set_encrypted(){
	$input = 'TRUE'.$this->input->post('text');
 
	$key1 = $this->input->post('key1');
	$key2 = '9fio6gRXtubLheVtGfDOuf2jqetMxXE8';
	$length = strlen($input);
	$td = mcrypt_module_open('rijndael-256', '', 'cbc', '');
	$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
	$ks = mcrypt_enc_get_key_size($td);
	$key1 = md5($key1);
	$key2 = md5($key2);
	$key = substr($key1, 0, $ks/2) . substr(strtoupper($key2), (round(strlen($key2) / 2)), $ks/2);
	$key = substr($key.$key1.$key2.strtoupper($key1),0,$ks);
	mcrypt_generic_init($td, $key, $iv);
	$encrypted = mcrypt_generic($td, $input);
	mcrypt_generic_deinit($td);
	mcrypt_module_close($td);
	$this->db->set('secure_iv', $iv);
	$this->db->set('secure_data', $encrypted);
	// $this->db->set('secure_len', $length);
	$this->db->set('secure_len', '0');
	$this->db->where('secure_id', '1');
	$this->db->update('secure');
	echo 1;
}

C Source (part code from a DLL module we built for PHP)

key1 = php_md5(key1);
key2 = php_md5(key2);
 
kpart1 = php_substr(key1, 0, ks/2);
substr1 = php_strtoupper(key2,strlen(key2));
substr2 = round(strlen(key2)/2);
substr3 = ks/2;
kpart2 = php_substr(substr1,substr2,substr3);
key = strcat(kpart1,kpart2);
 
join1 = strcat(key,key1);
join2 = strcat(join1,key2);
join3 = strcat(join2,php_strtoupper(key1,strlen(key1)));
 
key = php_substr(join3,0,ks);
 
td = mcrypt_module_open(MCRYPT_RIJNDAEL_256, NULL, "cbc", NULL);
 
if (td == MCRYPT_FAILED){
	// Mcrypt module open failed
}
 
/** Initialize cipher with key and IV */
i = mcrypt_generic_init(td, key, ks, iv);
if(i < 0){
	// mcrypt_perror
}
 
mdecrypt_generic(td, lic, block_size);
 
mcrypt_generic_deinit(td);
mcrypt_module_close(td);