"Karioj" Discrete math Spring semester programming Test 1

Source: Internet
Author: User

A. Caesar Password Test Instructions:

Give you k1,k2, and a string of plaintext, a bunch of ciphertext.

PlainText is encrypted with K1, ciphertext is decrypted with K2.

For clear text to convert the letters into uppercase letters, all non-letters are deleted.

Amount: To take into account that the modulus may become negative, so add 26 and then modulo. Code:
#include <stdio.h> #define N 85int K1, K2;char plain[n], cipher[n];void init (char s[])//preprocessing, converted to uppercase {    int i;    for (i = 0; s[i]; i++)        if (S[i] >= ' A ' && s[i] <= ' z ')            s[i] = s[i]-' a ' + ' a ';} void Encrypt (char s[],int k,int f)//Plus decryption {    init (s);    int i;    for (i = 0; s[i]; i++)        if (s[i] >= ' A ' && s[i] <= ' Z ')            printf ("%c", ((S[i]-' A ' + k*f + +)%  + ' A '));    printf ("\ n");} int main () {    scanf ("%d,%d", &k1, &K2);    Gets (plain);    Gets (cipher);    Encrypt (plain,k1,1);    Encrypt (cipher,k2,-1);    return 0;}
B. RSA Encryption Test Instructions:

Give you n,e, and a bunch of clear text. encrypt the plaintext with (n,e). Converts the plaintext letter to a number, divided by 8 digits, and the less part complements 0. There are non-letter deletions in the plaintext, a and a turn into numbers, and Z and Z turn into numbers of 25. Clear text number 8 bits of each paragraph corresponding to the ciphertext is also required to be 8 bits, if less than 8 bits, the front top up 0.

For clear text to convert the letters into uppercase letters, all non-letters are deleted.

Add:

RSA encryption is the conversion of letters to two-digit, segmented processing, such as every eight paragraphs, M is the clear-Text Number field, C for the ciphertext field, c=me%n.

Code:
#include <stdio.h> #include <string.h> #define N 1000#define ll long longll N, E;char plain[n];ll Qpow (ll A, LL b)    Fast Power {ll k = a% n;    ll ans = 1;        while (b) {if (b & 1) ans = (ans * k)% n;        K = (k * k)% n;    b >>= 1; } return ans;    void Init (char s[])//preprocessing, converted to uppercase {int i; for (i = 0; s[i]; i++) if (S[i] >= ' A ' && s[i] <= ' z ') s[i] = s[i]-' a ' + ' a ';}    void Encrypt (char s[])//encryption {init (s);    int k = 0,i;    ll block = 0; for (i = 0; s[i]; i++) if (s[i] >= ' A ' && s[i] <= ' Z ')//If it is the letter {block = block * 100 + (S[i]-' A ');            Clear text corresponding to the number string k++;                if (k = = 4)//enough 8 digits {printf ("%08lld", QPOW (block, e)% 100000000);//output ciphertext, this module does not know whether it is necessary, the title does not say the upper limit of n                block = 0;            k = 0;          }} if (k)//The remaining plaintext is to be followed by 0 {while (k! = 4) {block = block * 100;  k++;    } printf ("%08lld", QPOW (block, e)% 100000000);    }}int Main () {//Freopen ("In.txt", "R", stdin);    scanf ("%lld%lld", &n, &e);    Gets (plain);    Encrypt (plain); return 0;}
C. RSA Decryption Test Instructions:

Give you n,e, and a bunch of clear text. Encrypt the plaintext with (n,e). Converts the plaintext letter to a number, divided by 8 digits, and the less part complements 0. There are non-letter deletions in the plaintext, a and a turn into numbers, and Z and Z turn into numbers of 25. Clear text number 8 bits of each paragraph corresponding to the ciphertext is also required to be 8 bits, if less than 8 bits, the front top up 0.

For clear text to convert the letters into uppercase letters, all non-letters are deleted.

Add:

RSA encryption is the conversion of letters to two-digit, segmented processing, such as every eight paragraphs, M is the clear-Text Number field, C for the ciphertext field, c=me%n.

Code:
#include <stdio.h> #include <string.h> #define N 802#define ll long longll N, E, p, Q, D, X, Y;char cipher[n <& Lt        1], Plain[10];ll EXGCD (ll A, ll b)//Expand Euclid to seek the inverse of the meta {if (b = = 0) {x = 1;        y = 0;    return A;    } ll R = EXGCD (b, a% B);    ll tmp = x;    x = y;    y = tmp-a/b * y; return r;}    ll Qpow (ll A, ll b)//fast Power {ll k = a% n;    ll ans = 1;        while (b) {if (b & 1) ans = (ans * k)% n;        K = (k * k)% n;    b >>= 1; } return ans;    void init ()//ask P and Q, and d{int i;            for (i = 2; i < n; i++) if (n% i = = 0) {p = i;            Q = n/i;        Break    } ll M;    M = (p-1) * (q-1);    EXGCD (E, M);    d = (x% m + m)% m;//the inverse of e d}void decrypt ()//decryption {LL block = 0;    int k = 0, I, J;        for (i = 0; cipher[i]; i++) {block = block * + cipher[i]-' 0 ';//ciphertext is removed from the string to k++; if (k = = 8)//ciphertext reaches 8 digits {memset (plain, 0, sizEOF plain);//Clear Clear text string block = QPOW (block, d);//calculation of the plaintext number string for (j = 3; J >= 0; j--)//Calculate two digits each time and there is a clear text string, Because it is taken from the back, it is stored upside down {plain[j] = block% + ' A ';//take the back two digits block/= 100;//Remove the last two numbers Word} printf ("%s", plain);//output plaintext K = 0;//Empty counter}}}int main () {//Freopen ("in    . txt "," R ", stdin);    scanf ("%lld%lld", &n, &e);    Gets (cipher);    Init ();    printf ("%d\n", D);    Decrypt (); return 0;}

"Karioj" Discrete math Spring semester programming Test 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.