Implementation of custom encryption algorithms

Source: Internet
Author: User
Tags unpack
The implementation of custom encryption algorithms This post was last edited by feiniaoflyer from 2014-05-011:16:02. symmetric encryption is used to transmit a secret ID, however, the string encrypted by the mcrypt_encrypt algorithm is too long, so to implement a custom encryption algorithm, the idea is as follows: First, calculate the sha1 key and get the first 32bit of the result, then follow the implementation of the custom encryption algorithm to be encrypted

This post was last edited by feiniaoflyer at 13:16:02, January 1 ,.

Symmetric encryption is used to transmit an ID that requires confidentiality. However, the string encrypted by the mcrypt_encrypt algorithm is too long. therefore, to implement a custom encryption algorithm, the idea is as follows:

First, calculate the sha1 of the key, obtain the first 32bit of the result, and then perform the XOR operation with the integer to be encrypted to obtain an encrypted 32bit result.

Result Group: 2bit | 6bit | 6bit | 6bit | 6bit | 6bit

Each group is named a0, a1, a2, a3, a4, and a5 respectively.

Define a 64-bit dictionary array

$ Dict = array ('1', '2', '3', '4', '5', '6', '7', '8 ', '9 ',
'A', 'B', 'C', 'D', 'e', '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', '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 ');

When the values of each group are used as the subscript of the dictionary array, the encrypted result is: $ dict [a0]. $ dict [a1]. $ dict [a2]. $ dict [a3]. $ dict [a4]. $ dict [a5]

The encrypted result is a string of 6 to 12 characters. if the last three elements of the dictionary array are represented by other single characters, the result is a string of 6 characters.

I am not new to php and I am not familiar with the function library of php. please help me implement the encryption and decryption algorithm:

String encrypt (int id, string key)

Int decrypt (string text, string key)





------ Solution --------------------

------ Solution --------------------
Learning, amazing.
------ Solution --------------------
This is messy.
$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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.