- UID
- 44482
注册时间2008-2-7
阅读权限20
最后登录1970-1-1
以武会友
 
该用户从未签到
|
发表于 2009-6-6 11:31:49
|
显示全部楼层
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <string.h>
- void main( void )
- {
- int i,n;
- int nTmp = 0;//临时存放rand()取得的值
- static int nIndex = 0;//用来做数组索引
- char szBuffer[256] = {0}; //待加密字串
- char szEncrypt[256] = {0}; //存放加密结果
- int* pAry = NULL;
- int nLen = 0;
- printf("Input string:");
- scanf("%s",szBuffer); //输入
- nLen = strlen(szBuffer);
- pAry = (int*)malloc(nLen*sizeof(int)+1); //申请一个动态长度的数组
- if (NULL == pAry)
- {//是否申请空间失败
- printf("error\r\n");
- return;
- }
- memset(pAry,-1,nLen*sizeof(int));//初始化为-1,为了方便区分
- srand( (unsigned)time( NULL ) );//用时间做随即种子
- for (n = 0; n < nLen; n++) //控制取nLen个数字
- {
- while(1)
- {
- nTmp = rand()%nLen;//使数在0-nLen间
- for(i=0; i<=nLen; i++)
- {//判断数字是否存在
- if(pAry[i] == nTmp) break;
- }
- if(i == (nLen+1) && pAry[nLen] != nTmp)
- {//如果不存在则..
- pAry[nIndex] = nTmp;
- szEncrypt[nIndex] = szBuffer[nTmp];
- nIndex++;
- break;
- }
- }
- }
- /* Display . */
- printf("key:");
- for( n = 0; n < nLen; n++ )
- printf("%d,",pAry[n]);
- printf("\r\nEncrypt Result:%s\r\n",szEncrypt);
- system("pause");
- }
复制代码 |
|