导航:首页 > 编程语言 > phpchrjava

phpchrjava

发布时间:2022-07-09 07:22:25

A. java getBytes等同于php的什么

<?php

/**

* byte数组与字符串转化类

*/

class Bytes {

/**

* 转换一个String字符串为byte数组

* @param $str 需要转换的字符串

* @param $bytes 目标byte数组

* @author Zikie

*/
public static function getBytes($string) {
$bytes = array();
for($i = 0; $i < strlen($string); $i++){
$bytes[] = ord($string[$i]);
}
return $bytes;
}

/**

* 将字节数组转化为String类型的数据

* @param $bytes 字节数组

* @param $str 目标字符串

* @return 一个String类型的数据

*/

public static function toStr($bytes) {
$str = '';
foreach($bytes as $ch) {
$str .= chr($ch);
}

return $str;
}

/**

* 转换一个int为byte数组

* @param $byt 目标byte数组

* @param $val 需要转换的字符串

*

*/

public static function integerToBytes($val) {
$byt = array();
$byt[0] = ($val & 0xff);
$byt[1] = ($val >> 8 & 0xff);
$byt[2] = ($val >> 16 & 0xff);
$byt[3] = ($val >> 24 & 0xff);
return $byt;
}

/**

* 从字节数组中指定的位置读取一个Integer类型的数据

* @param $bytes 字节数组

* @param $position 指定的开始位置

* @return 一个Integer类型的数据

*/

public static function bytesToInteger($bytes, $position) {
$val = 0;
$val = $bytes[$position + 3] & 0xff;
$val <<= 8;
$val |= $bytes[$position + 2] & 0xff;
$val <<= 8;
$val |= $bytes[$position + 1] & 0xff;
$val <<= 8;
$val |= $bytes[$position] & 0xff;
return $val;
}

/**

* 转换一个shor字符串为byte数组

* @param $byt 目标byte数组

* @param $val 需要转换的字符串

*

*/

public static function shortToBytes($val) {
$byt = array();
$byt[0] = ($val & 0xff);
$byt[1] = ($val >> 8 & 0xff);
return $byt;
}

/**

* 从字节数组中指定的位置读取一个Short类型的数据。

* @param $bytes 字节数组

* @param $position 指定的开始位置

* @return 一个Short类型的数据

*/

public static function bytesToShort($bytes, $position) {
$val = 0;
$val = $bytes[$position + 1] & 0xFF;
$val = $val << 8;
$val |= $bytes[$position] & 0xFF;
return $val;
}

}

B. 关于php的crypt加密转换成java算法

JAVA 本身有MD5 。。。。。。。。。。。。。

C. Java的异或与PHP的异或,急求解!!!

是1,0,报错,推出。
相邻异或,得到一个7位数。试试:看满意么。

import java.util.Scanner;

class Test {

public static void main(String[] args){

int arr[] = new int[8];
Scanner sc = new Scanner(System.in);
System.out.println("please input the binary number");
String s = sc.next();
if(s.length()==8){

for(int i=0;i<8;i++){
char c = s.charAt(i);
if(c == '1'|| c== '0'){
arr[i] = (int)c;
}else{
System.out.println("the number is wrong");
System.exit(0);

}

}
}else {
System.out.println("the number's length is not 8");
System.exit(0);
}

for(int j=0;j<arr.length-1;j++) {
int k = (arr[j])^(arr[j+1]);
System.out.print(k);
}
}
}

D. 怎么把php AES128的代码转成java

publicclassSimpleCrypto{

publicstaticStringencrypt(Stringseed,Stringcleartext)throwsException{
byte[]rawKey=getRawKey(seed.getBytes());
byte[]result=encrypt(rawKey,cleartext.getBytes());
returntoHex(result);
}

publicstaticStringdecrypt(Stringseed,Stringencrypted)throwsException{
byte[]rawKey=getRawKey(seed.getBytes());
byte[]enc=toByte(encrypted);
byte[]result=decrypt(rawKey,enc);
returnnewString(result);
}

privatestaticbyte[]getRawKey(byte[]seed)throwsException{
KeyGeneratorkgen=KeyGenerator.getInstance("AES");
SecureRandomsr=SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128,sr);//
SecretKeyskey=kgen.generateKey();
byte[]raw=skey.getEncoded();
returnraw;
}


privatestaticbyte[]encrypt(byte[]raw,byte[]clear)throwsException{
SecretKeySpecskeySpec=newSecretKeySpec(raw,"AES");
Ciphercipher=Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
byte[]encrypted=cipher.doFinal(clear);
returnencrypted;
}

privatestaticbyte[]decrypt(byte[]raw,byte[]encrypted)throwsException{
SecretKeySpecskeySpec=newSecretKeySpec(raw,"AES");
Ciphercipher=Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE,skeySpec);
byte[]decrypted=cipher.doFinal(encrypted);
returndecrypted;
}

publicstaticStringtoHex(Stringtxt){
returntoHex(txt.getBytes());
}
publicstaticStringfromHex(Stringhex){
returnnewString(toByte(hex));
}

publicstaticbyte[]toByte(StringhexString){
intlen=hexString.length()/2;
byte[]result=newbyte[len];
for(inti=0;i<len;i++)
result[i]=Integer.valueOf(hexString.substring(2*i,2*i+2),16).byteValue();
returnresult;
}

publicstaticStringtoHex(byte[]buf){
if(buf==null)
return"";
StringBufferresult=newStringBuffer(2*buf.length);
for(inti=0;i<buf.length;i++){
appendHex(result,buf[i]);
}
returnresult.toString();
}
privatefinalstaticStringHEX="0123456789ABCDEF";
privatestaticvoidappendHex(StringBuffersb,byteb){
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}

}

E. 如何让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 '没有中文字符串。';} ?>你看看这样怎么样,建议你去后盾人看看,那里有教学视频

阅读全文

与phpchrjava相关的资料

热点内容
自己购买云主服务器推荐 浏览:422
个人所得税java 浏览:761
多余的服务器滑道还有什么用 浏览:192
pdf劈开合并 浏览:28
不能修改的pdf 浏览:752
同城公众源码 浏览:489
一个服务器2个端口怎么映射 浏览:298
java字符串ascii码 浏览:79
台湾云服务器怎么租服务器 浏览:475
旅游手机网站源码 浏览:332
android关联表 浏览:946
安卓导航无声音怎么维修 浏览:333
app怎么装视频 浏览:431
安卓系统下的软件怎么移到桌面 浏览:96
windows拷贝到linux 浏览:772
mdr软件解压和别人不一样 浏览:904
单片机串行通信有什么好处 浏览:340
游戏开发程序员书籍 浏览:860
pdf中图片修改 浏览:288
汇编编译后 浏览:491