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按位异结果有什么不同这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
结果
加密
不同
内容
函数
空格
长度
学习
字符
字符串
小结
过程
运算
严谨
加密解密
变量
就是
常用
思路
情况
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网易mcpvp服务器有什么
嘟嘟网络技术单位联系人
如何了解一个数据库
工行软件开发中心优势
哈利波特非官方版服务器互通吗
omv代理服务器
郑州中原总部和网络安全
腾讯网络安全防控要求
湛江oa软件开发哪家好
厦门普菲克网络技术有限公司
会上强调网络安全重要
天猫精灵显示服务器竟然出错了
软件开发领域是什么意思
软件开发新报价图片欣赏
网络安全一年级教案
获取本地缓存数据库
服务器开机启动正常显示无网络
教育信息化与网络安全调研
珠海租房网络安全
网络安全动态展示
web数据库技术及应用论文
企业查询软件开发
网络安全手抄报一等奖字又少
涪陵区工商软件开发流程参考价
销售软件开发中心
管理系统软件开发排行榜
网络安全生态峰会
北京软件开发人员平均薪资
RFID软件开发实例
服务器管理dns