前一段时间我在Python中创建了一个脚本,它通过将所有字符与一个字符数组进行比较,将所有字符移动到右边。
Python代码:
def Cypher(self,StrMsg,StrCode,IntCypher):
self.StrNwMsg = ""
IntI = IntCypher
for IntO in range(0,len(StrMsg)):
IntI += 1
for IntL in range(0,len(StrCode)):
if StrMsg[IntO] == StrCode[IntL]:
if(IntL + IntI) < len(StrCode):
self.StrNwMsg += StrCode[IntL + IntI]
else:
while (IntL + IntI) >= len(StrCode):
IntI -= len(StrCode)
self.StrNwMsg += StrCode[IntL + IntI]
return self.StrNwMsg
PHP代码:
function Cyther($Msg,$StrCode,$IntCypther){
$NewMsg = "";
$I = $IntCypther;
for($O = 0; $O <= strlen($Msg)-1;$O++){
$I += 1;
for($L = 0; $L <= count($StrCode)-1;$L++){
if($Msg[$O] == $StrCode[$L]){
if(($L+$I) < (count($StrCode)-1)){
$NewMsg .= $StrCode[$L+$I];
}else{
while(($L + $I) >= (count($StrCode)-1)){
$I -= count($StrCode)-1;
}
$NewMsg .= $StrCode[$L+$I];
}
}
}
}
return $NewMsg;
}
但我想在PHP中存储密码,因为您可以将IntI更改为用户ID,因此存储的数据将始终具有相当好的隐藏密码。但PHP代码不起作用。这里是他们都使用的数组:(我删除了括号以消除任何混淆。数组是1维)
"A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z","a","b","c","d",
"e","f","g","h","i","j","k","l","m","n",
"o","p","q","r","s","t","u","v","w","x",
"y","z","1","2","3","4","5","6","7","8",
"9","0",".",",","'","?","!"," ","_","-"
好的,当我输入'Example'时,PHP和Pyhton脚本都返回'P0n152v'但是当我输入'0A'时,PHP返回'D'并出现此错误:
Undefined offset: -57 in /var/www/Test_Page/Encryption/Encrypt.php on line 25
错误是指代这行代码$NewMsg .= $StrCode[$L+$I];
,因为$L + $I
超出了$StrCode
数组的范围。但我在相同的Python代码中没有这个问题。
并且Python返回'CM',这是他们应该返回的。我现在已经重写了这个代码至少6次,我不知道为什么我会遇到这个问题。任何人都知道为什么我有这个问题。
只是为PHP和Python脚本添加IntCypther变量为10。
答案 0 :(得分:3)
I'm not sure how Python works, but you can't indefinitely add or subtract from the index of an array in PHP. Once you get to 0 and start subtracting more (assuming the indexes are zero-based and numerical), you're going to get an undefined offset error because that index doesn't exist. The same happens if you increase it more than the maximum index. You might want to do something like modulo to make sure that the value is always in the range of the indexes of the array.
I whipped up a quick PHP script that should do this:
function Offset($offset) {
$array = ["A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z","a","b","c","d",
"e","f","g","h","i","j","k","l","m","n",
"o","p","q","r","s","t","u","v","w","x",
"y","z","1","2","3","4","5","6","7","8",
"9","0",".",",","'","?","!","@","_","-"];
// Make sure the offset is a +/- value of the indexes of $array
// by taking the modulo
$offset = $offset % (count($array)-1) ;
// If it is a negative number, subtract it from the end instead
if ($offset < 0) {
$offset = count($array)-1 + $offset;
}
// Get the index from your array
return $array[$offset];
}
for ($i=-200; $i<200; $i++) {
echo "$i => ".(Offset($i))."<br>";
}
This outputs:
-200 => K
-199 => L
-198 => M
-197 => N
-196 => O
-195 => P
-194 => Q
-193 => R
-192 => S
-191 => T
-190 => U
-189 => V
-188 => W
-187 => X
-186 => Y
-185 => Z
-184 => a
-183 => b
-182 => c
-181 => d
-180 => e
-179 => f
-178 => g
-177 => h
-176 => i
-175 => j
-174 => k
-173 => l
-172 => m
-171 => n
-170 => o
-169 => p
-168 => q
-167 => r
-166 => s
-165 => t
-164 => u
-163 => v
-162 => w
-161 => x
-160 => y
-159 => z
-158 => 1
-157 => 2
-156 => 3
-155 => 4
-154 => 5
-153 => 6
-152 => 7
-151 => 8
-150 => 9
-149 => 0
-148 => .
-147 => ,
-146 => '
-145 => ?
-144 => !
-143 => @
-142 => _
-141 => -
-140 => A
-139 => B
-138 => C
-137 => D
-136 => E
-135 => F
-134 => G
-133 => H
-132 => I
-131 => J
-130 => K
-129 => L
-128 => M
-127 => N
-126 => O
-125 => P
-124 => Q
-123 => R
-122 => S
-121 => T
-120 => U
-119 => V
-118 => W
-117 => X
-116 => Y
-115 => Z
-114 => a
-113 => b
-112 => c
-111 => d
-110 => e
-109 => f
-108 => g
-107 => h
-106 => i
-105 => j
-104 => k
-103 => l
-102 => m
-101 => n
-100 => o
-99 => p
-98 => q
-97 => r
-96 => s
-95 => t
-94 => u
-93 => v
-92 => w
-91 => x
-90 => y
-89 => z
-88 => 1
-87 => 2
-86 => 3
-85 => 4
-84 => 5
-83 => 6
-82 => 7
-81 => 8
-80 => 9
-79 => 0
-78 => .
-77 => ,
-76 => '
-75 => ?
-74 => !
-73 => @
-72 => _
-71 => -
-70 => A
-69 => B
-68 => C
-67 => D
-66 => E
-65 => F
-64 => G
-63 => H
-62 => I
-61 => J
-60 => K
-59 => L
-58 => M
-57 => N
-56 => O
-55 => P
-54 => Q
-53 => R
-52 => S
-51 => T
-50 => U
-49 => V
-48 => W
-47 => X
-46 => Y
-45 => Z
-44 => a
-43 => b
-42 => c
-41 => d
-40 => e
-39 => f
-38 => g
-37 => h
-36 => i
-35 => j
-34 => k
-33 => l
-32 => m
-31 => n
-30 => o
-29 => p
-28 => q
-27 => r
-26 => s
-25 => t
-24 => u
-23 => v
-22 => w
-21 => x
-20 => y
-19 => z
-18 => 1
-17 => 2
-16 => 3
-15 => 4
-14 => 5
-13 => 6
-12 => 7
-11 => 8
-10 => 9
-9 => 0
-8 => .
-7 => ,
-6 => '
-5 => ?
-4 => !
-3 => @
-2 => _
-1 => -
0 => A
1 => B
2 => C
3 => D
4 => E
5 => F
6 => G
7 => H
8 => I
9 => J
10 => K
11 => L
12 => M
13 => N
14 => O
15 => P
16 => Q
17 => R
18 => S
19 => T
20 => U
21 => V
22 => W
23 => X
24 => Y
25 => Z
26 => a
27 => b
28 => c
29 => d
30 => e
31 => f
32 => g
33 => h
34 => i
35 => j
36 => k
37 => l
38 => m
39 => n
40 => o
41 => p
42 => q
43 => r
44 => s
45 => t
46 => u
47 => v
48 => w
49 => x
50 => y
51 => z
52 => 1
53 => 2
54 => 3
55 => 4
56 => 5
57 => 6
58 => 7
59 => 8
60 => 9
61 => 0
62 => .
63 => ,
64 => '
65 => ?
66 => !
67 => @
68 => _
69 => -
70 => A
71 => B
72 => C
73 => D
74 => E
75 => F
76 => G
77 => H
78 => I
79 => J
80 => K
81 => L
82 => M
83 => N
84 => O
85 => P
86 => Q
87 => R
88 => S
89 => T
90 => U
91 => V
92 => W
93 => X
94 => Y
95 => Z
96 => a
97 => b
98 => c
99 => d
100 => e
101 => f
102 => g
103 => h
104 => i
105 => j
106 => k
107 => l
108 => m
109 => n
110 => o
111 => p
112 => q
113 => r
114 => s
115 => t
116 => u
117 => v
118 => w
119 => x
120 => y
121 => z
122 => 1
123 => 2
124 => 3
125 => 4
126 => 5
127 => 6
128 => 7
129 => 8
130 => 9
131 => 0
132 => .
133 => ,
134 => '
135 => ?
136 => !
137 => @
138 => _
139 => -
140 => A
141 => B
142 => C
143 => D
144 => E
145 => F
146 => G
147 => H
148 => I
149 => J
150 => K
151 => L
152 => M
153 => N
154 => O
155 => P
156 => Q
157 => R
158 => S
159 => T
160 => U
161 => V
162 => W
163 => X
164 => Y
165 => Z
166 => a
167 => b
168 => c
169 => d
170 => e
171 => f
172 => g
173 => h
174 => i
175 => j
176 => k
177 => l
178 => m
179 => n
180 => o
181 => p
182 => q
183 => r
184 => s
185 => t
186 => u
187 => v
188 => w
189 => x
190 => y
191 => z
192 => 1
193 => 2
194 => 3
195 => 4
196 => 5
197 => 6
198 => 7
199 => 8
答案 1 :(得分:0)
Who ever commented, thank you. python when IntI
got into the negatives it just looped back through the array. so i just had to add some statements to catch if IntI had gone bellow 0 or above the length of the array.