菜单
  

    byte[] raw = ly.encrypt(私钥, md5digest.getBytes());
    //用私钥加密MD5消息摘要
    File file = new File(保存数字签名的文件);
    OutputStream out = new FileOutputStream(file);
    //将签名转成比特流写入文件
    out.write(raw);
    out.close();}
    catch (Exception e)
    {差错反应机制}。
    3.2 验证数字签名
    验证签名的算法有两步:第一步是接收者解密签名块后得到摘要,然后接收方计算明文的摘要并比较这两个摘要,若相同则能断定发送方的身份。验证过程中需要输入的数据为消息、发送者的公钥、签名块;输出的内容为验证的判断结果。主要方法描述如下:
    3.2.1MD5消息摘要计算
    //用类lyMD5来表示MD5算法。
    public void MD5Update(byte[] input, int inputLen)
    //算法的主循环,循环的次数是消息中512位消息分组数目
    public String getMD5Digest(String msg) throws Exception
    //返回指定String的Digest摘要
    String getMD5Digest(File f) throws Exception
    //返回指定File的Digest摘要
    3.2.1签名验证
    String md5digest=null;
    try
    { File ftemp = new File(待验证文件);
    lyMD5 md5 = new lyMD5();
    //计算待验证文件MD5消息摘要
    md5digest = md5.getMD5Digest(待验证文件);
    File ft = new File(保存数字签名的文件);
    FileInputStream in = new FileInputStream(ft);
    ByteArrayOutputStream bout = newByteArrayOutputStream();
    byte[] tmpbuf = new byte[1024];
    int count = 0;
    while ((count =in.read(tmpbuf)) != -1)
    { bout.write(tmpbuf, 0, count);
    tmpbuf = new byte[1024];}
    byte[] orgData =bout.toByteArray();
    byte[] raw = ly.decrypt(验证公钥, orgData);
    //解密保存数字签名的文件,得到数字签名
    String newdigest=new String(raw);
    if(newdigest.equals(md5digest))
    {提示签名验证正确}
    //判断摘要是否匹配
    else{提示签名不匹配}}
    catch (Exception e2)
    {差错反应机制}。
    4 RSA数字签名方案
    4.1 大数运算库
    因为实现数字签名有很多大数的运算所以定义大数的数据结构是很必要的。
        typedef struct
        {
        unsigned long int bn[MAX_LENGTH];
        unsigned int size;
        }BigNum;
        在加密过程和解密过程中会有大数的一些基本运算。所以应定义包括大数的加、减、乘、除、取模等运算的基本运算库,其中的模乘和模幂运算最为重要。
    int Cmp(struct BigNum*a,struct BigNum*b);
    //大数的比较
    struct BigNum Add (struct BigNum*a,struct BigNum*b);
    //大数加运算
    struct BigNum Sub(struct BigNum*a,struct BigNum*b);
    //大数减运算
    struct BigNum Mul(struct CBigInt*a,struct BigNum*b);
    //大数乘运算
    struct BigNum Div(struct BigNum*a,struct BigNum*b);
    //大数除运算
    struct BigNum Mod(struct BigNum*a,struct BigNum*b);
    //大数模运算
    struct BigNum Euc(struct BigNum*a,struct BigNum*b);
    //大数求逆运算
    struct BigNum Get(char*st,unsigned int system);
    //字符串转换为大数
    void Put(struct BigNum*a,char*c,unsigned int system);
    //大数转换为字符
        模幂算法是加密解密过程的核心。目前的模幂算法中比较有效的一种算法是采用"平方-乘"的方法。
    4.2 密钥生成与管理
    //类lyRSA用来表示密钥生成算法。
    public static KeyPair generateKeyPair(int key_size) throws EncryptException
    //生成密钥对,key_size代表密钥长度
    public static PublicKey generateRSAPublicKey() throws EncryptException
    //生成公钥
    Public static PrivateKey generateRSAPrivateKey() throws EncryptException
    //生成私钥,另使用Access数据库保存密钥,提供管理方法。
  1. 上一篇:VB+access高校图书借阅系统的设计与实现
  2. 下一篇:ASP.net英语四六级在线报考平台设计与实现
  1. 基于MATLAB的图像增强算法设计

  2. jsp+sqlserver高校二手商品交...

  3. 基于Kinect的手势跟踪与识别算法设计

  4. JAVA基于安卓平台的医疗护工管理系统设计

  5. java+mysql设备监控记录的大...

  6. 基于核独立元分析的非线...

  7. 基于Hadoop的制造过程大数据存储平台构建

  8. 电站锅炉暖风器设计任务书

  9. 大众媒体对公共政策制定的影响

  10. 河岸冲刷和泥沙淤积的监测国内外研究现状

  11. 酸性水汽提装置总汽提塔设计+CAD图纸

  12. 中考体育项目与体育教学合理结合的研究

  13. 十二层带中心支撑钢结构...

  14. 杂拟谷盗体内共生菌沃尔...

  15. 当代大学生慈善意识研究+文献综述

  16. java+mysql车辆管理系统的设计+源代码

  17. 乳业同业并购式全产业链...

  

About

751论文网手机版...

主页:http://www.751com.cn

关闭返回