PHP加密解密函数Authcode()

Authcode 这函数来自Discuz程序,用于加密解密字符串,可以设置钥匙(key)和过期时间, 原版的函数代码可能会生成+、/、&这样的字符,导致通过URL传值取回时被转义,导致无法解密。CODEGO.NET提供了一个办法:火端网络稍加修改,把这几个字符替换成其它字符,解密时再替换回去:
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0){     if($operation == 'DECODE') {  $string = str_replace('[a]','+',$string);  $string = str_replace('[b]','&',$string);  $string = str_replace('[c]','/',$string);  }  $ckey_length = 4;  $key = md5($key ? $key : 'livcmsencryption ');  $keya = md5(substr($key, 0, 16));  $keyb = md5(substr($key, 16, 16));  $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';  $cryptkey = $keya.md5($keya.$keyc);  $key_length = strlen($cryptkey);  $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;  $string_length = strlen($string);  $result = '';  $box = range(0, 255);  $rndkey = array();  for($i = 0; $i <= 255; $i++) {   $rndkey[$i] = ord($cryptkey[$i % $key_length]);  }  for($j = $i = 0; $i < 256; $i++) {   $j = ($j + $box[$i] + $rndkey[$i]) % 256;   $tmp = $box[$i];   $box[$i] = $box[$j];   $box[$j] = $tmp;  }  for($a = $j = $i = 0; $i < $string_length; $i++) {   $a = ($a + 1) % 256;   $j = ($j + $box[$a]) % 256;   $tmp = $box[$a];   $box[$a] = $box[$j];   $box[$j] = $tmp;   $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));  }  if($operation == 'DECODE') {   if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {       return substr($result, 26);   } else {    return '';   }  } else {  $ustr = $keyc.str_replace('=', '', base64_encode($result));  $ustr = str_replace('+','[a]',$ustr);  $ustr = str_replace('&','[b]',$ustr);  $ustr = str_replace('/','[c]',$ustr);   return $ustr;  } } 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30187653/viewspace-1509080/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30187653/viewspace-1509080/

Java中也有类似的加密解密算法可以实现类似PHPauthcode功能。常见的加密解密算法有AES、DES、RSA等。 以AES算法为例,可以使用Java内置的javax.crypto库中的AES算法实现加密解密。 加密示例代码: ``` import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; public class AESUtil { //默认密钥 private static final String DEFAULT_SECRET_KEY = "1234567890abcdef"; /** * AES加密 * @param content 待加密内容 * @param secretKey 密钥 * @return 加密后的内容 */ public static String encrypt(String content, String secretKey) { try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes("UTF-8"), "AES"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] encrypted = cipher.doFinal(content.getBytes("UTF-8")); return Base64.encodeBase64String(encrypted); } catch (Exception e) { e.printStackTrace(); } return null; } /** * AES加密,默认使用默认密钥 * @param content 待加密内容 * @return 加密后的内容 */ public static String encrypt(String content) { return encrypt(content, DEFAULT_SECRET_KEY); } } ``` 解密示例代码: ``` import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; public class AESUtil { //默认密钥 private static final String DEFAULT_SECRET_KEY = "1234567890abcdef"; /** * AES解密 * @param content 待解密内容 * @param secretKey 密钥 * @return 解密后的内容 */ public static String decrypt(String content, String secretKey) { try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes("UTF-8"), "AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] decrypted = cipher.doFinal(Base64.decodeBase64(content)); return new String(decrypted, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return null; } /** * AES解密,默认使用默认密钥 * @param content 待解密内容 * @return 解密后的内容 */ public static String decrypt(String content) { return decrypt(content, DEFAULT_SECRET_KEY); } } ``` 使用示例: ``` String content = "hello world"; String secretKey = "1234567890abcdef"; //加密 String encryptedContent = AESUtil.encrypt(content, secretKey); System.out.println("加密后的内容:" + encryptedContent); //解密 String decryptedContent = AESUtil.decrypt(encryptedContent, secretKey); System.out.println("解密后的内容:" + decryptedContent); ``` 输出: ``` 加密后的内容:wK0MwIxt8GQ4JUQXHcJ5Og== 解密后的内容:hello world ``` 需要注意的是,加密和解密需要使用相同的密钥才能得到正确的结果。因此在实际使用中,密钥需要进行安全保管,避免泄露。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值