phpjava加密
function
encryptTokey($data){
$apikey
=
'testapikey111';
$ps1
=
sha1($apikey
.
strtolower($data));
$ps1
=
strtoupper($ps1);
$s1
=
implode(str_split($ps1,
2),
'-');
$ps2
=
md5($s1
.
$apikey);
$ps2
=
strtoupper($ps2);
$token
=
implode(str_split($ps2,
2),
'-');
return
$token;
}
echo
encryptTokey('testdata');
运行结果:
68-10-98-74-4C-82-74-4B-CC-49-31-98-46-02-EE-8E
详细你可以去后盾人看看,这些都是后盾人里面的,哪里有详细的视频教学都是高质量,我自己就是在里面学的。
② 电脑里Java php的代码资料可以防泄密吗
可以的。推荐的是使用绿盾加密,采用的是文件透明加密模块,对平常办公使用是没有影响的。而且绿盾支持与SVN等源代码管理工具无缝结合。
如果企业内部SVN服务器采取透明模式,即加密文件是可以存放在SVN服务器上的,需要达到的效果是SVN服务器上文件密文存储。则配合天锐绿盾应用服务器安全接入系统来实现只有安装了加密客户端的Windows、Linux、MAC端才能够正常的访问公司内部的SVN服务器。
如果企业内部采用eclipse、VS等开发工具,从这些开发工具将代码直接上传到SVN服务器上时会自动解密。为了避免明文、密文混乱存放导致版本比对时出现错误等问题。因此,SVN服务器上需统一存放明文文件。则通过服务器白名单功能实现对终端电脑数据进行强制透明加密,对上传到应用服务器数据实现上传自动解密、下载自动加密。再配合天锐绿盾应用服务器安全接入系统实现只有安装了加密客户端的Windows、Linux、MAC端才能够正常的访问公司内部的SVN服务器。
赛虎信息科技始终倾力为企事业单位的信息安全、绿盾数据防泄密提供一体化顾问式解决方案,为客户提供优质的内网安全管理产品和适合多种行业的应用解决方案。
③ PHP JAVA AES加密 转换
PHP代码:
<?php
class Security {
public static function encrypt($input, $key) {
$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$input = Security::pkcs5_pad($input, $size);
$td = mcrypt_mole_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_mole_close($td);
$data = base64_encode($data);
return $data;
}
private static function pkcs5_pad ($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
public static function decrypt($sStr, $sKey) {
$decrypted= mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$sKey,
base64_decode($sStr),
MCRYPT_MODE_ECB
);
$dec_s = strlen($decrypted);
$padding = ord($decrypted[$dec_s-1]);
$decrypted = substr($decrypted, 0, -$padding);
return $decrypted;
}
}
$key = "1234567891234567";
$data = "example";
$value = Security::encrypt($data , $key );
echo $value.'<br/>';
echo Security::decrypt($value, $key );
---------------
java 代码
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class Security {
public static String encrypt(String input, String key){
byte[] crypted = null;
try{
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey);
crypted = cipher.doFinal(input.getBytes());
}catch(Exception e){
System.out.println(e.toString());
}
return new String(Base64.encodeBase64(crypted));
}
public static String decrypt(String input, String key){
byte[] output = null;
try{
SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skey);
output = cipher.doFinal(Base64.decodeBase64(input));
}catch(Exception e){
System.out.println(e.toString());
}
return new String(output);
}
public static void main(String[] args) {
String key = "1234567891234567";
String data = "example";
System.out.println(Security.encrypt(data, key));
System.out.println(Security.decrypt(Security.encrypt(data, key), key));
}
}
④ php的base64加密,怎么调整才能和java的base64的加密结果一致呢
phpbase64以后每76个字符加一个换行,
function javaBase64Encode($str)
{
$str = base64_encode($str);
$strLength = strlen($str);
$n = intval($strLength / 76);
if ($n <= 0) {
return $str;
}
for ($i = 1; $i <= $n; $i++) {
$position = 76 * $i + ($i - 1);
$str = substr_replace($str, PHP_EOL, $position, 0);
}
return $str;
}
function javaBase64Decode($str)
{
$strLength = strlen($str);
$n = intval($strLength / 76);
if ($n <= 0) {
return $str;
}
for ($i = $n; $i >= 1; $i--) {
$position = 76 * $i + ($i - 1);
$str = substr_replace($str, "", $position, 1);
}
return base64_decode($str);
}
或者使用chunk_split 函数也可以,默认就是76,而且不是所有的java代码都需要这样转,要看java调用的是什么类库,有一些是不用的
⑤ php AES加密对不上java的加密,请问如何实现
要注意特定的Padding实现跟算法的blockSize有关,这里php的blocksize是在php的aes加密前先对源字符串进行Padding,问题得到解决。
⑥ 这段JAVA加密用php怎么写
php写法如下:
<?PHP
/**
* AES加密、解密类
* @author hushangming
*
* 用法:
* <pre>
* // 实例化类
* // 参数$_bit:格式,支持256、192、128,默认为128字节的
* // 参数$_type:加密/解密方式,支持cfb、cbc、nofb、ofb、stream、ecb,默认为ecb
* // 参数$_key:密钥,默认为abcdefghijuklmno
* $tcaes = new TCAES();
* $string = 'laohu';
* // 加密
* $encodeString = $tcaes->encode($string);
* // 解密
* $decodeString = $tcaes->decode($encodeString);
* </pre>
*/
class TCAES{
private $_bit = MCRYPT_RIJNDAEL_256;
private $_type = MCRYPT_MODE_CBC;
//private $_key = '';
private $_key = 'abcdefghijuklmno'; // 密钥
private $_use_base64 = true;
private $_iv_size = null;
private $_iv = null;
/**
* @param string $_key 密钥
* @param int $_bit 默认使用128字节
* @param string $_type 加密解密方式
* @param boolean $_use_base64 默认使用base64二次加密
*/
public function __construct($_key = '', $_bit = 128, $_type = 'ecb', $_use_base64 = true){
// 加密字节
if(192 === $_bit){
$this->_bit = MCRYPT_RIJNDAEL_192;
}elseif(128 === $_bit){
$this->_bit = MCRYPT_RIJNDAEL_128;
}else{
$this->_bit = MCRYPT_RIJNDAEL_256;
}
// 加密方法
if('cfb' === $_type){
$this->_type = MCRYPT_MODE_CFB;
}elseif('cbc' === $_type){
$this->_type = MCRYPT_MODE_CBC;
}elseif('nofb' === $_type){
$this->_type = MCRYPT_MODE_NOFB;
}elseif('ofb' === $_type){
$this->_type = MCRYPT_MODE_OFB;
}elseif('stream' === $_type){
$this->_type = MCRYPT_MODE_STREAM;
}else{
$this->_type = MCRYPT_MODE_ECB;
}
// 密钥
if(!empty($_key)){
$this->_key = $_key;
}
// 是否使用base64
$this->_use_base64 = $_use_base64;
$this->_iv_size = mcrypt_get_iv_size($this->_bit, $this->_type);
$this->_iv = mcrypt_create_iv($this->_iv_size, MCRYPT_RAND);
}
/**
* 加密
* @param string $string 待加密字符串
* @return string
*/
public function encode($string){
if(MCRYPT_MODE_ECB === $this->_type){
$encodeString = mcrypt_encrypt($this->_bit, $this->_key, $string, $this->_type);
}else{
$encodeString = mcrypt_encrypt($this->_bit, $this->_key, $string, $this->_type, $this->_iv);
}
if($this->_use_base64)
$encodeString = base64_encode($encodeString);
return $encodeString;
}
/**
* 解密
* @param string $string 待解密字符串
* @return string
*/
public function decode($string){
if($this->_use_base64)
$string = base64_decode($string);
$string = $this->toHexString($string);
if(MCRYPT_MODE_ECB === $this->_type){
$decodeString = mcrypt_decrypt($this->_bit, $this->_key, $string, $this->_type);
}else{
$decodeString = mcrypt_decrypt($this->_bit, $this->_key, $string, $this->_type, $this->_iv);
}
return $decodeString;
}
/**
* 将$string转换成十六进制
* @param string $string
* @return stream
*/
private function toHexString ($string){
$buf = "";
for ($i = 0; $i < strlen($string); $i++){
$val = dechex(ord($string{$i}));
if(strlen($val)< 2)
$val = "0".$val;
$buf .= $val;
}
return $buf;
}
/**
* 将十六进制流$string转换成字符串
* @param stream $string
* @return string
*/
private function fromHexString($string){
$buf = "";
for($i = 0; $i < strlen($string); $i += 2){
$val = chr(hexdec(substr($string, $i, 2)));
$buf .= $val;
}
return $buf;
}
}
⑦ java 怎么实现PHP的base64加密,两种语言的base64加密后的数据不一致
在开发的时候遇到个现象。对方用PHP base64_encode() 对字符串进行加米。但我这边是用Java解马。导致出现问题。问题如下:
[java] view plain
package com.tudou.test;
import java.io.IOException;
/**
* <p>java base64编码和解码的演示类
* 注:base64编码后通过url传递时,获得时"="会给替换掉,* 处理方式:在编码前将"=","/","+" 替换成别的字符,在解码之前替换回来* </p>
* @author tw 2010-03-01
*
*/
public class TestBase64Net {
/**
* 编马
* @param filecontent
* @return String
*/
public static String encode(byte[] bstr){return new sun.misc.BASE64Encoder().encode(bstr);}
/**
* 解码
* @param filecontent
* @return string
*/
public static byte[] decode(String str){
byte[] bt = null;
try {
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();bt = decoder.decodeBuffer( str );
} catch (IOException e) {
e.printStackTrace();
}
return bt;
}
/**
* @param args
*/
public static void main(String[] args) {
TestBase64Net te = new TestBase64Net();
//PHP 用base64 将union_id=102155_100001_01_01 加米后的字符串为: //java 用sun.misc.BASE64Encoder().encode()进行解马,结果为:union_id=102155_100001_01_01乱码0System.out.println(new String(te.decode("")));//java 用sun.misc.BASE64Decoder 将union_id=102155_100001_01_01进行加米,结果为:==System.out.println(new String(te.encode("union_id=102155_100001_01_01".getBytes())));}
}
经过对比不难发现用php的base64_encode() 方法进行加米,JAVA 不能用sun.misc.BASE64Encoder().encode() 进行解米。那该怎么办?!
可以用apache的commons包 commons-codec-1.7.jar 中的org.apache.commons.codec.binary.Base64 进行解米。
[java] view plain
import org.apache.commons.codec.binary.Base64;public class TestBase64 {
public static void main(String[] args) {
System.out.println(new String(Base64.decodeBase64("".getBytes())));}
}
?
⑧ java的 DES 加密解密方法 求对应php的加密解密方法!!!!急切
DES是一种标准的数据加密算法,关于这个算法的详细介绍可以参考wiki和网络:
php中有一个扩展可以支持DES的加密算法,是:extension=php_mcrypt.dll
在配置文件中将这个扩展打开还不能够在windows环境下使用
需要将PHP文件夹下的 libmcrypt.dll 拷贝到系统的 system32 目录下,这是通过phpinfo可以查看到mcrypt表示这个模块可以正常试用了。
下面是PHP中使用DES加密解密的一个例子:
//$input-stufftodecrypt
//$key-thesecretkeytouse
functiondo_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_mole_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_mole_close($td);
returntrim(chop(base64_encode($encrypted_data)));
}
//$input-stufftodecrypt
//$key-thesecretkeytouse
functiondo_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_mole_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_mole_close($td);
returntrim(chop($decrypted_data));
}
参考自:http://www.cnblogs.com/cocowool/archive/2009/01/07/1371309.html
⑨ 如何让php能象java的方式md5加密
<?php//示例代码:$str = 'hello 这里是php preg_match正则匹配演示';// UTF8编码:正则表达式匹配中文;if(preg_match('/[\x{4e00}-\x{9fa5}]+/u',$str)){ echo '匹配成功,有中文字符串!';}else{ echo '没有中文字符串。';}// GB2312,GBK编码:正则表达式匹配中文;if(preg_match("/^[".chr(0xa1)."-".chr(0xff)."A-Za-z0-9_]+$/",$str)){ echo '匹配成功,有中文字符串!';}else{ echo '没有中文字符串。';} ?>你看看这样怎么样,建议你去后盾人看看,那里有教学视频