自定义加密算法的实现
由于要传一个需要保密的ID,因此用到对称加密,但mcrypt_encrypt算法加密后字符串太长,因此想实现一个自定义加密算法,想法如下
首先先对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)
回复讨论(解决方案)
echo encrypt(1234, 'abc'), PHP_EOL;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
我自己也实现了加密过程,不过看起来就没那么优雅了,执行效率也低点,贴出来衬托下高手风范
$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
有没有什么好的办法打散下结果?
这个够乱的了吧
$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 12341235 oD2LN* 12351236 wqkf8u 12361237 6k=GVU 12371238 bxeCr* 12381239 =W-AOi 12391240 IiQ3e1 12401241 z6uMMA 12411242 WLcnd8 12421243 Rizj*M 12431244 4rHK47 12441245 oD2LNT 12451246 wqkf8Z 12461247 6k=GVJ 12471248 bxeCrE 12481249 =W-AOP 12491250 IiQ3et 12501251 z6uMMP 12511252 WLcndU 12521253 Rizj*p 12531254 4rHK4s 12541255 oD2LNs 12551256 wqkf84 12561257 6k=GVn 12571258 bxeCrL 12581259 =W-AOT 12591260 IiQ3ex 12601261 z6uMM1 12611262 WLcndD 12621263 Rizj*s 12631264 4rHK4h 12641265 oD2LNq 12651266 wqkf83 12661267 6k=GVg 12671268 bxeCr5 12681269 =W-AOH 12691270 IiQ3eP 12701271 z6uMMc 12711272 WLcndE 12721273 Rizj*6 12731274 4rHK4I 12741275 oD2LN= 12751276 wqkf8U 12761277 6k=GVI 12771278 bxeCr9 12781279 =W-AOl 12791280 IiQ3bI 12801281 z6uMhG 12811282 WLcnaY 12821283 Rizj6d 12831284 4rHK3Z 12841285 oD2L*n 12851286 wqkfbP 12861287 6k=Gzj 12871288 bxeC=o 12881289 =W-AEd 12891290 IiQ3bY 12901291 z6uMh* 12911292 WLcnag 12921293 Rizj6v 12931294 4rHK3F 12941295 oD2L*e 12951296 wqkfbJ 12961297 6k=Gzm 12971298 bxeC=N 12981299 =W-AEw 12991300 IiQ3bs 13001301 z6uMhl 13011302 WLcna4 13021303 Rizj6V 13031304 4rHK3u 13041305 oD2L*V 13051306 wqkfbm 13061307 6k=Gz* 13071308 bxeC=- 13081309 =W-AEa 13091310 IiQ3bm 13101311 z6uMhe 13111312 WLcnaS 13121313 Rizj6= 13131314 4rHK38 13141315 oD2L*l 13151316 wqkfbS 13161317 6k=Gz6 13171318 bxeC=q 13181319 =W-AEn 13191320 IiQ3bO 13201321 z6uMhV 13211322 WLcnau 13221323 Rizj61 13231324 4rHK3K 13241325 oD2L*p 13251326 wqkfbv 13261327 6k=Gzw 13271328 bxeC=h 13281329 =W-AE- 13291330 IiQ3bS 13301331 z6uMhj 13311332 WLcna9 1332
在你的设计中,第一节只有 2bit 有效位,所以可在其上再附加4bit信息
而0~15的随机数正好是4bit
算法中,这个随机数起到2个作用
1、调整 key
2、打乱字典
如果不怎么需要太强的保密性,位运算移位就足够了,省点CPU
楼主的解答超赞
加上随机数后,1个整数可对应16种结果,64^6种结果尽数用上,解密时也不用再判断是不是无效字符串了。
是楼主的解答超赞,打错啦
不打了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



An avatar on Netflix is a visual representation of your streaming identity. Users can go beyond the default avatar to express their personality. Continue reading this article to learn how to set a custom profile picture in the Netflix app. How to quickly set a custom avatar in Netflix In Netflix, there is no built-in feature to set a profile picture. However, you can do this by installing the Netflix extension on your browser. First, install a custom profile picture for the Netflix extension on your browser. You can buy it in the Chrome store. After installing the extension, open Netflix on your browser and log into your account. Navigate to your profile in the upper right corner and click

How to customize background image in Win11? In the newly released win11 system, there are many custom functions, but many friends do not know how to use these functions. Some friends think that the background image is relatively monotonous and want to customize the background image, but don’t know how to customize the background image. If you don’t know how to define the background image, the editor has compiled the steps to customize the background image in Win11 below. If you are interested If so, take a look below! Steps for customizing background images in Win11: 1. Click the win button on the desktop and click Settings in the pop-up menu, as shown in the figure. 2. Enter the settings menu and click Personalization, as shown in the figure. 3. Enter Personalization and click on Background, as shown in the picture. 4. Enter background settings and click to browse pictures

A Venn diagram is a diagram used to represent relationships between sets. To create a Venn diagram we will use matplotlib. Matplotlib is a commonly used data visualization library in Python for creating interactive charts and graphs. It is also used to create interactive images and charts. Matplotlib provides many functions to customize charts and graphs. In this tutorial, we will illustrate three examples to customize Venn diagrams. The Chinese translation of Example is: Example This is a simple example of creating the intersection of two Venn diagrams; first, we imported the necessary libraries and imported venns. Then we create the dataset as a Python set, after that we use the "venn2()" function to create

CakePHP is a powerful PHP framework that provides developers with many useful tools and features. One of them is pagination, which helps us divide large amounts of data into several pages, making browsing and manipulation easier. By default, CakePHP provides some basic pagination methods, but sometimes you may need to create some custom pagination methods. This article will show you how to create custom pagination in CakePHP. Step 1: Create a custom pagination class First, we need to create a custom pagination class. this

How to customize shortcut key settings in Eclipse? As a developer, mastering shortcut keys is one of the keys to improving efficiency when coding in Eclipse. As a powerful integrated development environment, Eclipse not only provides many default shortcut keys, but also allows users to customize them according to their own preferences. This article will introduce how to customize shortcut key settings in Eclipse and give specific code examples. Open Eclipse First, open Eclipse and enter

The iOS 17 update for iPhone brings some big changes to Apple Music. This includes collaborating with other users on playlists, initiating music playback from different devices when using CarPlay, and more. One of these new features is the ability to use crossfades in Apple Music. This will allow you to transition seamlessly between tracks, which is a great feature when listening to multiple tracks. Crossfading helps improve the overall listening experience, ensuring you don't get startled or dropped out of the experience when the track changes. So if you want to make the most of this new feature, here's how to use it on your iPhone. How to Enable and Customize Crossfade for Apple Music You Need the Latest

Vue is a popular JavaScript framework that provides many convenient functions and APIs to help developers build interactive front-end applications. With the release of Vue3, the render function has become an important update. This article will introduce the concept and purpose of the render function in Vue3 and how to use it to customize the rendering function. What is the render function? In Vue, template is the most commonly used rendering method, but in Vue3, you can use another method: r

How to implement custom middleware in CodeIgniter Introduction: In modern web development, middleware plays a vital role in applications. They can be used to perform some shared processing logic before or after the request reaches the controller. CodeIgniter, as a popular PHP framework, also supports the use of middleware. This article will introduce how to implement custom middleware in CodeIgniter and provide a simple code example. Middleware overview: Middleware is a kind of request
