首先先对key计算sha1,取结果的前32bit,然后跟要加密整数进行异或,得到一个加密后的32bit结果
对结果分组:2bit | 6bit | 6bit | 6bit | 6bit | 6bit
各个组分别取名为:a0、a1、a2、a3、a4、a5
另定义一个长度64的字典数组
$dict=array('1','2','3','4','5','6','7','8','9',
'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',
'01','02','03');
将前面每个分组的值作为字典数组的下标,则加密结果为:$dict[a0].$dict[a1].$dict[a2].$dict[a3].$dict[a4].$dict[a5]
这样加密后的结果就是一个长度6-12的字符串,如果字典数组最后3个元素用其他单字符表示,那么结果就固定为6个字符的字符串。
由于初学php不久,对php的函数库不熟悉,求大侠帮忙实现下加密解密算法:
string encrypt(int id,string key)
int decrypt(string text,string key)
10 个解决方案
#1
echo encrypt(1234, 'abc'), PHP_EOL;1TgGSY
echo decrypt( '1TgGSY', 'abc');
function encrypt($id, $key) {
$dict = array('1','2','3','4','5','6','7','8','9',
'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',
'-','=','*'
);
$key = current(unpack('L', sha1($key, 1)));
$id ^= $key;
$t = str_split(sprintf('%036b', $id), 6);
foreach($t as &$v) $v = $dict[bindec($v)];
return join($t);
}
function decrypt($s, $key) {
$dict = array('1','2','3','4','5','6','7','8','9',
'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',
'-','=','*'
);
$dict = array_flip($dict);
foreach(str_split($s) as $c) $r[] = sprintf('%06b', $dict[$c]);
$id = bindec(join($r));
$key = current(unpack('L', sha1($key, 1)));
return $id ^ $key;
}
1234
#2
#3
学习了,厉害。
#4
我自己也实现了加密过程,不过看起来就没那么优雅了,执行效率也低点,贴出来衬托下高手风范
$key_string = 'abc';
function keyToInt($key) {
$key_sha1 = sha1 ( $key );
$first_char = $key_sha1 [0];
if (ord ( $first_char ) > 55) {
return hexdec ( (hexdec ( $first_char ) & 7) . substr ( $key_sha1, 1, 7 ) ) | (- 2147483648);
} else {
return hexdec ( $key_sha1 );
}
}
function Encrypt($num){
$dict = array('0','1','2','3','4','5','6','7','8','9','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','*','!');
$r1 = $num ^ keyToInt ( $key_string );
$r2 = decbin ( $r1 );
$r3 = array (
bindec ( substr ( $r2, 0, 2 ) ),
bindec ( substr ( $r2, 2, 6 ) ),
bindec ( substr ( $r2, 8, 6 ) ),
bindec ( substr ( $r2, 14, 6 ) ),
bindec ( substr ( $r2, 20, 6 ) ),
bindec ( substr ( $r2, 26, 6 ) )
);
return $dict [$r3 [0]] . $dict [$r3 [1]] . $dict [$r3 [2]] . $dict [$r3 [3]] . $dict [$r3 [4]] . $dict [$r3 [5]];
}
算法实现之后,发觉在设计算法时,有个缺陷没考虑到,
由于仅仅是id与key异或,加密后的结果存在一定规律性,
比如
1234->1TgGSY
1235->1TgGSX
有没有什么好的办法打散下结果?
#5
这个够乱的了吧
$id = 1234;
$key = 'aaa';
for($i=1; $i<100; $i++) {
printf("%-10d %s %s\n", $id, $s = encrypt($id++, $key), decrypt( $s, $key));
}
function encrypt($id, $key) {
$dict = array('1','2','3','4','5','6','7','8','9',
'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',
'-','=','*'
);
$n = rand(0, 15);
srand($n);
$key = current(unpack('L', substr(sha1($key, 1), $n)));
$id ^= $key;
$t = str_split(sprintf('%04b%032b', $n, $id), 6);
foreach($t as $i=>&$v) {
$v = $dict[bindec($v)];
if($i == 0) shuffle($dict);
}
return join($t);
}
function decrypt($s, $key) {
$dict = array('1','2','3','4','5','6','7','8','9',
'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',
'-','=','*'
);
$m = array_search($s{0}, $dict);
$n = $m >> 2;
srand($n);
shuffle($dict);
$dict = array_flip($dict);
foreach(str_split($s) as $i=>$c) {
$r[] = sprintf('%06b', $i==0 ? $m&0x03 : $dict[$c]);
}
$id = bindec(join($r));
$key = current(unpack('L', substr(sha1($key, 1), $n)));
return $id ^ $key;
}
1234 4rHK4B 1234
1235 oD2LN* 1235
1236 wqkf8u 1236
1237 6k=GVU 1237
1238 bxeCr* 1238
1239 =W-AOi 1239
1240 IiQ3e1 1240
1241 z6uMMA 1241
1242 WLcnd8 1242
1243 Rizj*M 1243
1244 4rHK47 1244
1245 oD2LNT 1245
1246 wqkf8Z 1246
1247 6k=GVJ 1247
1248 bxeCrE 1248
1249 =W-AOP 1249
1250 IiQ3et 1250
1251 z6uMMP 1251
1252 WLcndU 1252
1253 Rizj*p 1253
1254 4rHK4s 1254
1255 oD2LNs 1255
1256 wqkf84 1256
1257 6k=GVn 1257
1258 bxeCrL 1258
1259 =W-AOT 1259
1260 IiQ3ex 1260
1261 z6uMM1 1261
1262 WLcndD 1262
1263 Rizj*s 1263
1264 4rHK4h 1264
1265 oD2LNq 1265
1266 wqkf83 1266
1267 6k=GVg 1267
1268 bxeCr5 1268
1269 =W-AOH 1269
1270 IiQ3eP 1270
1271 z6uMMc 1271
1272 WLcndE 1272
1273 Rizj*6 1273
1274 4rHK4I 1274
1275 oD2LN= 1275
1276 wqkf8U 1276
1277 6k=GVI 1277
1278 bxeCr9 1278
1279 =W-AOl 1279
1280 IiQ3bI 1280
1281 z6uMhG 1281
1282 WLcnaY 1282
1283 Rizj6d 1283
1284 4rHK3Z 1284
1285 oD2L*n 1285
1286 wqkfbP 1286
1287 6k=Gzj 1287
1288 bxeC=o 1288
1289 =W-AEd 1289
1290 IiQ3bY 1290
1291 z6uMh* 1291
1292 WLcnag 1292
1293 Rizj6v 1293
1294 4rHK3F 1294
1295 oD2L*e 1295
1296 wqkfbJ 1296
1297 6k=Gzm 1297
1298 bxeC=N 1298
1299 =W-AEw 1299
1300 IiQ3bs 1300
1301 z6uMhl 1301
1302 WLcna4 1302
1303 Rizj6V 1303
1304 4rHK3u 1304
1305 oD2L*V 1305
1306 wqkfbm 1306
1307 6k=Gz* 1307
1308 bxeC=- 1308
1309 =W-AEa 1309
1310 IiQ3bm 1310
1311 z6uMhe 1311
1312 WLcnaS 1312
1313 Rizj6= 1313
1314 4rHK38 1314
1315 oD2L*l 1315
1316 wqkfbS 1316
1317 6k=Gz6 1317
1318 bxeC=q 1318
1319 =W-AEn 1319
1320 IiQ3bO 1320
1321 z6uMhV 1321
1322 WLcnau 1322
1323 Rizj61 1323
1324 4rHK3K 1324
1325 oD2L*p 1325
1326 wqkfbv 1326
1327 6k=Gzw 1327
1328 bxeC=h 1328
1329 =W-AE- 1329
1330 IiQ3bS 1330
1331 z6uMhj 1331
1332 WLcna9 1332
#6
在你的设计中,第一节只有 2bit 有效位,所以可在其上再附加4bit信息
而0~15的随机数正好是4bit
算法中,这个随机数起到2个作用
1、调整 key
2、打乱字典
而0~15的随机数正好是4bit
算法中,这个随机数起到2个作用
1、调整 key
2、打乱字典
#7
如果不怎么需要太强的保密性,位运算移位就足够了,省点CPU
#8
楼主的解答超赞
加上随机数后,1个整数可对应16种结果,64^6种结果尽数用上,解密时也不用再判断是不是无效字符串了。
加上随机数后,1个整数可对应16种结果,64^6种结果尽数用上,解密时也不用再判断是不是无效字符串了。
#9
是楼主的解答超赞,打错啦
#10
不打了
#1
echo encrypt(1234, 'abc'), PHP_EOL;1TgGSY
echo decrypt( '1TgGSY', 'abc');
function encrypt($id, $key) {
$dict = array('1','2','3','4','5','6','7','8','9',
'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',
'-','=','*'
);
$key = current(unpack('L', sha1($key, 1)));
$id ^= $key;
$t = str_split(sprintf('%036b', $id), 6);
foreach($t as &$v) $v = $dict[bindec($v)];
return join($t);
}
function decrypt($s, $key) {
$dict = array('1','2','3','4','5','6','7','8','9',
'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',
'-','=','*'
);
$dict = array_flip($dict);
foreach(str_split($s) as $c) $r[] = sprintf('%06b', $dict[$c]);
$id = bindec(join($r));
$key = current(unpack('L', sha1($key, 1)));
return $id ^ $key;
}
1234
#2
#3
学习了,厉害。
#4
我自己也实现了加密过程,不过看起来就没那么优雅了,执行效率也低点,贴出来衬托下高手风范
$key_string = 'abc';
function keyToInt($key) {
$key_sha1 = sha1 ( $key );
$first_char = $key_sha1 [0];
if (ord ( $first_char ) > 55) {
return hexdec ( (hexdec ( $first_char ) & 7) . substr ( $key_sha1, 1, 7 ) ) | (- 2147483648);
} else {
return hexdec ( $key_sha1 );
}
}
function Encrypt($num){
$dict = array('0','1','2','3','4','5','6','7','8','9','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','*','!');
$r1 = $num ^ keyToInt ( $key_string );
$r2 = decbin ( $r1 );
$r3 = array (
bindec ( substr ( $r2, 0, 2 ) ),
bindec ( substr ( $r2, 2, 6 ) ),
bindec ( substr ( $r2, 8, 6 ) ),
bindec ( substr ( $r2, 14, 6 ) ),
bindec ( substr ( $r2, 20, 6 ) ),
bindec ( substr ( $r2, 26, 6 ) )
);
return $dict [$r3 [0]] . $dict [$r3 [1]] . $dict [$r3 [2]] . $dict [$r3 [3]] . $dict [$r3 [4]] . $dict [$r3 [5]];
}
算法实现之后,发觉在设计算法时,有个缺陷没考虑到,
由于仅仅是id与key异或,加密后的结果存在一定规律性,
比如
1234->1TgGSY
1235->1TgGSX
有没有什么好的办法打散下结果?
#5
这个够乱的了吧
$id = 1234;
$key = 'aaa';
for($i=1; $i<100; $i++) {
printf("%-10d %s %s\n", $id, $s = encrypt($id++, $key), decrypt( $s, $key));
}
function encrypt($id, $key) {
$dict = array('1','2','3','4','5','6','7','8','9',
'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',
'-','=','*'
);
$n = rand(0, 15);
srand($n);
$key = current(unpack('L', substr(sha1($key, 1), $n)));
$id ^= $key;
$t = str_split(sprintf('%04b%032b', $n, $id), 6);
foreach($t as $i=>&$v) {
$v = $dict[bindec($v)];
if($i == 0) shuffle($dict);
}
return join($t);
}
function decrypt($s, $key) {
$dict = array('1','2','3','4','5','6','7','8','9',
'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',
'-','=','*'
);
$m = array_search($s{0}, $dict);
$n = $m >> 2;
srand($n);
shuffle($dict);
$dict = array_flip($dict);
foreach(str_split($s) as $i=>$c) {
$r[] = sprintf('%06b', $i==0 ? $m&0x03 : $dict[$c]);
}
$id = bindec(join($r));
$key = current(unpack('L', substr(sha1($key, 1), $n)));
return $id ^ $key;
}
1234 4rHK4B 1234
1235 oD2LN* 1235
1236 wqkf8u 1236
1237 6k=GVU 1237
1238 bxeCr* 1238
1239 =W-AOi 1239
1240 IiQ3e1 1240
1241 z6uMMA 1241
1242 WLcnd8 1242
1243 Rizj*M 1243
1244 4rHK47 1244
1245 oD2LNT 1245
1246 wqkf8Z 1246
1247 6k=GVJ 1247
1248 bxeCrE 1248
1249 =W-AOP 1249
1250 IiQ3et 1250
1251 z6uMMP 1251
1252 WLcndU 1252
1253 Rizj*p 1253
1254 4rHK4s 1254
1255 oD2LNs 1255
1256 wqkf84 1256
1257 6k=GVn 1257
1258 bxeCrL 1258
1259 =W-AOT 1259
1260 IiQ3ex 1260
1261 z6uMM1 1261
1262 WLcndD 1262
1263 Rizj*s 1263
1264 4rHK4h 1264
1265 oD2LNq 1265
1266 wqkf83 1266
1267 6k=GVg 1267
1268 bxeCr5 1268
1269 =W-AOH 1269
1270 IiQ3eP 1270
1271 z6uMMc 1271
1272 WLcndE 1272
1273 Rizj*6 1273
1274 4rHK4I 1274
1275 oD2LN= 1275
1276 wqkf8U 1276
1277 6k=GVI 1277
1278 bxeCr9 1278
1279 =W-AOl 1279
1280 IiQ3bI 1280
1281 z6uMhG 1281
1282 WLcnaY 1282
1283 Rizj6d 1283
1284 4rHK3Z 1284
1285 oD2L*n 1285
1286 wqkfbP 1286
1287 6k=Gzj 1287
1288 bxeC=o 1288
1289 =W-AEd 1289
1290 IiQ3bY 1290
1291 z6uMh* 1291
1292 WLcnag 1292
1293 Rizj6v 1293
1294 4rHK3F 1294
1295 oD2L*e 1295
1296 wqkfbJ 1296
1297 6k=Gzm 1297
1298 bxeC=N 1298
1299 =W-AEw 1299
1300 IiQ3bs 1300
1301 z6uMhl 1301
1302 WLcna4 1302
1303 Rizj6V 1303
1304 4rHK3u 1304
1305 oD2L*V 1305
1306 wqkfbm 1306
1307 6k=Gz* 1307
1308 bxeC=- 1308
1309 =W-AEa 1309
1310 IiQ3bm 1310
1311 z6uMhe 1311
1312 WLcnaS 1312
1313 Rizj6= 1313
1314 4rHK38 1314
1315 oD2L*l 1315
1316 wqkfbS 1316
1317 6k=Gz6 1317
1318 bxeC=q 1318
1319 =W-AEn 1319
1320 IiQ3bO 1320
1321 z6uMhV 1321
1322 WLcnau 1322
1323 Rizj61 1323
1324 4rHK3K 1324
1325 oD2L*p 1325
1326 wqkfbv 1326
1327 6k=Gzw 1327
1328 bxeC=h 1328
1329 =W-AE- 1329
1330 IiQ3bS 1330
1331 z6uMhj 1331
1332 WLcna9 1332
#6
在你的设计中,第一节只有 2bit 有效位,所以可在其上再附加4bit信息
而0~15的随机数正好是4bit
算法中,这个随机数起到2个作用
1、调整 key
2、打乱字典
而0~15的随机数正好是4bit
算法中,这个随机数起到2个作用
1、调整 key
2、打乱字典
#7
如果不怎么需要太强的保密性,位运算移位就足够了,省点CPU
#8
楼主的解答超赞
加上随机数后,1个整数可对应16种结果,64^6种结果尽数用上,解密时也不用再判断是不是无效字符串了。
加上随机数后,1个整数可对应16种结果,64^6种结果尽数用上,解密时也不用再判断是不是无效字符串了。
#9
是楼主的解答超赞,打错啦
#10
不打了