千家信息网

php和go按位异结果有什么不同

发表于:2025-11-12 作者:千家信息网编辑
千家信息网最后更新 2025年11月12日,这篇文章主要讲解了"php和go按位异结果有什么不同",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"php和go按位异结果有什么不同"吧!PHP的按位异
千家信息网最后更新 2025年11月12日php和go按位异结果有什么不同

这篇文章主要讲解了"php和go按位异结果有什么不同",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"php和go按位异结果有什么不同"吧!

  • PHP的按位异或

echo "'Y'的ASCII值是:" . ord('Y') . PHP_EOL;echo "8的ASCII值是:" . ord(8) . PHP_EOL;echo "'8'的ASCII值是:" . ord('8') . PHP_EOL;echo "'Y' ^ 8 = " . ('Y' ^ 8) . PHP_EOL; // 8echo "'Y' + 8 = " . ('Y' + 8) . PHP_EOL; // 8echo "'Y' ^ '8' = " . ('Y' ^ '8') . PHP_EOL; // aecho "89 ^ 8 = " . (89 ^ 8) . PHP_EOL; // 81echo "'89' ^ 8 = " . ('89' ^ 8) . PHP_EOL; // 81
#解析// Y ascii 89   ^  '8' ascii 56// 89 ^ 8  = 1011001//         ^ 0001000//         = 1010001 = 64 + 16 + 4 + 1 = 81// 89 ^ 56 = 1011001//         ^ 0111000//         = 1100001 = 64 + 32 + 1 = 97 = a
#结果'Y'的ASCII值是:898的ASCII值是:56'8'的ASCII值是:56'Y' ^ 8 = 8'Y' + 8 = 8'Y' ^ '8' = a89 ^ 8 = 81'89' ^ 8 = 81
小结

php 非数字字符串和整型 运算 时,取值为0


  • Go的按位异或

func main() {    fmt.Println("'Y' ^ 8 =", 'Y' ^ 8)    fmt.Println("'Y' ^ '8' =", 'Y' ^ '8')    fmt.Println("89 ^ 8 =", 89 ^ 8)}
#结果'Y' ^ 8 = 81'Y' ^ '8' = 9789 ^ 8 = 81
小结

go相对来说还是比较严谨,将rune都转为了对应ascii码值进行运算


扩展 网络上常用的一种加密解密算法

PHP实现

// 加密函数function encryptOp($string){        $string = str_replace(' ', '+', $string);//2019-9-8 16:36:12 把空格替换为+号,+号在传递过程中变成了空格;    $encryptKey = md5(rand(0, 32000));  // 用于加密的key    $init = 0; // 初始化变量长度    $tmp = '';    $strLen = strlen($string); // 待加密字符串的长度    $encryptKeyLen = strlen($encryptKey); // 加密key的长度    for ($index = 0; $index < $strLen; $index++) {        $init = $init == $encryptKeyLen ? 0 : $init; // 如果 $init = $encryptKey 的长度, 则 $init 清零        // $tmp 字串在末尾增加两位, 其第一位内容为 $encryptKey 的第 $init 位,        // 第二位内容为 $string 的第 $index 位与 $encryptKey 的 $init 位取异或。然后 $init = $init + 1        $tmp .= $encryptKey[$init] . ($string[$index] ^ $encryptKey[$init++]);    }    // 返回结果,结果为 passportKeyOp() 函数返回值的 base65 编码结果    return base64_encode(passportKeyOp($tmp));}// 密匙处理函数function passportKeyOp($string){    $encrypt_key = 'abcdefghijkl';    $encryptKey = md5($encrypt_key); // 加密的key    $init = 0;    $tmp = '';    $len = strlen($string);    $encryptKeyLen = strlen($encryptKey);    for ($index = 0; $index < $len; $index++) {        $init = $init == $encryptKeyLen ? 0 : $init;        $tmp .= $string[$index] ^ $encryptKey[$init++];    }    return $tmp;}// 解密函数function decryptOp($string){    $string = passportKeyOp(base64_decode($string));    $tmp = '';    $len = strlen($string);    for ($index = 0; $index < $len; $index++) {        if (!isset($string[$index]) || !isset($string[$index + 1])) {            return false;        }        $tmp .= $string[$index] ^ $string[++$index];    }    return $tmp;}$id = "123456";$idStr = encryptOp($id);echo $idStr . PHP_EOL;echo decryptOp($idStr) . PHP_EOL;

GO实现

package mainimport (        "crypto/md5"        "encoding/base64"        "fmt"        "math/rand"        "strconv"        "strings"        "time")func Md5(str string) string {        if str == "" {                return ""        }        init := md5.New()        init.Write([]byte(str))        return fmt.Sprintf("%x", init.Sum(nil))}func MtRand(min, max int64) int64 {        r := rand.New(rand.NewSource(time.Now().UnixNano()))        return r.Int63n(max-min+1) + min}func encryptOp(str string) string {        encryptKey := strconv.Itoa(int(MtRand(0, 32000)))        init := 0        tmp := ""        strPlus := []rune(str)        strLen := len(strPlus)        encryptKeyPlus := []rune(encryptKey)        encryptKeyLen := len(encryptKeyPlus)        for index := 0; index < strLen; index++ {                if init == encryptKeyLen {                        init = 0                }                strInt := int(strPlus[index])                encryptKeyInt := int(encryptKeyPlus[init])                tmp += string(encryptKeyPlus[init]) + string(rune(strInt ^ encryptKeyInt))                init++        }        sign := passportKeyOp(tmp)        sEnc := base64.StdEncoding.EncodeToString([]byte(sign))        return sEnc}func passportKeyOp(str string) string {        key := "abcdefghijkl"        encryptKey := Md5(key) // 加密的key        init := 0        result := ""        strPlus := []rune(str)        strLen := len(strPlus)        encryptKeyPlus := []rune(encryptKey)        encryptKeyLen := len(encryptKeyPlus)        for index := 0; index < strLen; index++ {                if init == encryptKeyLen {                        init = 0                }                result += string(rune(int(strPlus[index]) ^ int(encryptKeyPlus[init])))                init++        }        return result}func decryptOp(str string) string {        // 把空格替换为+号,+号在传递过程中变成了空格        str = strings.ReplaceAll(str, " ", "+")        sDec, _ := base64.StdEncoding.DecodeString(str)        str = passportKeyOp(string(sDec))        result := ""        strPlus := []rune(str)        strLen := len(strPlus)        for index := 0; index < strLen; index++ {                result += string(rune(int(strPlus[index]) ^ int(strPlus[index+1])))                index++        }        return result}func main() {        id := "123456"        idStr := encryptOp(id)        fmt.Println("idStr = ", idStr)        fmt.Println("id = ", decryptOp(idStr))}

感谢各位的阅读,以上就是"php和go按位异结果有什么不同"的内容了,经过本文的学习后,相信大家对php和go按位异结果有什么不同这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

0