Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
16 kB
1
Indexable
Never
//railfence
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char *plainTextToCipherText(char plainText[],int n)
{
    int i,j,counter,limit,index=0,len;
    char *cipherText;
    len=strlen(plainText);
    cipherText=(char*)malloc(sizeof(char)*(len+1));
    for(i=0;i<n;i++)
 {
  counter=0;
  for(j=i;j<len;j+=limit)
  {
   cipherText[index++]=plainText[j];
   if(i==0 || i==n-1)
       limit=2*n-2;
   else if(counter%2==0)
    limit=2*(n-i-1);
   else
    limit=2*i;
   if(limit<=0)
       break;
   counter++;
  }
 }
 cipherText[index]='\0';
 return cipherText;
}
int main()
{
 int n;
 char plainText[100];
 printf("Enter the plain text : ");
 scanf("%s",plainText);
 printf("Enter the value of n (key) : ");
 scanf("%d",&n);
    printf("%s\n",plainTextToCipherText(plainText,n));
 return 0;
}

/*
#include<stdio.h>
#include<string.h>
void main()
{
int i,j,k,l;
char a[20],c[20],d[20];
printf("\n\t\t RAIL FENCE TECHNIQUE");
printf("\n\nEnter the input string : ");
gets(a);
l=strlen(a);
/*Ciphering*/
for(i=0,j=0;i<l;i++)
{
if(i%2==0)
c[j++]=a[i];
}
for(i=0;i<l;i++)
{
if(i%2==1)
c[j++]=a[i];
}
c[j]='\0';
printf("\nCipher text after applying rail fence :");
printf("\n%s",c);
/*Deciphering*/
if(l%2==0)
k=l/2;
else
k=(l/2)+1;
for(i=0,j=0;i<k;i++)
{
d[j]=c[i];
j=j+2;
}
for(i=k,j=1;i<l;i++)
{
d[j]=c[i];
j=j+2;
}
d[l]='\0';
printf("\nText after decryption : ");
printf("%s",d);
}
*/

-------------------------------------------------------------------------------------------------------------------

//vignere
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char* encrypt(char* message, char* key);
char* decrypt(char* message, char* key);

int main()
{
    char message[1000], key[1000], *encrypted, *decrypted;
    int choice;

    printf("Enter message: ");
    fgets(message, 1000, stdin);
    printf("Enter key: ");
    fgets(key, 1000, stdin);

    // remove newline characters from input
    message[strcspn(message, "\n")] = '\0';
    key[strcspn(key, "\n")] = '\0';

    printf("Enter your choice:\n");
    printf("1. Encrypt\n");
    printf("2. Decrypt\n");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            encrypted = encrypt(message, key);
            printf("Encrypted message: %s\n", encrypted);
            free(encrypted);
            break;
        case 2:
            decrypted = decrypt(message, key);
            printf("Decrypted message: %s\n", decrypted);
            free(decrypted);
            break;
        default:
            printf("Invalid choice!\n");
            break;
    }

    return 0;
}

char* encrypt(char* message, char* key)
{
    char* encrypted = (char*)malloc(strlen(message) + 1);
    int messageLength = strlen(message), keyLength = strlen(key), i, j;

    for (i = 0, j = 0; i < messageLength; ++i, ++j) {
        if (j == keyLength)
            j = 0;

        // add key value to message character
        encrypted[i] = ((toupper(message[i]) - 'A' + toupper(key[j]) - 'A') % 26) + 'A';
    }

    encrypted[i] = '\0';

    return encrypted;
}

char* decrypt(char* message, char* key)
{
    char* decrypted = (char*)malloc(strlen(message) + 1);
    int messageLength = strlen(message), keyLength = strlen(key), i, j;

    for (i = 0, j = 0; i < messageLength; ++i, ++j) {
        if (j == keyLength)
            j = 0;

        // subtract key value from message character
        decrypted[i] = ((toupper(message[i]) - 'A' - (toupper(key[j]) - 'A') + 26) % 26) + 'A';
    }

    decrypted[i] = '\0';

    return decrypted;
}

-------------------------------------------------------------------------------------------------------------------------

//diffie
#include<stdio.h>
#include<conio.h>
#include<math.h>
// global parameter
int p = 11, g = 2;
int a, b;

int main()
{
 int x, y, ssk1, ssk2;

 printf("Alice Choice: ");
 scanf("%d", &x);
 printf("Bob Choice: ");
 scanf("%d", &y);
 // Alice
 a = (long int) pow(g, x) % p;
 // Bob
 b = (long int) pow(g, y) % p;

 // SSK for ALICE
 ssk1 = (long int) pow(b, x) % p;
 // SSK for BOB
 ssk2 = (long int) pow(a, y) % p;
 printf("SSK1 = %d and SSK2 = %d", ssk1, ssk2);
 getch();
 return 0;
}

-------------------------------------------------------------------------------------------------------------

//RSA
#include<stdio.h>
#include<conio.h>
#include<math.h>
// 1. select p and q
// 2. n = p*q
// 3. phi = (p-1)*(q-1)
// 4. choose public key e such that 1<e<phi, gcd(e, phi)=1

// 5. calculate privat key: e*d = 1 mod phi

// 6. Encryption: c = m^e mod n -- m message, c cipher
// 7. Decryption: m = c^d mod n

long long int p = 7, q = 11;

long long int gcd(long long int, long long int);
//long int mypow(long int, long int);

int main()
{
 long long int n, phi, e, d;
 long long int m = 2, c;
 //2
 n = p*q; // 77

 //3 Euler toitent function
 phi = (p-1)*(q-1);  // 60

 //4. choose public key
 p:
 printf("Enter public component: ");
 scanf("%lld", &e);  // 13

 // range 1<e<phi, gcd(e,phi)==1
 // printf("GCD = %ld", gcd(phi, e));
 // printf("GCD = %ld\n", gcd(phi, e));

 if(!(e>1 && e<phi && gcd(phi,e)==1))
 {
  printf("Public component is not selected properly.\n");
  goto p;
 }

 //printf("Okay!");

 // 5 find private key e * d = 1 mod phi

 // Simple searching strategy
 // In real implementation use proper algorithm.
 d = 2;
 while(e*d % phi != 1)
 {
  // printf("checking = %ld\n", d);
  d++;
 }

 printf("\nPrivate Key = %ld\n", d);

 // 6. Encryption

 //printf("\nm^e = %lld\n", (long long int) pow(m, e));

 c = (long long int) pow(m, e) % n;
 printf("\nCipher: %lld\n", c);

 // 7. Decryption
 //printf("\nc^d = %lld\n", (long int)pow(c,d));

 m = (long long int) pow(c, d) % n;
 printf("\nPlain: %lld\n", m);


 getch();
 return 0;
}

long long int gcd(long long int a, long long int b)
{
 long long int q, r, tmp;
 /* use if required

 if (b>a)
 {
  tmp = a;
  a = b;
  b = tmp;
 }

 */

 while (b!=0)
 {
  q = a/b;
  r = a%b;
  a = b;
  b = r;
 }
 return a;
}

/*
long int mypow(long int a, long int b)
{
 long int p=1, i;
 for(i=1;i<b;i++)
 {
  p *= a;
 }
 return p;
}
*/

------------------------------------------------------------------------------------------------------

//caesar 
#include<stdio.h>

int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to encrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
}
printf("Encrypted message: %s\n", message);


//decrypt
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch < 'A'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
printf("Decrypted message: %s", message);

return 0;
}

---------------------------------------------------------------------------------------------------------------

//MONOALPHABATIC CIPHER
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
            char pt[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
            char ct[26]={'Z','Y','X','W','V','U','T','S','R','Q','P','O','N','M','L','K','J','I','H','G','F','E','D','C','B','A'};
            char p[20]={'\0'},c[20]={'\0'},r[20]={'\0'};
            int i,j;
            printf("\n enter the plain text:");
            gets(p);
            //converting plain text into cipher text (encryption)
            for(i=0;i<strlen(p);i++)
            {
                        for(j=0;j<26;j++)
                        {
                        if(pt[j]==p[i])
                        {
                                    c[i]=ct[j];
                        }
                        }
            }
            printf("\n cipher text is: %s",c);

            //converting cipher text into plain text (decryption)
            for(i=0;i<strlen(c);i++)
            {
                        for(j=0;j<26;j++)
                        {
                        if(ct[j]==c[i])
                        {
                                    r[i]=pt[j];
                        }
                        }
            }
            printf("\n \n plain text is: %s",r);
            getch();
}

--------------------------------------------------------------------------------------------------------------

//euclidean
#include<stdio.h>
#include<conio.h>

int main()
{
 int a, b, q, r, t;
 clrscr();
 printf("Enter number a: ");
 scanf("%d", &a);
 printf("Enter number b: ");
 scanf("%d", &b);
 // large case
 if (b>a)
 {
  t = a;
  a = b;
  b = t;
 }
 printf("q\ta\tb\tr\n");

 do
 {
  q = a/b;
  r = a%b;
  printf("%d\t%d\t%d\t%d\n", q, a, b, r);
  a = b;
  b = r;
  if(b==0)
  {
   printf("%d\t%d\t%d\t%d\n", q, a, b, r);
  }
 }while(b!=0);

 printf("GCD = %d", a);
 getch();
 return 0;
}

----------------------------------------------------------------------------------------------------------

//extended euclidean
#include<stdio.h>
#include<conio.h>

int main()
{
 int a, b, q, r, temp, s1=1, s2=0, s, t1=0, t2=1, t, N;
 clrscr();
 printf("Enter number a: ");
 scanf("%d", &a);
 printf("Enter number b: ");
 scanf("%d", &b);

 // large case
 if (b>a)
 {
  temp = a;
  a = b;
  b = temp;
 }

 N = a;

 printf("q\ta\tb\tr\ts1\ts2\ts\tt1\tt2\tt\n");

 do
 {
  q = a/b;
  r = a%b;
  s = s1 - q*s2;
  t = t1 - q*t2;
  printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", q, a, b, r, s1, s2, s, t1, t2, t);

  a = b;
  b = r;

  s1 = s2;
  s2 = s;

  t1 = t2;
  t2 = t;

  if(b==0)
  {
   printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", q, a, b, r, s1, s2, s, t1, t2, t);
  }
 }while(b!=0);

 printf("GCD = %d\n", a);

 //multiplicative inverse (t1)
 t1>0 ? printf("Multiplicative inverse: %d", t1): printf("Multiplicative inverse: %d", t1+N);

 getch();
 return 0;
}

------------------------------------------------------------------------------------------------------------

//Relatively prime
#include<stdio.h>
#include<conio.h>

int main()
{
 int a, b, q, r, t;
 printf("Enter number a: ");
 scanf("%d", &a);
 printf("Enter number b: ");
 scanf("%d", &b);
 // large case
 if (b>a)
 {
  t = a;
  a = b;
  b = t;
 }
 printf("q\ta\tb\tr\n");

 do
 {
  q = a/b;
  r = a%b;
  printf("%d\t%d\t%d\t%d\n", q, a, b, r);
  a = b;
  b = r;
  if(b==0)
  {
   printf("%d\t%d\t%d\t%d\n", q, a, b, r);
  }
 }while(b!=0);

 printf("GCD = %d\n", a);

 if(a==1)
 {
  printf("Given numbers are relatively prime.");
 }
 else
 {
  printf("Given numbers are not relatively prime.");
 }

 getch();
 return 0;
}

------------------------------------------------------------------------------------------------------------------

//primtive root
#include<stdio.h>
#include<conio.h>
#include<math.h>

int check_unique(int t[100], int n)
{
 int i, j, flag = 1;
 for(i=0;i<n;i++)
 {
  for(j=i+1; j<n; j++)
  {
   if(t[i] == t[j])
   {
    flag = 0;
    break;
   }
  }
 }
 return flag;
}

int main()
{
 int m, n, i, t[100];
 printf("Enter number: ");
 scanf("%d", &n);
 printf("Enter modulo: ");
 scanf("%d", &m);

 // logic
 for(i=1;i<m; i++)
 {
  t[i-1] = (long int) pow(n, i) % m;
 }

 // display value
 for(i=0;i<m-1;i++)
 {
  printf("%d\t", t[i]);
 }

 // check
 check_unique(t, m-1)==1 ? printf("\nYES"):printf("\nNO");

 getch();
 return 0;
}

---------------------------------------------------------------------------------------------------------

//elagamal
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int is_prime(int num) {
    int i;
    for (i = 2; i <= sqrt(num); i++) {
        if (num % i == 0) {
            return 0;
        }
    }
    return 1;
}

int generate_prime(int min, int max) {
    int num = min + rand() % (max - min);
    while (!is_prime(num)) {
        num++;
    }
    return num;
}

int power(int a, int b, int p) {
    int result = 1;
    while (b > 0) {
        if (b % 2 == 1) {
            result = (result * a) % p;
        }
        a = (a * a) % p;
        b /= 2;
    }
    return result;
}

int main() {
    int p, g, x, y, k, m, r, e, d;

    printf("Enter a prime number p: ");
    scanf("%d", &p);

    while (!is_prime(p)) {
        printf("p is not prime. Enter a prime number p: ");
        scanf("%d", &p);
    }

    printf("Enter a generator g: ");
    scanf("%d", &g);

//    while (g <= 1 || g >= p - 1 || power(g, (p - 1) / 2, p) != 1) {
//        printf("g is not a generator. Enter a generator g: ");
//        scanf("%d", &g);
//    }

    printf("Enter a private key x: ");
    scanf("%d", &x);

    printf("Enter a message m: ");
    scanf("%d", &m);

    y = power(g,x,p);
    k = generate_prime(2, p - 2);
    r = power(g, k, p);
    e = (m * power(y, k, p)) % p;
    d = (e * power(r, p - 1 - x, p)) % p;

    printf("Public key (p, g, y) = (%d, %d, %d)\n", p, g, y);
    printf("Private key x = %d\n", x);
    printf("Message m = %d\n", m);
    printf("Random integer k = %d\n", k);
    printf("Encrypted message (r, e) = (%d, %d)\n", r, e);
    printf("Decrypted message d = %d\n", d);

    return 0;
}

---------------------------------------------------------------------------------------------------------------

//miller rabin
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// function to compute (base^exponent) modulo modulo
int modularExponentiation(int base, int exponent, int modulo)
{
    int result = 1;
    while (exponent > 0) {
        if (exponent % 2 == 1) {
            result = (result * base) % modulo;
        }
        base = (base * base) % modulo;
        exponent /= 2;
    }
    return result;
}

// function to test if n is prime using Miller-Rabin test with k iterations
int millerRabin(int n, int k)
{
    if (n == 2 || n == 3) {
        return 1;
    }
    if (n == 1 || n % 2 == 0) {
        return 0;
    }

    // write n-1 as (2^r)*d where d is odd
    int r = 0, d = n - 1;
    while (d % 2 == 0) {
        ++r;
        d /= 2;
    }

    int i, j;
    for (i = 0; i < k; ++i) {
        int a = rand() % (n - 3) + 2; // pick a random number in [2, n-2]
        int x = modularExponentiation(a, d, n);
        if (x == 1 || x == n - 1) {
            continue;
        }
        for (j = 1; j < r; ++j) {
            x = (x * x) % n;
            if (x == n - 1) {
                break;
            }
        }
        if (j == r) {
            return 0;
        }
    }
    return 1;
}

int main()
{
    int n, k;

    printf("Enter the number to be tested: ");
    scanf("%d", &n);

    printf("Enter the number of iterations: ");
    scanf("%d", &k);

    srand(time(NULL)); // seed the random number generator with the current time

    if (millerRabin(n, k)) {
        printf("%d is probably prime.\n", n);
    } else {
        printf("%d is composite.\n", n);
    }

    return 0;
}