Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by anon ( 17 years ago )
/*
 * rsa.c
 *
 *  Created on: 29.11.2009
 *      Author: Alexander
 */

#include "bigint.h"
#include "string.h"
#include "stdlib.h"
#include "util.h"

void usage(char*);
void doSign(char* keyfile, char* target, char* signout);
int doVerify(char* certfile, char* target, char* signin);

int main(int argc, char **argv) {
 bi_initialize();

 if (argc <= 4) {
  usage(argv[0]);
  exit(1);
 }
 if (strcmp(argv[1], "-s") && strcmp(argv[1], "-v")) {
  usage(argv[0]);
  exit(1);
 }

 if (strcmp(argv[1], "-s") == 0) {
  doSign(argv[3], argv[2], argv[4]);
 } else {
  if (doVerify(argv[3], argv[2], argv[4])) {
   fprintf(stdout, "File %s sucessfully check agains signature %s with cert %s", argv[2], argv[4], argv[3]);
  } else {
   fprintf(stdout, "File was modified!!!");
  }
 }

 return 0;

}

void doSign(char* keyfile, char* target, char* signout) {
 FILE* keyfd = fopen&#40;keyfile, "r"&#41;;
 if (!keyfd) {
  fprintf(stderr, "can't open keyfile %s", keyfile);
  exit(1);
 }
 bigint privateKey = bi_scan(keyfd);
 bigint modular = bi_scan(keyfd);

 unsigned long fhash = fadler32(target);

 bigint hash = int_to_bi((int) fhash);
 bigint sign = bi_mod_power(hash, privateKey, modular);

 FILE* signfd = fopen&#40;signout, "w"&#41;;
 if (!signfd) {
  fprintf(stderr, "can't open sign file %s", signout);
  exit(1);
 }
 bi_print(signfd, sign);
 fclose(signfd);

}

char* extractModularFromCert(char* certfile) {
 FILE* cfd = fopen&#40;certfile, "rb"&#41;;
 if (!cfd) {
  fprintf(stderr, "can't open sign file %s", certfile);
  exit(1);
 }
 char* s = malloc(1024);
 char* pkey = 0;
 while (!feof(cfd)) {
  fgets(s, 1024, cfd);

  if (strstr(s, "Modulus")) {
   pkey = malloc(4096);
   // следующие строки - открытый ключ. читаем пока не встретим Exponent: 65537 (0x10001)
   fgets(s, 1024, cfd);
   strcpy(pkey, s);
   fgets(s, 1024, cfd);
   while (!strstr(s, "Exponent: 65537 (0x10001)")) {
    strcat(pkey, s);
    fgets(s, 1024, cfd);
   }
   break;
  }
 }
 if (!pkey) {
  fprintf(stderr, "%s", "no key found");
  exit(1);
 }
 free(s);
 fclose(cfd);

 return pkey;

}

char* clearExtractedKey(char* key) {
 char* pkeycl = malloc(4096);
 int i, j;
 for (i = 0, j = 0; key[i] != '�'; i++) {
  if (key[i] == 't' || key[i] == 'n' || key[i] == 'r' || key[i] == ':') {
   continue;
  }
  pkeycl[j++] = key[i];
 }
 pkeycl[j] = 0x00;

 return pkeycl;
}

int doVerify(char* certfile, char* target, char* signin) {
 char* mod = extractModularFromCert(certfile);

 char* cmodul = clearExtractedKey(mod);
 free(mod);

 bigint modular = str_to_bi(cmodul);
 free(cmodul);
 bigint pubKey = int_to_bi(65537);

 FILE* sfd = fopen&#40;signin, "rb"&#41;;
 if (!sfd) {
  fprintf(stderr, "can't open sign file %s", signin);
  exit(1);
 }

 bigint signature = bi_scan(sfd);
 fclose(sfd);

 unsigned long hash = fadler32(target);
 bigint hashfs = bi_mod_power(signature, pubKey, modular);
 bi_permanent(hashfs);
 unsigned int hash2 = bi_to_int(hashfs);

 return hash == hash2;
}

void usage(char* prgname) {
 fprintf(stderr, "usage: %s <mode> target key/cert outn", strrchr(prgname, '/') + 1);
 fprintf(stderr, "%s", "example:n");
 fprintf(stderr, "%s",
   "tt -s test.txt key.txt sign.txt ttsign test.txt file with key from key.txt and put signature to sign.txtn");
 fprintf(stderr, "%s",
   "tt -v test.txt cert.txt sign.txt ttverify test.txt file with cert from cert.txt by signature from sign.txtn");
}

 

Revise this Paste

Your Name: Code Language: