⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmrsa.cpp

📁 rsa realisation in c++ language
💻 CPP
📖 第 1 页 / 共 2 页
字号:
             "                       (for encrypting or verifying signature).\n"
             "                  -pr: Similarly transforms the input, but using the\n"
             "                       private key (for decrypting or signing).\n"
             "                  -r : You can change the public key in the key file\n"
             "                       and use this command to generate a new\n"
             "                       private key corresponding to that public key.\n"
             "                       This command requires that the key file\n"
             "                       contain the \"private p\" and \"private q\"\n"
             "                       values.  If the specified public key is\n"
             "                       invalid, it will be incremented until valid.\n"
             "                       The public key should be significantly less\n"
             "                       than the mod, but greater than 2.\n"
             " -c : Only convert based on input and output modes.\n"
             "      No key is used to transform the actual values.\n"
             "Examples:\n"
             "  Generate a key file \"mykeys.txt\" containing 256-bit keys in hex format\n"
             "    bmrsa -mkh -g 16 -f MyKeys.txt\n"
             "  Encrypt the text file \"Readme.txt\" using the public key in MyKeys.txt\n"
             "  and output the results to \"Readme.enc\" in hex format:\n"
             "    bmrsa -mkh -mit -moh -pu -f MyKeys.txt <Readme.txt >Readme.enc\n"
             "  Decrypt the text file \"Readme.enc\" using the private key in MyKeys.txt\n"
             "  and output the results to the display:\n"
             "    bmrsa -mkh -mih -mot -pr -f MyKeys.txt <Readme.enc\n"
             "  \"Sign\" the text file \"Readme.txt\" using the private key in MyKeys.txt\n"
             "  and output the binary signed data to \"Readme.sgn\":\n"
             "    bmrsa -mkh -mit -pr -f MyKeys.txt <Readme.txt >Readme.sgn\n"
             "  Verify the signed file \"Readme.sgn\" using the public key in MyKeys.txt\n"
             "  and output the verified plain text to the display:\n"
             "    bmrsa -mkh -mot -pu -f MyKeys.txt <Readme.sgn\n"
             "  Convert this help text to a series of hex numbers and then back to plain\n"
             "  text output to the display ...trust me :):\n"
             "    bmrsa | bmrsa -mit -moh -c | bmrsa -mih -mot -c\n"
             "Notes:\n"
             "  1. Key generation can be slow for large keys.  Be prepared to wait around\n"
             "     10+ minutes for a pair of 128-byte primes.  Transforming text using the\n"
             "     resulting 2048-bit mod can be quite slow as well.\n"
             "  2. Values larger than 128 for byte count will work, but are not\n"
             "     recommended due to performance issues.\n"
             "  3. Each line of input, no matter what format, must be 600 characters max.\n"
             "  4. Each line of input when using a non-text mode must represent a value\n"
             "     no larger than the public mod value in the key file.\n"
             "  5. The private key and P and Q values may be removed from the key file\n"
             "     if you are only transforming based on a public key. (P and Q are only\n"
             "     needed for re-generating new public and private keys on the same mod.\n"
             "     They should be kept as secret as the private key.)");
      return 0;
   }

   FILE *pFile = NULL;
   CBigNum PubMod, PubKey, PriKey, PriP, PriQ;

   if ((nCommand == 2) || (nCommand == 3) || (nCommand == 5))
   {
      unsigned int nFileSize;
      pFile = fopen(szKeyFile, "rt");
      if (NULL == pFile)
      {
         printf("Cannot open key file %s for reading.\n", szKeyFile);
         return -1;
      }
   
      fseek(pFile, 0, SEEK_END);
      nFileSize = ftell(pFile);
      rewind(pFile);
      if (nFileSize > 50000)
      {
         puts("Key file too big.");
         fclose(pFile);
         return -1;
      }

      char *szFileData = new char[nFileSize+1];
   
      int bytesRead = fread(szFileData, sizeof(szFileData[0]), nFileSize, pFile);
      fclose(pFile);
      nFileSize = bytesRead;

      szFileData[nFileSize] = '\0';

      char *pStartMod, *pStartPubKey, *pStartPriKey, *pStartPriP, *pStartPriQ,
         *pEnd1=NULL, *pEnd2=NULL, *pEnd3=NULL, *pEnd4=NULL, *pEnd5=NULL;

      pStartMod = strstr(szFileData, "public mod=");
      if (!pStartMod)
      {
         delete szFileData;
         puts("Public mod not found");
         return -1;
      }
      for (pStartMod=pStartMod + 11; *pStartMod; pStartMod++)
         if (!isspace(*pStartMod))
            break;
      for (pEnd1 = pStartMod; *pEnd1; pEnd1++)
         if (isspace(*pEnd1))
            break;

      pStartPubKey = strstr(szFileData, "public key=");
      if (pStartPubKey)
      {
         for (pStartPubKey=pStartPubKey + 11; *pStartPubKey; pStartPubKey++)
            if (!isspace(*pStartPubKey))
               break;
         for (pEnd2 = pStartPubKey; *pEnd2; pEnd2++)
            if (isspace(*pEnd2))
               break;
      }

      pStartPriKey = strstr(szFileData, "private key=");
      if (pStartPriKey)
      {
         for (pStartPriKey=pStartPriKey + 12; *pStartPriKey; pStartPriKey++)
            if (!isspace(*pStartPriKey))
               break;
         for (pEnd3 = pStartPriKey; *pEnd3; pEnd3++)
            if (isspace(*pEnd3))
               break;
      }

      pStartPriP = strstr(szFileData, "private p=");
      if (pStartPriP)
      {
         for (pStartPriP=pStartPriP + 10; *pStartPriP; pStartPriP++)
            if (!isspace(*pStartPriP))
               break;
         for (pEnd4 = pStartPriP; *pEnd4; pEnd4++)
            if (isspace(*pEnd4))
               break;
      }

      pStartPriQ = strstr(szFileData, "private q=");
      if (pStartPriQ)
      {
         for (pStartPriQ=pStartPriQ + 10; *pStartPriQ; pStartPriQ++)
            if (!isspace(*pStartPriQ))
               break;
         for (pEnd5 = pStartPriQ; *pEnd5; pEnd5++)
            if (isspace(*pEnd5))
               break;
      }

      *pEnd1 = '\0';
      if (pEnd2) *pEnd2 = '\0';
      if (pEnd3) *pEnd3 = '\0';
      if (pEnd4) *pEnd4 = '\0';
      if (pEnd5) *pEnd5 = '\0';

      switch(nTextModeK)
      {
      case 0:
         PubMod = pStartMod;
         if (pStartPubKey)
            PubKey = pStartPubKey;
         if (pStartPriKey)
            PriKey = pStartPriKey;
         if (pStartPriP)
            PriP = pStartPriP;
         if (pStartPriQ)
            PriQ = pStartPriQ;
         break;
      case 1:
         PubMod = CBigNum::FromHexString(pStartMod);
         if (pStartPubKey)
            PubKey = CBigNum::FromHexString(pStartPubKey);
         if (pStartPriKey)
            PriKey = CBigNum::FromHexString(pStartPriKey);
         if (pStartPriP)
            PriP = CBigNum::FromHexString(pStartPriP);
         if (pStartPriQ)
            PriQ = CBigNum::FromHexString(pStartPriQ);
         break;
      case 2:
         PubMod = CBigNum::FromByteString(pStartMod);
         if (pStartPubKey)
            PubKey = CBigNum::FromByteString(pStartPubKey);
         if (pStartPriKey)
            PriKey = CBigNum::FromByteString(pStartPriKey);
         if (pStartPriP)
            PriP = CBigNum::FromByteString(pStartPriP);
         if (pStartPriQ)
            PriQ = CBigNum::FromByteString(pStartPriQ);
         break;
      case 3:
         PubMod = CBigNum::FromBase64String(pStartMod);
         if (pStartPubKey)
            PubKey = CBigNum::FromBase64String(pStartPubKey);
         if (pStartPriKey)
            PriKey = CBigNum::FromBase64String(pStartPriKey);
         if (pStartPriP)
            PriP = CBigNum::FromBase64String(pStartPriP);
         if (pStartPriQ)
            PriQ = CBigNum::FromBase64String(pStartPriQ);
         break;
      }

      delete szFileData;

      if ((nCommand == 3) && (pStartPriKey == NULL))
      {
         puts("Private key not found in key file.");
         return -1;
      }

      if (((nCommand == 2) || (nCommand == 5)) && (pStartPubKey == NULL))
      {
         puts("Public key not found in key file.");
         return -1;
      }

      if ((nCommand == 5) && ((pStartPriP == NULL) || (pStartPriQ == NULL)))
      {
         puts("Private P and/or Private Q not found in key file.");
         return -1;
      }
   }

   if ((nCommand > 1) && (nCommand < 5))
   {
      char szLineBuf[601];
      CBigNum Transform;
      CBigNumString strTransform;
      size_t cbReadCount;
      unsigned int nMaxByteCount;
      if (nCommand < 4)
      {
         nMaxByteCount = (PubMod.log2()-1) / 8U;
         if (nMaxByteCount >= sizeof(szLineBuf))
            nMaxByteCount = sizeof(szLineBuf) - 1;
      }
      else
      {
         if (nTextModeI == 2)
            nMaxByteCount = 80;
         else
            nMaxByteCount = sizeof(szLineBuf) - 1;
      }

      while(1)
      {
         if (nTextModeI == 2)
         {
            if (feof(stdin))
               break;
            cbReadCount = fread(szLineBuf, 1, nMaxByteCount, stdin);
            if (cbReadCount == 0)
               break;
            szLineBuf[cbReadCount] = '\0';
         }
         else
         {
            if (!fgets(szLineBuf, sizeof(szLineBuf), stdin))
               break;
            if (!feof(stdin) && (!isspace(szLineBuf[strlen(szLineBuf)-1])))
            {
               fputs("Entry too long.\n", stderr);
               while (1)
               {
                  char ch;
                  ch = getchar();
                  if ((ch==EOF) || (ch=='\n'))
                     break;
               }
               continue;
            }
            while (isspace(szLineBuf[strlen(szLineBuf)-1]))
               szLineBuf[strlen(szLineBuf)-1] = '\0';
         }
         switch(nTextModeI)
         {
         case 0:
            Transform = szLineBuf;
            break;
         case 1:
            Transform = CBigNum::FromHexString(szLineBuf);
            break;
         case 2:
            Transform = CBigNum::FromByteString(szLineBuf);
            break;
         case 3:
            Transform = CBigNum::FromBase64String(szLineBuf);
            break;
         }
         if (nCommand < 4)
         {
            if (Transform > PubMod)
            {
               fputs("Too much text in a single entry.\n", stderr);
               continue;
            }
            else if (nCommand == 2)
            {
               Transform = Transform.PowMod(PubKey, PubMod);
            }
            else if (nCommand == 3)
            {
               Transform = Transform.PowMod(PriKey, PubMod);
            }
         }

         switch(nTextModeO)
         {
         case 0:
            strTransform = Transform;
            break;
         case 1:
            strTransform = Transform.ToHexString();
            break;
         case 2:
            strTransform = Transform.ToByteString();
            break;
         case 3:
            strTransform = Transform.ToBase64String();
            break;
         }

         if (nTextModeO==2)
            printf("%s", (const char *)strTransform);
         else
            puts(strTransform);
      }
   }
   else
   {
      CBigNumString strMod, strPubKey, strPriKey, strP, strQ;

      MakeSmallPrimes();

      if (nCommand == 1)
      {      
         PriP = 0U;
         PriQ = 0U;
      }

      if (nBytes > 0)
         GenKeyPair(PubMod, PubKey, PriKey, PriP, PriQ, nBytes);
      else
         GenKeyPair(PubMod, PubKey, PriKey, PriP, PriQ);
      
      if (szKeyFile[0])
      {
         pFile = fopen(szKeyFile, "wt");
         if (NULL == pFile)
         {
            printf("Cannot open key file %s for writing.\n", szKeyFile);
            return -1;
         }
      }

      switch(nTextModeK)
      {
      case 0:
         strMod = PubMod;
         strPubKey = PubKey;
         strPriKey = PriKey;
         strP = PriP;
         strQ = PriQ;
         break;
      case 1:
         strMod = PubMod.ToHexString();
         strPubKey = PubKey.ToHexString();
         strPriKey = PriKey.ToHexString();
         strP = PriP.ToHexString();
         strQ = PriQ.ToHexString();
         break;
      case 2:
         strMod = PubMod.ToByteString();
         strPubKey = PubKey.ToByteString();
         strPriKey = PriKey.ToByteString();
         strP = PriP.ToByteString();
         strQ = PriQ.ToByteString();
         break;
      case 3:
         strMod = PubMod.ToBase64String();
         strPubKey = PubKey.ToBase64String();
         strPriKey = PriKey.ToBase64String();
         strP = PriP.ToBase64String();
         strQ = PriQ.ToBase64String();
         break;
      }


      fprintf(pFile==NULL?stdout:pFile,
         "public mod=%s\npublic key=%s\nprivate key=%s\nprivate p=%s\nprivate q=%s",
         (const char *)strMod,
         (const char *)strPubKey,
         (const char *)strPriKey,
         (const char *)strP,
         (const char *)strQ);
      
      if (pFile != NULL)
         fclose(pFile);
   }

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -