十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
先看用什么方式的加密,拿AES来说,你需要问java要到混淆值、初始化向量与AES加密的方式如AES-192-CFB.然后直接调用openssl_decrypt方法进行解密.
创新互联自2013年起,是专业互联网技术服务公司,拥有项目成都网站制作、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元布尔津做网站,已为上家服务,为布尔津各地企业和个人服务,联系电话:13518219792
openssl_decrypt('需要解密的字符串','AES-192-CFB','混淆值',0,'初始化向量'),true)
DES是一种标准的数据加密算法,关于这个算法的详细介绍可以参考wiki和百度百科:
php中有一个扩展可以支持DES的加密算法,是:extension=php_mcrypt.dll
在配置文件中将这个扩展打开还不能够在windows环境下使用
需要将PHP文件夹下的 libmcrypt.dll 拷贝到系统的 system32 目录下,这是通过phpinfo可以查看到mcrypt表示这个模块可以正常试用了。
下面是PHP中使用DES加密解密的一个例子:
//$input - stuff to decrypt
//$key - the secret key to use
function do_mencrypt($input, $key)
{
$input = str_replace(""n", "", $input);
$input = str_replace(""t", "", $input);
$input = str_replace(""r", "", $input);
$key = substr(md5($key), 0, 24);
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$encrypted_data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return trim(chop(base64_encode($encrypted_data)));
}
//$input - stuff to decrypt
//$key - the secret key to use
function do_mdecrypt($input, $key)
{
$input = str_replace(""n", "", $input);
$input = str_replace(""t", "", $input);
$input = str_replace(""r", "", $input);
$input = trim(chop(base64_decode($input)));
$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$key = substr(md5($key), 0, 24);
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$decrypted_data = mdecrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return trim(chop($decrypted_data));
}
参考自:
php,jsp,delphi多语言兼容的简单的加密解密算法,jsp目前暂时没有实现中文的加密解密兼容。希望哪路高手能帮忙补上。
1,php的需要对以上代码稍加修改,内容如下:
?php
$key = "admin";
function keyED($txt,$encrypt_key) {
$encrypt_key = md5($encrypt_key);
$ctr=0;
$tmp = "";
for ($i=0;$istrlen($txt);$i++){
if ($ctr==strlen($encrypt_key)) $ctr=0;
$tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);
$ctr++;
}
return $tmp;
}
function encrypt($txt,$key){
srand((double)microtime()*1000000);
$encrypt_key = md5(rand(0,32000));
$ctr=0;
$tmp = "";
for ($i=0;$istrlen($txt);$i++){
if ($ctr==strlen($encrypt_key)) $ctr=0;
$bbb=substr($encrypt_key,$ctr,1) .
(substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));
$tmp.= $bbb;
$ctr++;
}
return base64_encode(keyED($tmp,$key));
}
function decrypt($txt,$key){
$txt=base64_decode($txt);
$txt = keyED($txt,$key);
$tmp = "";
for ($i=0;$istrlen($txt);$i++){
$md5 = substr($txt,$i,1);
$i++;
$tmp.= (substr($txt,$i,1) ^ $md5);
}
return $tmp;
}
$string = "hello world!";
// encrypt $string, and store it in $enc_text
$enc_text = encrypt($string,$key);
// decrypt the encrypted text $enc_text, and store it in $dec_text
$dec_text = decrypt($enc_text,$key);
echo "原字符串: ".$string."br/";
echo "加密字符串: ".$enc_text."br/";
echo "解密字符串: ".$dec_text."br/";
?
2,jsp代码(java类)
package com.intwork.struct点抗 mon;
import java.util.Random;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class KeyED {
public char[] keyED(char[] cText,String encrype_key){
encrype_key=MD5.getInstance(encrype_key).encrypt();
int ctr=0;
char[] cEncrype_key=encrype_key.toCharArray();
char[] temp=new char[cText.length];
int ind=0;
String tmp="";
for(int i=0;icText.length;i++){
if(ctr==cEncrype_key.length){
ctr=0;
}
temp[ind++]=(char)(cText[i] ^ cEncrype_key[ctr]);
ctr++;
}
return temp;
}
public String encrype(String text,String key){
String encrype_key=MD5.getInstance(String.valueOf(new Random().nextInt(1000000))).encrypt();
int ctr=0;
char[] cText=text.toCharArray();
char[] cEncrype_key=encrype_key.toCharArray();
char[] temp=new char[cText.length*2];
int ind=0;
String tmp="";
for(int i=0;icText.length;i++){
if(ctr==cEncrype_key.length){
ctr=0;
}
temp[ind++]=cEncrype_key[ctr];
temp[ind++]=(char)(cText[i]^cEncrype_key[ctr]);
ctr++;
}
BASE64Encoder b = new sun.misc.BASE64Encoder();
return b.encode(new String(this.keyED(temp,key)).getBytes());
}
public String decrype(String text,String key)throws Exception{
BASE64Decoder decoder = new BASE64Decoder();
text=new String(decoder.decodeBuffer(text));
char[] cText=text.toCharArray();
char[] temp=new char[cText.length/2];
int ind=0;
cText=this.keyED(cText, key);
String tmp="";
for(int i=0;icText.length;i++){
char md5=cText[i];
i++;
temp[ind++]=(char)(cText[i]^md5);
}
return new String(temp);
}
}
3,delphi代码(只有加密代码,没写解密代码。有兴趣的可以给补上)
function Tfrmunmsgmain.EnPlugPwcrypt(str, skey: string): string;
//加密插件用的字符串
var
IdEncoder64: TIdEncoderMIME;
sencrypt_key, stemp, stemp1 : string;
ictr, i : Integer;
ifirst, ilast : Integer;
function enpwcrypt(str, skey: string): string;
//获取加密后的字符串
var
stemp, sencrypt_key : string;
ictr, i : Integer;
ifirst, ilast : Integer;
begin
try
result := '';
try
sencrypt_key := LowerCase(UMD5Crypt.UGetMd5Str(skey));
ictr := 1;
stemp := '';
for I := 1 to length(str) do
begin
if (ictr = length(sencrypt_key)) then
ictr := 1;
ifirst := Ord(midstr(str,i,1)[1]);
ilast := Ord(midstr(sencrypt_key,ictr,1)[1]);
stemp := stemp +
chr((ifirst) XOR (ilast));
inc(ictr);
end;
result := stemp;
except
end;
except
end;
//
end;
begin
try
result := '';
try
IdEncoder64 := TIdEncoderMIME.Create(Application);
sencrypt_key := LowerCase(UMD5Crypt.UGetMd5Str(inttostr((Random(100000)))));
ictr := 1;
stemp := '';
stemp1 := '';
for I := 1 to length(str) do
begin
if (ictr = length(sencrypt_key)) then
ictr := 1;
ifirst := Ord(midstr(str,i,1)[1]);
ilast := Ord(midstr(sencrypt_key,ictr,1)[1]);
stemp1 := midstr(sencrypt_key,ictr,1) +
chr((ifirst) XOR (ilast));
stemp := stemp + stemp1;
inc(ictr);
end;
result := IdEncoder64.Encode(enpwcrypt(stemp, skey))
finally
freeandnil(IdEncoder64);
end;
except
end;
end;