精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>C/C++>>硬件、外设相关>>Re: 如何取得CPUID和硬盘序列号(非格式化

主题:Re: 如何取得CPUID和硬盘序列号(非格式化
发信人: dragegg()
整理人: wenbobo(2002-12-06 22:49:21), 站内信件
我也忘记是从哪本杂志上抄来的了。送你吧!

/*硬盘序列号的获取:
   在进行加密算法时,经常需要用到硬盘序列号,我们可以用一个简单的程序来
取得。
关于硬盘序列号有两种:
   硬盘序列号(Hard disk Serial Number)是出厂时厂家为区别产品而设置的,
它是惟
一的,只读的。利用硬盘序列号的加密往往是利用其惟一和只读的特性,大多是
针对有
序列的IDE HDD而言,对于没有序列号或SCIS HDD硬盘则无能为力,这也是利用它
进行加
密的局限性。
   卷的序列号(Volume Serial Number)既可指软磁盘,如A盘和B盘,又可以硬
盘的逻
辑盘,如C,D**是高级格化时随机产生的。它可以修改,所以利用其进行加密,
其唯王
码电脑公司软件中心性还可,而其可修改性对于安全而言就大打折扣了。
   在DOS环境下,如两程序用TC或BC编译后运行即可:程序1:获得ID硬盘C的序
列号 */

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <string.h>
#include <conio.h>

char * getascii(unsigned int in_data[],int off_statr,int off_end);

void main(void)
{
 unsigned int dd[256];  /* diskdata */
 unsigned int dd_off;   /* diskdata offset */
 while(inp(0x1F7)!=0x50);     /* wait for controller not busy */
 outp(0x1F7,0xA0);   /* get first/second drive */
 outp(0x1F7,0xEC);   /* get drive info data */
 while(inp(0x1F7)!=0x58);   /* wait for data ready */
 for(dd_off=0;dd_off!=256;dd_off++)       /* read "sector" */
    dd[dd_off]=inpw(0x1F0);
 printf("The Serial Number HardDisk [C] is %s",getascii(dd,10,19));
}

char * getascii(unsigned int in_data[],int off_start,int off_end)
{
 static char ret_val[255];
 int loop,loopl;
 for(loop=off_start,loopl=0;loop<=off_end;loop++)
{
ret_val[loopl++]=(char)(in_data[loop]/256); /* get high byte */
ret_val[loopl++]=(char)(in_data[loop]%256); /* get low byte */
}
ret_val[loopl]='\0'; /* make sure it ends in a NULL character */
return(ret_val);
}



/* 例子2:获取逻辑盘C的序列号 */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <dos.h>

void main(void)
{
 char serial_no[10];
 union REGS r;
 struct SREGS s;
 unsigned sno1,sno2;

 r.x.ax=0x6900;
 r.h.bl=3;         /* a=1 b=2 c=3 */
 segread(&s);
 intdosx(&r,&r,&s);
 if(r.x.cflag)
   *serial_no='\0';
 else
   {
    sno2=*((unsigned far *)MK_FP(s.ds,r.x.dx+2));
    sno1=*((unsigned far *)MK_FP(s.ds,r.x.dx+4));
    sprintf(serial_no,"%04x-%04x\n",sno1,sno2);
   }
 printf("The Serial Number of Login Disk [C] is %s",serial_no);
}

--
或许该改名叫做潮汐了,没钱没时间,也就只能限制在一个月上网一次了。
欢迎你给我写信,但若想收信则只能请你耐心等候了。
"路漫漫其修远兮,吾将上下而求索!"
回复时请CLICK 一下“将本文章寄一份给原作者”后的方框,谢谢

※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.104.33.128]

[关闭][返回]