php代碼轉換為java,php進行編碼轉換的函數

本文目錄一覽:

如何將PHP轉換成JAVA

先了解PHP的基本語言結構,然後去嘗試讀懂PHP項目的代碼,然後就按着代碼功能,用JAVA語言重寫一遍就是了,暫不知道有直接從PHP代碼轉成JAVA的工具。。。

PHP代碼轉為java代碼

沒法轉的,這個php中調用了不少外部對象,沒人能猜到那些是什麼內容的。

怎麼把php AES128的代碼轉成java

public class SimpleCrypto { 

        public static String encrypt(String seed, String cleartext) throws Exception { 

                byte[] rawKey = getRawKey(seed.getBytes()); 

                byte[] result = encrypt(rawKey, cleartext.getBytes()); 

                return toHex(result); 

        } 

        

        public static String decrypt(String seed, String encrypted) throws Exception { 

                byte[] rawKey = getRawKey(seed.getBytes()); 

                byte[] enc = toByte(encrypted); 

                byte[] result = decrypt(rawKey, enc); 

                return new String(result); 

        } 

        private static byte[] getRawKey(byte[] seed) throws Exception { 

                KeyGenerator kgen = KeyGenerator.getInstance(“AES”); 

                SecureRandom sr = SecureRandom.getInstance(“SHA1PRNG”); 

                sr.setSeed(seed); 

            kgen.init(128, sr); // 192 and 256 bits may not be available 

            SecretKey skey = kgen.generateKey(); 

            byte[] raw = skey.getEncoded(); 

            return raw; 

        } 

        

        private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception { 

            SecretKeySpec skeySpec = new SecretKeySpec(raw, “AES”); 

                Cipher cipher = Cipher.getInstance(“AES”); 

            cipher.init(Cipher.ENCRYPT_MODE, skeySpec); 

            byte[] encrypted = cipher.doFinal(clear); 

                return encrypted; 

        } 

        private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { 

            SecretKeySpec skeySpec = new SecretKeySpec(raw, “AES”); 

                Cipher cipher = Cipher.getInstance(“AES”); 

            cipher.init(Cipher.DECRYPT_MODE, skeySpec); 

            byte[] decrypted = cipher.doFinal(encrypted); 

                return decrypted; 

        } 

        public static String toHex(String txt) { 

                return toHex(txt.getBytes()); 

        } 

        public static String fromHex(String hex) { 

                return new String(toByte(hex)); 

        } 

        

        public static byte[] toByte(String hexString) { 

                int len = hexString.length()/2; 

                byte[] result = new byte[len]; 

                for (int i = 0; i  len; i++) 

                        result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue(); 

                return result; 

        } 

        public static String toHex(byte[] buf) { 

                if (buf == null) 

                        return “”; 

                StringBuffer result = new StringBuffer(2*buf.length); 

                for (int i = 0; i  buf.length; i++) { 

                        appendHex(result, buf[i]); 

                } 

                return result.toString(); 

        } 

        private final static String HEX = “0123456789ABCDEF”; 

        private static void appendHex(StringBuffer sb, byte b) { 

                sb.append(HEX.charAt((b4)0x0f)).append(HEX.charAt(b0x0f)); 

        } 

        

}

請把一小段PHP代碼換成一段的JAVA代碼~非常感謝

你這不好單獨翻  調用到太多其它方法了 , 打注釋的地方根據php函數的功能 改成java的就行了

private static boolean check_password_db(String nickname,String password){

String pwd=mysql_query(“select password from users where username=”+nickname+””); //mysql_query

String sha_info;

if(mysql_num_rows(pwd)==1){   //mysql_num_rows(pwd)  php的函數

String password_info=mysql_fetch_array(pwd); //mysql_fetch_array(pwd); 

sha_info=explode(“$”,password_info[0]); //explode(“$”,password_info[0]);

}else{

return false;

}

if(sha_info[1]==”SHA”){

String salf=sha_info[2];

String sha256_password=hash(“sha256”,password); //hash();

sha256_password+=sha_info[2];

if(strcasecmp(trim(sha_info[3]),hash(“sha256”,sha256_password))==0){ //strcasecmp

return true;

}else{

return false;

}

}

}

php示例怎麼轉java?

/**

* 生成簽名

* @param string timestamp 時間戳

* @param string appSecret 合作商開發者密鑰

* @param string nonce 隨機字符串

* @return string

*/

public String makeSignature (String timestamp,String appSecret,String nonce) {

String[] tmpArr = {timestamp, nonce, appSecret};

// 按值升序排序

Arrays.sort(tmpArr)

// 數組拼接為字符串

// 調用md5方法

return signature;

}

其他的都是方法調用, 根據需要編寫就行

PHP代碼變成java代碼

php代碼沒幾行,信息量很大,翻譯成java代碼行數量比較大。僅提供思路和php代碼解釋。

—————

?php 

$appid = “123”; //數組裏面的值,id。

$apikey = “456”; //數組裏面的值,為加密密鑰。

$secretKey =”789″; //數組裏面的值,安全密鑰。

$timestamp = time(); ////數組裏面的值,獲得當前時間。

//UNIX 時間戳(timestamp)是 PHP 中關於時間日期一個很重要的概念,它表示從 1970年1月1日 00:00:00 到當前時間的秒數之和。

//echo輸出$timestamp變量值,例如輸出了1389379960

echo $timestamp;  

//定義數組。以鍵值對方式存儲。

//’appid’ ‘apikey’ ‘secretkey’ ‘timestamp’是key,鍵。

//$appid $apikey, $secretKey $timestamp是value,值。

$params = array(‘appid’=$appid, ‘apikey’=$apikey, ‘secretkey’=$secretKey, ‘timestamp’=$timestamp);

//對數組鍵值進行升序排序。排序結果為apikey appid secretkey timestamp

ksort($params);

//拼接數組中的參數,並且用encoded編碼。

//http_build_query — 生成 url-encoded 之後的請求字符串。當數組沒有寫下標時,就會用第二個參數結合當前默認下標當前綴。

//$param_uri變量值,結果為apikey=456appid=123secretkey=789×tamp=1389379498

$param_uri = http_build_query($params,”,”);

echo $param_uri;   //echo輸出結果為apikey=456appid=123secretkey=789×tamp=1389379498

//先使用調用hash_hmac方法加密,HMAC-SHA1算法。

//$secretKey為安全密鑰,$param_uri為要加密的明文。’sha1’是HMAC-SHA1算法。

//再調用base64_encode方法加密,base64_encode 使用 MIME base64 對數據進行編碼。

$sig = base64_encode(hash_hmac(‘sha1’, $param_uri, $secretKey));

?

java:

1、用hashmap存儲元素,鍵值對方式。

MapString, String hashMap = new HashMapString, String(){

            {

            put(“appid”, “123”);

            put(“apikey”, “456”);

    put(“secretKey”, “789”);

    put(“timestamp”, “當前UNIX 時間戳,秒數,java中獲取”);

            }            

};

2、java中可以通過Timestamp獲得UNIX 時間戳。

3、然後對hashmap進行升序排序。

4、然後寫一個方法遍歷hashmap,拼接成字符串格式為apikey=456appid=123secretkey=789timestamp=1389379498

然後對該字符串進行encoded編碼,輸出格式為apikey=456appid=123secretkey=789×tamp=1389379498

5、通過java中HMAC-SHA1算法加密該字符串,$secretKey為安全密鑰。

6、再通過base64_encode加密第5步產生的字符串。這是最終sig結果。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/192141.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-30 15:15
下一篇 2024-11-30 15:15

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python字符串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字符串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字符串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python中capitalize函數的使用

    在Python的字符串操作中,capitalize函數常常被用到,這個函數可以使字符串中的第一個單詞首字母大寫,其餘字母小寫。在本文中,我們將從以下幾個方面對capitalize函…

    編程 2025-04-29
  • PHP和Python哪個好找工作?

    PHP和Python都是非常流行的編程語言,它們被廣泛應用於不同領域的開發中。但是,在考慮擇業方向的時候,很多人都會有一個問題:PHP和Python哪個好找工作?這篇文章將從多個方…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變量和數…

    編程 2025-04-29
  • Python中set函數的作用

    Python中set函數是一個有用的數據類型,可以被用於許多編程場景中。在這篇文章中,我們將學習Python中set函數的多個方面,從而深入了解這個函數在Python中的用途。 一…

    編程 2025-04-29
  • 單片機打印函數

    單片機打印是指通過串口或並口將一些數據打印到終端設備上。在單片機應用中,打印非常重要。正確的打印數據可以讓我們知道單片機運行的狀態,方便我們進行調試;錯誤的打印數據可以幫助我們快速…

    編程 2025-04-29
  • 三角函數用英語怎麼說

    三角函數,即三角比函數,是指在一個銳角三角形中某一角的對邊、鄰邊之比。在數學中,三角函數包括正弦、餘弦、正切等,它們在數學、物理、工程和計算機等領域都得到了廣泛的應用。 一、正弦函…

    編程 2025-04-29
  • Python3定義函數參數類型

    Python是一門動態類型語言,不需要在定義變量時顯示的指定變量類型,但是Python3中提供了函數參數類型的聲明功能,在函數定義時明確定義參數類型。在函數的形參後面加上冒號(:)…

    編程 2025-04-29

發表回復

登錄後才能評論