haods 发表于 2007-07-23 20:19

求助DB2 MD5算法函数!

要在db2数据库存放密码,想用MD5加密,求助DB2 MD5算法函数!谢谢!

haods 发表于 2007-07-24 09:31

哪位大侠有这方面的资料啊!

哪位大侠有这方面的资料啊!谢谢啦!~~~~~~~~~~~~

大梦 发表于 2007-07-24 13:37

package db2udf;

import java.io.*;
import java.security.*;

public class MyUDF {

        public static int putLine(String inLine) throws Exception {
                int outCount = 0;
                try {
                        outCount = inLine.length();
                        File writefile = new File("TraceOut.log");
                        if (writefile.exists() == false) {
                                writefile.createNewFile();
                                writefile = new File("TraceOut.log");
                        }
                        FileWriter filewriter = new FileWriter(writefile, true);
                        filewriter.write("[" + "Timestamp" + "] " + inLine + "\n");
                        filewriter.flush();
                        filewriter.close();
                } catch (Exception d) {
                        System.out.println(d.getMessage());
                }
                return outCount;
        }

        public static String MD5(String s) throws Exception {
                String s1 = new String("");
                char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                                'a', 'b', 'c', 'd', 'e', 'f' };
                try {
                        byte[] strTemp = s.getBytes();
                        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
                        mdTemp.update(strTemp);
                        byte[] md = mdTemp.digest();
                        int j = md.length;
                        char str[] = new char;
                        int k = 0;
                        for (int i = 0; i < j; i++) {
                                byte byte0 = md;
                                str = hexDigits;
                                str = hexDigits;
                        }
                        s1 = new String(str);
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }
                return s1;
        }
}
搞个java的udf

[ 本帖最后由 大梦 于 2007-7-24 13:40 编辑 ]

lizhuo 发表于 2007-07-25 10:25

db2 有加密函数。

大梦 发表于 2007-07-25 11:13

ENCRYPT与DECRYPT_BIN and DECRYPT_CHAR那个是可逆的!
加密码的结果与标准MD5的对不上!

[ 本帖最后由 大梦 于 2007-7-25 11:18 编辑 ]

haods 发表于 2007-07-25 13:33

多谢 大梦 的支持,为我找到了解决的方向,十分感谢!

haods 发表于 2007-07-25 19:28

回复 #3 大梦 的帖子

import java.io.*;
import java.security.*;
import COM.ibm.db2.app.*;

public class MyUDF extends UDF {

      public static String MD5(String s) throws Exception {
                String s1 = new String("");
                char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                              'a', 'b', 'c', 'd', 'e', 'f' };
                try {
                        byte[] strTemp = s.getBytes();
                        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
                        mdTemp.update(strTemp);
                        byte[] md = mdTemp.digest();
                        int j = md.length;
                        char str[] = new char;
                        int k = 0;
                        for (int i = 0; i < j; i++) {
                              byte byte0 = md;
                              str = hexDigits;
                              str = hexDigits;
                        }
                        s1 = new String(str);
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }
                return s1;
      }
}



DROP FUNCTION MD5;
CALL sqlj.remove_jar('Md5UDF');
CALL sqlj.install_jar('file:D:\temp\Md5UDF.jar', 'Md5UDF');


CREATE FUNCTION MD5(varchar(88))
RETURNS INTEGER
EXTERNAL NAME 'Md5UDF:MyUDF!MD5'
FENCED
SCRATCHPAD 10
FINAL CALL
VARIANT
NO SQL
PARAMETER STYLE DB2GENERAL
LANGUAGE JAVA
NO EXTERNAL ACTION
;

values md5('00000000');

出错:

SQL4304N 具有特定名称 "SQL070725192051200" 的 Java 存储过程或用户定义的函数
"ORAFUN.MD5"不能装入 Java 类 "MyUDF",原因码为 "2"。SQLSTATE=42724
db2 =>
请大家帮我分析一下是什么原因?

haods 发表于 2007-07-25 19:30

在java里面执行结果是正确的。

haods 发表于 2007-07-25 20:06

终于好了,谢谢大家了!

DROP FUNCTION orafun.MD5;
CALL sqlj.remove_jar('Md5UDF');
CALL sqlj.install_jar('file:D:\temp\Md5UDF.jar', 'Md5UDF');

CREATE FUNCTION ORAFUN.MD5(VARCHAR(88))
RETURNS VARCHAR(70)
EXTERNAL NAME 'MD5UDF:MyUDF!MD5'
FENCED
VARIANT
NO SQL
EXTERNAL ACTION
LANGUAGE JAVA
PARAMETER STYLE JAVA
;
values orafun.md5('00000000');
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.security.*;
import COM.ibm.db2.app.*;

public class MyUDF extends UDF {

      public static String MD5(String s) throws Exception {
                String s1 = new String("");
                char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                              'a', 'b', 'c', 'd', 'e', 'f' };
                try {
                        byte[] strTemp = s.getBytes();
                        MessageDigest mdTemp = MessageDigest.getInstance("MD5");
                        mdTemp.update(strTemp);
                        byte[] md = mdTemp.digest();
                        int j = md.length;
                        char str[] = new char;
                        int k = 0;
                        for (int i = 0; i < j; i++) {
                              byte byte0 = md;
                              str = hexDigits;
                              str = hexDigits;
                        }
                        s1 = new String(str);
                } catch (Exception e) {
                        System.out.println(e.getMessage());
                }
                return s1;
      }
}

大梦 发表于 2007-07-25 21:19

参见

http://bbs.chinaunix.net/viewthread.php?tid=960822&extra=page%3D3
页: [1] 2
查看完整版本: 求助DB2 MD5算法函数!