本文目录一览:
1、如何将PHP转换成JAVA 2、PHP代码转为java代码 3、[怎么把php AES128的代码转成java](#怎么把php AES128的代码转成java) 4、请把一小段PHP代码换成一段的JAVA代码~非常感谢 5、php示例怎么转java? 6、PHP代码变成java代码
如何将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((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
}
请把一小段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存储元素,键值对方式。
Map<String, String> hashMap = new HashMap<String, 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结果。