當前位置:首頁 » 編程語言 » phpjava加密

phpjava加密

發布時間: 2023-01-17 07:26:04

php 如何實現 java的sha1加密

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 '沒有中文字元串。';} ?>你看看這樣怎麼樣,建議你去後盾人看看,那裡有教學視頻

熱點內容
銳龍本編程 發布:2025-07-16 19:35:08 瀏覽:643
初學c語言用什麼軟體 發布:2025-07-16 19:29:12 瀏覽:338
編譯原理實驗分析子程序 發布:2025-07-16 19:28:06 瀏覽:459
長江存儲宿舍有wifi嗎 發布:2025-07-16 19:20:45 瀏覽:872
sqlservertrigger 發布:2025-07-16 19:08:19 瀏覽:400
android中權重 發布:2025-07-16 19:07:26 瀏覽:422
lol界面在哪個文件夾 發布:2025-07-16 19:01:53 瀏覽:937
php文件解壓 發布:2025-07-16 19:01:08 瀏覽:884
日誌中心伺服器怎樣搭建 發布:2025-07-16 19:00:27 瀏覽:605
硬碟加密保護 發布:2025-07-16 18:58:52 瀏覽:40