Sie sind auf Seite 1von 5

1. Implement the encryption and decryption of 8-bit data using orithm (created by Prof. Edward Schaefer) in C .

#include #include #include #include class caesar { public: int i,n,x; char l,s[10],s1[10]; void encryption(); void decryption(); }; void caesar::encryption(void) { cout<<"\n enter the string to be encrypted:"; cin>>s; n=strlen(s); for(i=0;i { s[i]=s[i]+3; if(s[i]>'z') { x=s[i]-'z'; x=x-1; s[i]='a'; s[i]+=x; } s1[i]=s[i]; } cout<<"\n encrypted string is:"; for(i=0;i cout< getch(); } void caesar::decryption(void) { clrscr(); cout<<"\n enter the string to be decrypted:"; cin>>s; n=strlen(s); for(i=0;i { s[i]=s[i]-3; if(s[i]<'a') { x='a'-s[i]; x=x-1; s[i]='z'; s[i]-=x; } s1[i]=s[i]; } cout<<"\n decrypted string is:"; for(i=0;i cout<} void main() { clrscr(); caesar c; c.encryption();

Simplified DES Alg

c.decryption(); getch(); } INPUT: enter the string to be encrypted: abc OUTPUT: encrypted string is: def INPUT: enter the string to be decrypted: def OUTPUT: decrypted string is: abc RESULT: The program was successfully executed. 2.Implement Linear Congruential Algorithm to generate 5 pseudo-random numbers in C .

#include <iostream> 02 using namespace std; 03 04 int main() 05 { 06 int M = 256; 07 int a = 65; 08 int c = 27; 09 int X = 1; 10 int i; 11 for(i=0; i<256; i++) 12 { 13 X=(a * X + c) % M; 14 cout << X << " "; 15 } 16 return 0; 17

} 3 Implement Rabin-Miller Primality Testing Algorithm in * input n output the smallest k > 0 such that n+k is prime */ #include <stdio.h> #include <stdlib.h> #include <gmp.h> int n_bits; int n_primes; int prime_list[65536]; char candidates[65536]; void make_prime_list(int upper) { int i, p; prime_list[0] = 2; n_primes = 1; for (p = 3; p <= upper; p += 2) { for (i = 0; i < n_primes; i ++) if (p % prime_list[i] == 0) break; if (i >= n_primes) prime_list[n_primes ++] = p; } } /* make candidates of k: let r = n % p, than all k = pq - r is not a candidate */ void make_candidates(mpz_t n, int upper) { int i; int r; int k; mpz_t rr; mpz_init(rr); for (i = 1; i <= upper; i ++) candidates[i] = 1; candidates[0] = 0; for (i = 0; i < n_primes; i ++) { r = mpz_mod_ui(rr, n, prime_list[i]); for (k = prime_list[i] - r; k <= upper; k += prime_list[i]) candidates[k] = 0; } mpz_clear(rr); } void my_powm(mpz_t x, mpz_t a, mpz_t rr, mpz_t n) { mpz_t b; mpz_init_set(b, a); mpz_t r; mpz_init_set(r, rr); C .

mpz_set_ui(x, 1); while (mpz_cmp_ui(r, 0) != 0) { if (mpz_odd_p(r)) { mpz_mul(x, x, b); mpz_mod(x, x, n); mpz_sub_ui(r, r, 1); } mpz_fdiv_q_ui(r, r, 2); mpz_mul(b, b, b); mpz_mod(b, b, n); } mpz_clear(b); mpz_clear(r); } int primality_test(mpz_t n, int t) { int i; int j; int s; int primality; mpz_t r; mpz_t y; mpz_init(y); mpz_t n_minus_1; mpz_init(n_minus_1); mpz_t p; mpz_sub_ui(n_minus_1, n, 1); s = 0; mpz_init_set(r, n_minus_1); while (mpz_even_p(r)) { s ++; mpz_fdiv_q_ui(r, r, 2); } primality = 1; for (i = 0; i < t; i ++) { mpz_init_set_ui(p, prime_list[i]); my_powm(y, p, r, n); mpz_clear(p); if (mpz_cmp_ui(y, 1) && mpz_cmp(y, n_minus_1)) { for (j = 1; j < s && mpz_cmp(y, n_minus_1); j ++) { mpz_powm_ui(y, y, 2, n); if (!mpz_cmp_ui(y, 1)) { primality = 0; goto DONE; } } if (mpz_cmp(y, n_minus_1)) { primality = 0; goto DONE; } } } DONE: mpz_clear(r); mpz_clear(y); mpz_clear(n_minus_1);

return primality; } int main(void) { int k; int upper; mpz_t n; mpz_init(n); mpz_t m; mpz_init(m); mpz_inp_str(n, stdin, 10); n_bits = mpz_sizeinbase(n, 2); upper = n_bits > 1000 ? n_bits : 1000; make_prime_list(upper); if (mpz_cmp_ui(n, prime_list[n_primes - 1]) < 0) { int nn = mpz_get_ui(n); for (k = 0; k < n_primes; k ++) if (prime_list[k] > nn) break; printf("%d\n", prime_list[k] - nn); goto DONE; } make_candidates(n, upper); for (k = 1; k <= upper; k ++) if (candidates[k]) { mpz_add_ui(m, n, k); if (primality_test(m, 5)) { printf("%d\n", k); break; } } DONE: mpz_clear(n); mpz_clear(m); return 0; }

Das könnte Ihnen auch gefallen