存档

作者存档

Java版Discuz! AuthCode

2010年3月9日 CanleiSky 3 条评论

package discuz;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Random;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AuthCode {
    static BASE64Encoder encoder = new BASE64Encoder();
    static BASE64Decoder decoder = new BASE64Decoder();
    public enum DiscuzAuthcodeMode {
        Encode, Decode
    };

    public static void main(String[] args){
        System.out.print(”["+authcodeDecode("672d+9y5HQ0zqsONrBpSv5HwfTBASYjqVPyQ4O3RMN7xQcg", "soonyo")+"]“);
    }

    public static String CutString(String str, int startIndex, int length){
        if (startIndex >= 0) {
            if (length < 0) {
                length = length * -1;
                if(startIndex – length < 0) {
                    length = startIndex;
                    startIndex = 0;
                } else {
                    startIndex = startIndex – length;
                }
            }

            if (startIndex > str.length()) {
                return “”;
            }

        } else {
            if (length < 0) {
                return “”;
            } else {
                if (length + startIndex > 0) {
                    length = length + startIndex;
                    startIndex = 0;
                } else {
                    return “”;
                }
            }
        }

        if (str.length() – startIndex < length) {

            length = str.length() – startIndex;
        }

        return str.substring(startIndex, startIndex + length);
    }

    public static String CutString(String str, int startIndex) {
        return CutString(str, startIndex, str.length());
    }

    public static String MD5(String pass) {
        byte[] defaultBytes = pass.getBytes();
        try{
             MessageDigest algorithm = MessageDigest.getInstance(”MD5″);
             algorithm.reset();
             algorithm.update(defaultBytes);
             byte messageDigest[] = algorithm.digest();

             StringBuffer hexString = new StringBuffer();
             for (int i=0;i<messageDigest.length;i++) {
            String hex = Integer.toHexString(0xFF & messageDigest[i]);
            if(hex.length()==1)
            hexString.append(”0″ );

            hexString.append(hex);
             }
             return hexString.toString();
        }
        catch(NoSuchAlgorithmException nsae){
        }

        return “”;
    }

    public static boolean StrIsNullOrEmpty(String str) {
        //#if NET1
        if (str == null || str.trim().equals(”")) {
            return true;
        }

        return false;
    }

    static private byte[] GetKey(byte[] pass, int kLen) {
        byte[] mBox = new byte[kLen];

        for (int i = 0; i < kLen; i++) {
            mBox[i] = (byte) i;
        }

        int j = 0;
        for (int i = 0; i < kLen; i++) {

            j = (j + (int) ((mBox[i] + 256) % 256) + pass[i % pass.length])
                    % kLen;

            byte temp = mBox[i];
            mBox[i] = mBox[j];
            mBox[j] = temp;
        }

        return mBox;
    }

    public static String RandomString(int lens) {
        String[] CharArray = {”a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “j”, “k”, “l”, “m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”, “w”, “x”, “y”, “z”, “0″ , “1″ , “2″ , “3″ , “4″ , “5″ , “6″ , “7″ , “8″ , “9″ };
        int clens = CharArray.length;
        String sCode = “”;
        Random random = new Random();
        for (int i = 0; i < lens; i++) {
            sCode += CharArray[Math.abs(random.nextInt(clens))];
        }
        return sCode;
    }

    public static String authcodeEncode(String source, String key, int expiry) {
        return authcode(source, key, DiscuzAuthcodeMode.Encode, expiry);

    }

    public static String authcodeEncode(String source, String key) {
        return authcode(source, key, DiscuzAuthcodeMode.Encode, 0);

    }

    public static String authcodeDecode(String source, String key) {
        return authcode(source, key, DiscuzAuthcodeMode.Decode, 0);

    }

    private static String authcode(String source, String key,
            DiscuzAuthcodeMode operation, int expiry) {
        try {
            if (source == null || key == null) {
                return “”;
            }

            int ckey_length = 4;
            String keya, keyb, keyc, cryptkey, result;

            key = MD5(key);

            keya = MD5(CutString(key, 0, 16));

            keyb = MD5(CutString(key, 16, 16));

            keyc = ckey_length > 0 ? (operation == DiscuzAuthcodeMode.Decode ? CutString(
                    source, 0, ckey_length)
                    : RandomString(ckey_length))
                    : “”;

            cryptkey = keya + MD5(keya + keyc);

            if (operation == DiscuzAuthcodeMode.Decode) {
                byte[] temp;
                temp = decoder.decodeBuffer(CutString(source, ckey_length));
                result = new String(RC4(temp, cryptkey));
                if (CutString(result, 10, 16).equals(CutString(MD5(CutString(result, 26) + keyb), 0, 16))) {
                    return CutString(result, 26);
                } else {
                    temp = decoder.decodeBuffer(CutString(source+”=”, ckey_length));
                    result = new String(RC4(temp, cryptkey));
                    if (CutString(result, 10, 16).equals(CutString(MD5(CutString(result, 26) + keyb), 0, 16))) {
                        return CutString(result, 26);
                    } else {
                        temp = decoder.decodeBuffer(CutString(source+”==”, ckey_length));
                        result = new String(RC4(temp, cryptkey));
                        if (CutString(result, 10, 16).equals(CutString(MD5(CutString(result, 26) + keyb), 0, 16))) {
                            return CutString(result, 26);
                        }else{
                            return “”;
                        }
                    }
                }
            } else {
                source = “0000000000″ + CutString(MD5(source + keyb), 0, 16)
                        + source;

                byte[] temp = RC4(source.getBytes(”GBK”), cryptkey);

                return keyc + encoder.encodeBuffer(temp);

            }
        } catch (Exception e) {
            return “”;
        }

    }

    private static byte[] RC4(byte[] input, String pass) {
        if (input == null || pass == null)
            return null;

        byte[] output = new byte[input.length];
        byte[] mBox = GetKey(pass.getBytes(), 256);

        int i = 0;
        int j = 0;

        for (int offset = 0; offset < input.length; offset++) {
            i = (i + 1) % mBox.length;
            j = (j + (int) ((mBox[i] + 256) % 256)) % mBox.length;

            byte temp = mBox[i];
            mBox[i] = mBox[j];
            mBox[j] = temp;
            byte a = input[offset];

            byte b = mBox[(toInt(mBox[i]) + toInt(mBox[j])) % mBox.length];

            output[offset] = (byte) ((int) a ^ (int) toInt(b));
        }

        return output;
    }

    public static int toInt(byte b) {
        return (int) ((b + 256) % 256);
    }

    public long getUnixTimestamp() {
        Calendar cal = Calendar.getInstance();
        return cal.getTimeInMillis() / 1000;
    }

}

记录一下,挺实有的函数,加解密从方式上看貌似与PHP版不同,但结果出来是一样滴~

分类: Java 标签:

ORACLE事务机制,附PHP+ORACLE操作实例

2009年12月23日 CanleiSky 没有评论

        熟悉SQL SERVER的话,肯定常用到BEGIN TRAN和COMMIT TRAN语句。但在ORACLE中并没有这种类似的事务控制语句,ORACLE当然有事务,不然也不会是最牛X的数据库服务器软件了。下面简单的介绍下ORACLE的事务机制。

        在ORACLE中采用隐式事务,既然是隐式事务,显然是不需要COMMIT TRAN语句了,但COMMIT还是要的。

        在默认情况下,ORACLE对每次查询中对表的修改都是有隐式事务的,直到执行到COMMIT;或ROLLBACK;时提交或回滚事务。

        下面附上一段在PHP中一条语句操作多个表的解决办法。OCI_DEFAULT模式将默认为每次查询产生一个事务。

$sql = ‘BEGIN

UPDATE TEST_A SET COLUMN_A = 1 WHERE ROWNUM = 1;

UPDATE TEST_B SET COLUMN_B = 2 WHERE ROWNUM =1;

 END;’;

 $conn = oci_connect(’scott’, ‘tiger’, ‘orcl’);

$stmt = oci_parse($conn, $sql);

oci_execute($stmt, OCI_DEFAULT);

// 回滚事务,返回boolean值

$rollbacked = oci_rollback($conn);

// 提交事务,返回boolean值

$commited = oci_commit($conn);

实例完毕,谢谢收看CCTV。

分类: PHP, 数据库 标签:

PHP最牛B的开发工具 – NetBeans

2009年12月5日 CanleiSky 3 条评论

无论是界面、运行效率、易用性、都比eclipse强多了。

1.运行效率高,eclipse太慢了

2.设置可以保存下来,公司可以通过保存下来的设置文件统一设置开发工具

3.提供HTML控件,虽然不是常用,但必竟有时用起来还是挺爽的

4.代码提示更完善,被支持的提示有HTML, PHP,JAVA,CSS,JAVASCRIPT,JQUERY等一些框架的提示,非常不错

分类: 开发工具 标签:

jquery UI dialog 挡住浮动层或iframe

2009年12月5日 CanleiSky 没有评论

jquery UI dialog 老是挡住浮动的层,导致dialog弹出后浮动元素消失,这种情况只会在ie6下出现,解决办法是在浮动层的CSS样式里设置一下高度就可以了。

分类: JavaScript 标签:

CentOS安装wine 1.1.33日记

2009年11月22日 CanleiSky 没有评论

1.去官网下载wine 1.1.33,下载后解压到/opt/download/wine-1.1.33

下载地址:http://www.winehq.org/announce/1.1.33

2.cd /opt/downloa/wine-1.1.33

3.执行安装前环境检查

[root@localhost wine-1.1.33]# ./configure

如果如现checking error … please install xxx 字样表示执行这项检查报错,再执行yum install xxx,这里如果报Xlib/Xfree86错误的话要执行

[root@localhost wine-1.1.33]# yum -y groupinstall “X Software Development”

或者:

[root@localhost wine-1.1.33]# yum install libxorg-x11-devel
[root@localhost wine-1.1.33]# yum install libxorg-x11-static-devel

执行安装后再次运行

[root@localhost wine-1.1.33]# ./configure

直到完全检查成功为止

4.进行编译安装,这个过程非常长,耐心等待编译完成:

[root@localhost wine-1.1.33]# make depend && make

分类: 服务器 标签:

设置Oracle Instant Client(windows 版)

2009年11月22日 CanleiSky 1 条评论

第一步,下载Instant Client for Microsoft Windows (32-bit)
        下载地址:http://www.oracle.com/technology/software/tech/oci/instantclient/htdocs/winsoft.html
        下载和数据库版本一至的Instant Client,本机是10g r2所以选择Version 10.2.0.4

安装:
        1.直接解压到一个目录,本机是:D:\WareSoft\instantclient_10_2
        2.设置环境变量
            在PATH变量后追回D:\WareSoft\instantclient_10_2
            新建TNS_ADMIN变量,值为:D:\WareSoft\instantclient_10_2
            新建LD_LIBRARY_PATH变量,值为:D:\WareSoft\instantclient_10_2
           新建NLS_LANG变量(连接字符集,本机是UTF-8),值为:SIMPLIFIED CHINESE_CHINA.AL32UTF8
3.创建链接配置文件
           新建文件,命名为:tnsnames.ora,放到D:\WareSoft\instantclient_10_2下,内容示例:
192.168.1.105 =
    (DESCRIPTION =
        (ADDRESS_LIST =
            (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.105)(PORT = 1521))
    )
    (CONNECT_DATA =
        (SERVICE_NAME = XE)
)
)
内容说明:
192.168.1.105(链接别名) =
    (DESCRIPTION =
        (ADDRESS_LIST =
            (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.105(服务器IP))(PORT = 1521(服务端口)))
    )
    (CONNECT_DATA =
        (SERVICE_NAME = XE(服务端口,rpm版的为XE,tar版的是ORCL)
    )
)

分类: 数据库 标签:

CentOS安装Oracle 10G R2日记

2009年11月21日 CanleiSky 没有评论

Oracle可以免费使用
Oracle 10G R2下载地址:
http://www.oracle.com/technology/global/cn/software/products/database/xe/htdocs/102xelinsoft.html
下载通用版(含语言包的) : oracle-xe-univ-10.2.0.1-1.0.i386.rpm
rpm包无法设置安装路径,如果一定要设置选译tar.gz的
下载后打开命令行:
[root@localhost ~]# cd /opt/download/
[root@localhost download]# rpm -ivh oracle-xe-univ-10.2.0.1-1.0.i386.rpm
一直回车,输入SYSTEM密码,安装成功。
默认数据库端口是1521,默认HTTP管理端口是8080,HTTP管理地址:http://127.0.0.1:8080/apex

安装完成,现在可以去http://127.0.0.1:8080/apex爽一把了

分类: 数据库 标签:

为系统增加右键菜单

2009年10月23日 CanleiSky 没有评论

实例:通过编辑注册表新建一个菜单叫“用EditPlus 编辑”

开始、运行、regedit

找到HKEY_CLASSES_ROOT,下面是系统所有扩展名列表,我们选择*,在下面shell目录里新建一个项,命名叫“用EditPlus 编辑”,然后在里面新建项 command ,选中command,在右边选中默认击右键->修改值为D:\WareSoft\EditPlus33.1\EditPlus 3\EditPlus.exe “%1″ 这是EditPlus的路径,后面的”%1″是把触发这个事件的对象传递给EditPlus吧,偶是这么理解的

分类: 未分类 标签:

JavaScript stringToTime(字符转时间),dateDiff(时间相减)

2009年9月23日 CanleiSky 没有评论


function stringToTime(string){
var f = string.split(' ', 2);
var d = (f[0] ? f[0] : '').split('-', 3);
var t = (f[1] ? f[1] : '').split(':', 3);

return (new Date(
parseInt(d[0], 10) || null,
(parseInt(d[1], 10) || 1)-1,
parseInt(d[2], 10) || null,
parseInt(t[0], 10) || null,
parseInt(t[1], 10) || null,
parseInt(t[2], 10) || null
)).getTime();

}

function dateDiff(date1, date2){
var type1 = typeof date1, type2 = typeof date2;

if(type1 == 'string')
date1 = stringToTime(date1);
else if(date1.getTime)
date1 = date1.getTime();

if(type2 == 'string')
date2 = stringToTime(date2);
else if(date2.getTime)
date2 = date2.getTime();

return (date1 - date2) / 1000;//结果是毫秒
}

分类: JavaScript 标签:

SQL SERVER设置自增字段值

2009年9月22日 CanleiSky 没有评论

把下面的TableName改成自己的表名,0是设置自增值是多少

DBCC CHECKIDENT(TableName, RESEED, 0)

分类: 数据库 标签: