懂ni 发表于 2018-7-16 11:53:46

Cisco Type 7 Password 加密解密原理和代码

  Cisco公司用于IOS的密码种类有Type0-7种,其中0为不加密,5为MD5签名,7为很简单的异或加密方法,姑且称为Type7密码。印象中这种加密方式好像是诞生于1997年,本着简化管理的目的,这种强度较弱的加密被保留下来。如果采用DES,RSA等这种强度相对较高的加密方式,管理员的为维护密钥要做很多工作。
  加密过程,从一个有26个ASCII字符表中,产生一个seed(0-25之间)随机抽取一个用来和明文密码的第一个字符异或,产生的结果用16进制表示,放在加密后字符串的第2、3位,然后 seed+1,再去抽取一个用来和明文密码第二个字符异或,16进制结果放在随后的位置位……。
  26字符表如下:
  {
  0x64, 0x73, 0x66, 0x64, 0x3b, 0x6b, 0x66, 0x6f,
  0x41, 0x2c, 0x2e, 0x69, 0x79, 0x65, 0x77, 0x72,
  0x6b, 0x6c, 0x64, 0x4a, 0x4b, 0x44, 0x48, 0x53 , 0x55, 0x42
  };
  用ASCII形式表示如下:
  "dsfd;kfoA,.iyewrkldJKDHSUBsgvca69834ncxv9873254k;fg87"
  如:随机产生一个0-25之间的数字,如seed=2,即为0x66。假设密码为lala。那么我们先把seed分解成一个两位数,即0*10+2 = 2,就产生了密码的前两位,02,接着,我们用0x66和"l"的的ascii形式0x6C进行异或,即0x66 ^ 0x 6C = 0x0A。放在密码的随后两位,得到020A。然后seed++,得到0x64,再与第二个明文密码字符异或,得到结果放在020A的后面。依次类推即得到密文。因此第一次取得的seed不一样,最后得到的结果基本也不一样,如果seed超过了25,那么将回滚到0.,也即seed %=26。
  解密过程就是加密的反向,先取得初始seed,也就是密文的前两位。如0623……,seed就等于0*10+6 = 6 ,从表中取出字符,和0x23异或即得到原文第一个字符。也很简单,此处不在赘述。
  代码为如下(Visual C++ 6.0 Sp6编译通过)
  //====================================================================================
  // type7.cpp : Defines the entry point for the console application.
  //
  #include "stdafx.h"
  #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>
  #include <time.h>
  char DecPwd={0};
  char xlat[] = {
  0x64, 0x73, 0x66, 0x64, 0x3b, 0x6b, 0x66, 0x6f,
  0x41, 0x2c, 0x2e, 0x69, 0x79, 0x65, 0x77, 0x72,
  0x6b, 0x6c, 0x64, 0x4a, 0x4b, 0x44, 0x48, 0x53 , 0x55, 0x42
  };
  intdecpwd(char * pOriPwd){ //解密函数
  int pp = 0;
  int iOriPwdLen = strlen(pOriPwd);
  int seed,i,j=0;
  seed = (pOriPwd-'0')*10 + (pOriPwd-'0');
  printf(&quot;intilize seed is %d\n&quot;,seed);
  for (i=2;i<=iOriPwdLen;i++){
  if (i>2 && i%2 == 0){
  DecPwd = pp ^ xlat;
  pp =0 ;
  seed %= 26;
  }
  pp*=16;
  char lala = toupper(pOriPwd);
  if(lala >= '0' && lala<='9')
  {
  pp+=lala-'0';
  continue;
  }else if (lala>='A'&& lala<='F'){
  pp+=lala-'A'+10;
  continue;
  }
  if (i != iOriPwdLen)
  return -1;
  }
  DecPwd='\0';
  return 0;
  }
  char enccode={0};
  int encpwd(char *pwd) //加密函数
  {
  int seed=0;
  srand((unsigned)time(NULL));
  rand();
  seed = (double)rand()/(RAND_MAX+1)*25;
  printf(&quot;intilize seed is %d\n&quot;,seed);
  int i ,iCode= 0;
  int iLeft,iRight;
  enccode = seed /10 + '0';
  enccode = seed %10 + '0';
  for (i = 0 ; i <= strlen(pwd)-1 ; i++)
  {
  iCode = xlat ^ pwd;
  seed %= 26;
  iLeft = iCode /16 ;
  if (iLeft > 9)
  iLeft = iLeft -10 +'A';
  else
  iLeft +='0';
  iRight = iCode % 16 ;
  if (iRight > 9)
  iRight = iRight -10 + 'A';
  else
  iRight+='0';
  enccode = iLeft;
  enccode = iRight;
  }
  enccode = 0;
  return 0;
  }
  int main(int argc,char * argv[])
  {
  if (argc == 3 && strcmp(argv,&quot;-e&quot;) == 0 && strlen(argv)<=16)
  {
  if (encpwd(argv) == 0)
  printf(&quot;结果是:%s&quot;,enccode);
  else
  printf(&quot;I'm so sorry,encrypt is failed,please try again!&quot;);
  return 1;
  }
  if (argc <2 || strlen(argv) %2 !=0 || strlen(argv) <4 ){
  printf(&quot;Cisco Type-7 password decrpter !Programmed by fuyewei at 2010-06-03.\nPlease Enter Cisco Type-7 Code Correctly! \
  \ne.g. 095F41041C480713181F13253920 and so on&quot;);
  return -1;
  }
  if(decpwd(argv) == 0)
  printf(&quot;解密成功,密码是:%s&quot;,DecPwd);
  return 0;
  }
  //==================================================================================
页: [1]
查看完整版本: Cisco Type 7 Password 加密解密原理和代码