i have tried lots of stuff, and im still having trouble... im working w/ titanium appcelerator studio (newest version) on a pc (win7) creating desktop apps with php. all my other tests have been going great so far, but i have run into trouble using the mcrypt functions.
here's the code im working with:
<html>
<head>
<title> crypto </title>
<style type="text/css">
*, html, body {
margin: 0;
padding: 0;
border: 0;
}
body {
background: #000;
color: #fff;
font: normal 12pt Arial, Helvetica, San-serif;
}
.wrap {
margin: 20px auto;
width: 80%;
border: 1px solid #ccc;
background: #333;
}
.row {
padding: 5px;
border-bottom: 1px solid #666;
}
</style>
<script type="text/php">
function check($name) {
$d = '<div class="row">';
$e = '</div>';
if(function_exists($name)) {
return $d.$name.": found".$e;
} else {
return $d.$name.": not found".$e;
}
}
function test1() {
$key = "this is a secret key";
$input = "Let us meet at 9 o'clock at the secret place.";
$encrypted_data = mcrypt_cbc('tripledes', $key, $input, 'mcrypt_encrypt');
$d = '<div class="row">';
$b = '<br/>';
$e = '</div>';
return $d."key:".$b.$key.$e.$d."clear text:".$b.$input.$e.$d."encrypted:".$b.$encrypted_data.$e;
}
function test2() {
$key = "this is a secret key";
$input = "Let us meet at 9 o'clock at the secret place.";
$td = mcrypt_module_open('tripledes', '', 'cbc', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), 'mcrypt_rand');//alt_mcrypt_create_iv(8));
mcrypt_generic_init($td, $key, $iv);
$encrypted_data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$d = '<div class="row">';
$b = '<br/>';
$e = '</div>';
return $d."key:".$b.$key.$e.$d."clear text:".$b.$input.$e.$d."encrypted:".$b.$encrypted_data.$e;
}
function alt_mcrypt_create_iv($size) {
$iv = '';
for($i = 0; $i < $size; $i++) {
$iv .= chr(rand(0,255));
}
return $iv;
}
</script>
<script language="JavaScript" type="text/javascript" src="lib/js/jq-min.js"></script>
<script type="text/javascript">
function ready() {
$("#box").html(check("md5"));
$("#box").append(check("sha1"));
$("#box").append(check("xero")); // a fake function name for testing
$("#box").append(check("mcrypt_encrypt"));
$("#box").append(check("mcrypt_decrypt"));
$("#box").append(check("mcrypt_cbc"));
$("#box").append(check("mcrypt_cfb"));
$("#box").append(check("mcrypt_ecb"));
$("#box").append(check("mcrypt_ofb"));
$("#box").append(check("mcrypt_generic"));
$("#box").append(check("mcrypt_module_open"));
$("#box").append(check("mcrypt_module_close"));
$("#box").append(check("mcrypt_create_iv"));
$("#box").append(check("mcrypt_generic_init"));
$("#box").append(check("mcrypt_generic_deinit"));
$("#box2").html(test1());
$("#box3").html(test2());
}
</script>
</head>
<body>
<div id="box2" class="wrap">
<input type="button" value="test" onclick="ready()" />
</div>
<div id="box3" class="wrap">
</div>
<div id="box" class="wrap">
</div>
</body>
</html>
basically there are 3 div, then you press the 'test' button a series of tests are run. first i call function_exists lots of times to see if the mcrypt functions are available (the are showing up correctly). then i execute test1 and test2 functions, each of which try and do a simple string encryption and return the results. one uses the old method mcrypt_cbc and the other the newer one mcrypt_generic.
when i run the app, everything seems to execute ok, e.g. im not seeing any error in the debugger (though php开发者_开发知识库 error handling is a bit weird in Ti). but when i return the $encrypted_data variable from ether function nothing is returned. if i remove it from the return statement, i get all the other variables, but not the part i want.
any other Ti users have any suggestions for me?
thanx in advance.
精彩评论