我让php脚本与api进行交互,它是为android设计的开发人员。但是这个Api的开发者他没有发布它只有andriod应用程序示例..所以我正在完成一个任务,转换这个java与PHP一起工作。对我来说一切都很容易,因为它主要是发布和解析json ...我只有密码编码的问题,我无法通过php制作。我只尝试使用base64_encode但是如果我尝试的话就没用了。这是java代码。我的java非常小,我需要翻译为php
import android.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
public final class c
{
private Cipher a;
private Cipher b;
private final byte c[] = {
-87, -101, -56, 50, 86, 53, -29, 3
};
public c(String s)
{
PBEKeySpec pbekeyspec = new PBEKeySpec(s.toCharArray(), c, 20);
try
{
SecretKey secretkey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(pbekeyspec);
a = Cipher.getInstance(secretkey.getAlgorithm());
b = Cipher.getInstance(secretkey.getAlgorithm());
PBEParameterSpec pbeparameterspec = new PBEParameterSpec(c, 20);
a.init(1, secretkey, pbeparameterspec);
b.init(2, secretkey, pbeparameterspec);
return;
}
catch (Exception exception)
{
return;
}
}
public final String a(String s)
{
String s1;
try
{
s1 = Base64.encodeToString(a.doFinal(s.getBytes()), 0);
}
catch (Exception exception)
{
return null;
}
return s1;
}
}
我很感兴趣了解public final String a(String s)
的这个函数答案 0 :(得分:0)
用于解码base64:
<?php
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
?>
编码:
<?php
$str = 'This is an encoded string';
echo base64_encode($str);
?>
参考:
http://php.net/manual/en/function.base64-encode.php
http://php.net/manual/en/function.base64-decode.php