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