[BACK]Return to ntlrsatest.cpp CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_ntl / crypt / rsa

Annotation of OpenXM/src/ox_ntl/crypt/rsa/ntlrsatest.cpp, Revision 1.2

1.2     ! iwane       1: /* $OpenXM: OpenXM/src/ox_ntl/crypt/rsa/ntlrsatest.cpp,v 1.1 2004/05/16 15:04:54 iwane Exp $ */
1.1       iwane       2:
                      3: #include "ntlrsa.h"
                      4: #include <stdio.h>
                      5: #include <string.h>
                      6:
1.2     ! iwane       7: static void
        !             8: enctest(NtlRsa *rsa, const char *C)
        !             9: {
        !            10:        unsigned char code[1024];
        !            11:        unsigned char buf[1024];
        !            12:        int a1 = rsa->encryptByPublicKey(code, C);
        !            13:        int a2 = rsa->decryptByPrivateKey(buf, code);
        !            14:
        !            15:        printf("<%2d>: [%s] ==> [%s][%02x %02x %02x %02x %02x]", strncmp(C, (char *)buf, a1), C, buf, buf[0], buf[1], buf[2], buf[3], buf[4]);
        !            16:        printf("%d %d %d\n", a1, a2, strlen(C));
        !            17:        if (a1 < 0 || a2 < 0) {
        !            18:                printf("error\n");
        !            19:                exit(-1);
        !            20:        }
        !            21:
        !            22:        a1 = rsa->encryptByPrivateKey(code, C);
        !            23:        a2 = rsa->decryptByPublicKey(buf, code);
        !            24:        printf("<%2d>: [%s] ==> [%s]", strncmp(C, (char *)buf, a2), C, buf);
        !            25:        printf("\t\t%d %d\n", a1, a2);
        !            26:        if (a1 < 0 || a2 < 0) {
        !            27:                printf("error\n");
        !            28:                exit(-1);
        !            29:        }
        !            30: }
        !            31:
1.1       iwane      32: /*
                     33:  * example: NtlRsa
                     34:  */
                     35: int
                     36: main(int argc, char *argv[])
                     37: {
                     38:        NtlRsa rsa;
                     39:        char msg[] = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%$#FRTJ$JGJSDJJEWRAQJRJEWJ\"J$J#"
                     40:                        "fhdsjkahflsjdkhaflkjhdslfkajhflkjh32ruihf\n4hkjhdsajklfhlskajhflksjhdflkjshalfjkhsdfkjhjsh"
                     41:                        "fdsajfdksjafksdajf\tfsajkhlfkjhsalkjfhdfdsahj hlkjahfld sj flksjdhflkjhasdlfkjhasldjfhalksjh";
                     42:        unsigned char code[1024];
                     43:        unsigned char buf[1024];
                     44:        int a1, a2;
                     45:
                     46:        // keygen(seed, bit)
                     47:        rsa.keygen(0, 512);
                     48:        cout << rsa << endl;
                     49:
                     50:
                     51:
                     52:        for (int i = 0; i < argc; i++) {
1.2     ! iwane      53:                enctest(&rsa, argv[i]);
1.1       iwane      54:        }
                     55:
                     56:        char *ptr = msg;
                     57:        unsigned char *p = code;
                     58:        int total = strlen(msg) + 1;
                     59:        int xxx;
                     60:
                     61:        for (xxx = 0; total > 0; xxx++) {
                     62:                a1 = rsa.encryptByPublicKey(p, ptr);
                     63:                if (a1 < 0) {
                     64:                        printf("encrypt error\n");
                     65:                        break;
                     66:                }
                     67:
                     68:                ptr += a1;
                     69:                p += rsa.getModLen();
                     70:
                     71:                total -= a1;
                     72:        }
                     73:
                     74:        cout << "total = " << total << endl;
                     75:        cout << "xxx = " << xxx << endl;
                     76:        p = buf;
                     77:
                     78:        for (int lll = 0; lll < xxx; lll++) {
                     79:
                     80:                a2 = rsa.decryptByPrivateKey(p, code + lll * rsa.getModLen());
                     81:                if (a2 < 0) {
                     82:                        printf("decrypt error: %d, %d\n", a2, lll);
                     83:                        break;
                     84:                }
                     85:
                     86:                p += a2;
                     87:        }
                     88:
1.2     ! iwane      89:        cout << "decrypt: " << strcmp((char *)buf, msg) << endl;
1.1       iwane      90: }
                     91:
                     92:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>