d622d38a |
1 | <?php |
2 | |
3 | /* |
4 | * |
5 | * Original code from LexZEUS <lexzeus@mifinca.com> |
6 | * and josh@superfork.com (extracted from php manual) |
7 | * Adapted for MailFetch by Philippe Mingo <mingo@rotedic.com> |
8 | * |
9 | */ |
10 | |
11 | |
12 | function hex2bin( $data ) { |
13 | |
14 | /* Original code by josh@superfork.com */ |
15 | |
16 | $len = strlen($data); |
17 | for( $i=0; $i < $len; $i += 2 ) { |
18 | $newdata .= pack( "C", hexdec( substr( $data, $i, 2) ) ); |
19 | } |
20 | return $newdata; |
21 | } |
22 | |
23 | function mf_keyED( $txt ) { |
24 | |
25 | global $MF_TIT; |
26 | |
27 | if( !isset( $MF_TIT ) ) { |
28 | $MF_TIT = "MailFetch Secure for SquirrelMail 1.x"; |
29 | } |
30 | |
31 | $encrypt_key = md5( $MF_TIT ); |
32 | $ctr = 0; |
33 | $tmp = ""; |
34 | for( $i = 0; $i < strlen( $txt ); $i++ ) { |
35 | if( $ctr == strlen( $encrypt_key ) ) $ctr=0; |
36 | $tmp.= substr( $txt, $i, 1 ) ^ substr( $encrypt_key, $ctr, 1 ); |
37 | $ctr++; |
38 | } |
39 | return $tmp; |
40 | } |
41 | |
42 | function encrypt( $txt ) { |
43 | |
44 | srand( (double) microtime() * 1000000 ); |
45 | $encrypt_key = md5( rand( 0, 32000 ) ); |
46 | $ctr = 0; |
47 | $tmp = ""; |
48 | for( $i = 0; $i < strlen( $txt ); $i++ ) { |
49 | if ($ctr==strlen($encrypt_key)) $ctr=0; |
50 | $tmp.= substr($encrypt_key,$ctr,1) . |
51 | (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1)); |
52 | $ctr++; |
53 | } |
54 | return bin2hex( mf_keyED( $tmp ) ); |
55 | |
56 | } |
57 | |
58 | function decrypt( $txt ) { |
59 | |
60 | $txt = mf_keyED( hex2bin( $txt ) ); |
61 | $tmp = ''; |
62 | for ( $i=0; $i < strlen( $txt ); $i++ ) { |
63 | $md5 = substr( $txt, $i, 1 ); |
64 | $i++; |
65 | $tmp.= ( substr( $txt, $i, 1 ) ^ $md5 ); |
66 | } |
67 | return $tmp; |
68 | } |
69 | |
70 | ?> |