GalaxyPilot —— D.S


        生命不熄,战斗不止
数据加载中……

java DSA 数字签名

        在下面的代码中,把DSA的公钥和私钥保存在文件中,要使用该代码你需要先得到DSA算法的公钥和私钥才行。其实下面的代码就是JProbe 6.0.2 不完整分析的注册机,本打算在看雪论坛混片精华,分析到后来才发现注册文件在二进制代码中有多处验证,这不是我的强项,再加上年末事情较多,所以只好放弃。

public final class Codecs
{

    private Codecs()
    {
    }

    public static final byte[] base64Encode(byte abyte0[])
    {
        if(abyte0 == null)
            return null;
        byte abyte1[] = new byte[((abyte0.length + 2) / 3) * 4];
        int i = 0;
        int j = 0;
        for(; i < abyte0.length - 2; i += 3)
        {
            abyte1[j++] = Base64EncMap[abyte0[i] >>> 2 & 0x3f];
            abyte1[j++] = Base64EncMap[abyte0[i + 1] >>> 4 & 0xf | abyte0[i] << 4 & 0x3f];
            abyte1[j++] = Base64EncMap[abyte0[i + 2] >>> 6 & 3 | abyte0[i + 1] << 2 & 0x3f];
            abyte1[j++] = Base64EncMap[abyte0[i + 2] & 0x3f];
        }

        if(i < abyte0.length)
        {
            abyte1[j++] = Base64EncMap[abyte0[i] >>> 2 & 0x3f];
            if(i < abyte0.length - 1)
            {
                abyte1[j++] = Base64EncMap[abyte0[i + 1] >>> 4 & 0xf | abyte0[i] << 4 & 0x3f];
                abyte1[j++] = Base64EncMap[abyte0[i + 1] << 2 & 0x3f];
            } else
            {
                abyte1[j++] = Base64EncMap[abyte0[i] << 4 & 0x3f];
            }
        }
        for(; j < abyte1.length; j++)
            abyte1[j] = 61;

        return abyte1;
    }

    public static final byte[] base64Decode(byte abyte0[])
    {
        if(abyte0 == null)
            return null;
        int i;
        for(i = abyte0.length; abyte0[i - 1] == 61; i--);
        byte abyte1[] = new byte[i - abyte0.length / 4];
        for(int j = 0; j < abyte0.length; j++)
            abyte0[j] = Base64DecMap[abyte0[j]];

        int k = 0;
        int l;
        for(l = 0; l < abyte1.length - 2; l += 3)
        {
            abyte1[l] = (byte)(abyte0[k] << 2 & 0xff | abyte0[k + 1] >>> 4 & 3);
            abyte1[l + 1] = (byte)(abyte0[k + 1] << 4 & 0xff | abyte0[k + 2] >>> 2 & 0xf);
            abyte1[l + 2] = (byte)(abyte0[k + 2] << 6 & 0xff | abyte0[k + 3] & 0x3f);
            k += 4;
        }

        if(l < abyte1.length)
            abyte1[l] = (byte)(abyte0[k] << 2 & 0xff | abyte0[k + 1] >>> 4 & 3);
        if(++l < abyte1.length)
            abyte1[l] = (byte)(abyte0[k + 1] << 4 & 0xff | abyte0[k + 2] >>> 2 & 0xf);
        return abyte1;
    }

    private static byte Base64EncMap[];
    private static byte Base64DecMap[];

    static
    {
        byte abyte0[] = {
            65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
            75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
            85, 86, 87, 88, 89, 90, 97, 98, 99, 100,
            101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
            111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
            121, 122, 48, 49, 50, 51, 52, 53, 54, 55,
            56, 57, 43, 47
        };
        Base64EncMap = abyte0;
        Base64DecMap = new byte[128];
        for(int i = 0; i < Base64EncMap.length; i++)
            Base64DecMap[Base64EncMap[i]] = (byte)i;

    }
}

import java.io.UnsupportedEncodingException;

public final class SignableBlock
{

    private SignableBlock()
    {
    }

    public static byte[] createSignableBlock(String as[])
        throws UnsupportedEncodingException
    {
        StringBuffer stringbuffer = new StringBuffer(256);
        for(int i = 0; i < as.length; i++)
            stringbuffer.append(as[i]);

        return stringbuffer.toString().getBytes("UTF-8");
    }
}

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


final class ValidateSignature
{

    private ValidateSignature()
    {
    }
   
    public static void main(String[] args)
    {
     String[] as = {"qq:8117892","www.blogjava.net/galaxyp","DSA数字签名"};
     String s = "MCwCFFSpNp/n4Mq24FDXyVkk/kr815yHAhQO0TLslIJOqUes4OFn0ARvFaAVOw==";
     try{
      System.out.println(validateSignature(as,s));
     }catch(Exception e){}
    }

    private static boolean validate(byte abyte0[], byte abyte1[])
    {
        Signature signature = null;
        boolean flag = false;
        try
        {
         java.io.ObjectInputStream in=new java.io.ObjectInputStream(new java.io.FileInputStream("myprikey.dat"));
            PrivateKey prikey=(PrivateKey)in.readObject();
            in.close();
           
            java.io.ObjectInputStream in2=new java.io.ObjectInputStream(new java.io.FileInputStream("mypubkey.dat"));
            PublicKey pubkey=(PublicKey)in2.readObject();
            in2.close();
           
            signature = Signature.getInstance("SHA/DSA");
           
            signature.initSign(prikey);
            signature.update(abyte0);
            byte[] bt = signature.sign();
            String encode =new String(Codecs.base64Encode(bt),"UTF-8");
            System.out.println(encode);
           
            signature.initVerify(pubkey);
            signature.update(abyte0);
           
            flag = signature.verify(abyte1);
        }
        catch(Exception e)
        { 
            return false;
        }
        return flag;
    }

    private static boolean validateSignatureBytes(String as[], byte abyte0[])
        throws UnsupportedEncodingException
    {
        byte abyte1[] = SignableBlock.createSignableBlock(as);
        return validate(abyte1, abyte0);
    }

    public static boolean validateSignature(String as[], String s)
        throws UnsupportedEncodingException
    {
        byte abyte0[];
        try
        {
            abyte0 = Codecs.base64Decode(s.getBytes("UTF-8"));
        }
        catch(ArrayIndexOutOfBoundsException arrayindexoutofboundsexception)
        {
            return false;
        }
        return validateSignatureBytes(as, abyte0);
    }
}

posted on 2006-12-27 09:41 舵手 阅读(3191) 评论(4)  编辑  收藏

评论

# re: java DSA 数字签名  回复  更多评论   

招聘外挂破解技术员
人员: 2名-3名
项目: 网络游戏外挂
项目资料:海外网络游戏,无风险,不牵扯法律责任。
支持当面交易.
工作地点:SOHO
工作待遇:详谈
要求 能独立完成软件加密解密
精通软件软件逆向分析工程、软件汇编/反汇编分析
精通系统底层(API)开发、调试/反调试技术
精通网络通讯协议分析与开发,熟悉网络游戏通过协议及架构
熟练掌握VC、ASM、VB编程语言中任意一种
有能力的请与我联系 寻求可以长期合作的
可将您的个人简历和联系方式投放 EMAI: jiaxinwb@163.com
2007-01-03 17:37 | sssss

# re: java DSA 数字签名  回复  更多评论   

好,支持。
你写的比较高深,我也写了一篇关于数字签名的文章,有时间希望能够交流交流。地址如下:http://coresun.blog.sohu.com/70922655.html
2007-11-20 14:16 | 孙志伟

# re: java DSA 数字签名  回复  更多评论   

兄弟学习了;我也正在用数字签名;请问myprikey.dat,mypubkey.dat是怎么回事;程序运行结果 false;请赐教:EMAIL:kongfanyu7241@163.com
2008-01-02 11:31 | 孔夫子

# re: java DSA 数字签名  回复  更多评论   

人好技术好。
2008-03-05 17:25 | 人好技术好。

只有注册用户登录后才能发表评论。


网站导航: