[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.1

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

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