Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
6f50f4a
Added a program that evaluates if two words are anagrams
maysanchez Aug 13, 2018
5236d89
Create DoublyLinkedListInsertion.c
atul161 Oct 8, 2018
82985e5
Added primes_in_range.c
the-kaustubh Oct 25, 2018
c030fd1
Update isArmstrong.c
travellers Jul 7, 2019
1e5afcc
updated README.md
SanyuktaSaha Oct 1, 2019
80997b9
updated CodingGuidelines.md
SanyuktaSaha Oct 1, 2019
89069e2
updated CodingGuidelines.md
SanyuktaSaha Oct 1, 2019
bbd54a9
updated .gitignore
SanyuktaSaha Oct 1, 2019
4cd255d
updated shellSort.c
SanyuktaSaha Oct 1, 2019
ac66bb4
Update countingSort.c
Dips2705 Oct 1, 2019
f854410
Merge pull request #1 from Dips2705/Dips2705-patch-1
Dips2705 Oct 1, 2019
f1d299b
Create binarysearch.c
mateuseap Oct 1, 2019
69a263b
7.c :reverse of a number with overflow check added
SuranjanDaw Oct 2, 2019
19c2e93
9.c :pallindrome of a number with overflow check added
SuranjanDaw Oct 2, 2019
15f4fb1
README.md updated
SuranjanDaw Oct 2, 2019
59f998e
8.c: atoi implemented(used long int)
SuranjanDaw Oct 4, 2019
cd05073
updated 7.c and 9.c
SuranjanDaw Oct 4, 2019
42d4fb5
Solution for "2. Add Two Numbers"
shubhamdp Oct 5, 2019
4d11bc0
Solution for "4. Median of Two Sorted Arrays"
shubhamdp Oct 5, 2019
439ef79
Solution for "7. Reverse Integer"
shubhamdp Oct 5, 2019
33f2893
Solution for "8. String to Integer (atoi)"
shubhamdp Oct 5, 2019
d252361
Soulution for "21. Merge Two Sorted Lists"
shubhamdp Oct 5, 2019
082f9c8
Solution for "28. Implement strStr()"
shubhamdp Oct 5, 2019
13455ac
Add Splay Tree in DS
JazzKriss21 Oct 5, 2019
f550790
added RC4 stream cipher
Cornul11 Oct 5, 2019
fdae814
updated README.md
Cornul11 Oct 5, 2019
09a763e
Merge pull request #354 from Cornul11/rc4
danghai Oct 6, 2019
2b7fcfc
Merge pull request #353 from JazzKriss21/add_splay_tree
danghai Oct 6, 2019
e063abd
Merge pull request #310 from SuranjanDaw/master
danghai Oct 6, 2019
62e9029
Format specifier for size_t, calloc check
Spikatrix Oct 6, 2019
d76ed40
Cleaned up, meaningful variable names
Spikatrix Oct 6, 2019
681bfab
+1 byte for the NUL-terminator
Spikatrix Oct 6, 2019
92b9b9e
Update BubbleSort.c
msanket9 Oct 6, 2019
067858b
Merge pull request #361 from Spikatrix/patch-2
danghai Oct 6, 2019
a6bffe0
Merge pull request #363 from msanket9/patch-1
danghai Oct 6, 2019
e4aa9a5
Merge pull request #362 from Spikatrix/patch-3
danghai Oct 6, 2019
51926d5
Merge pull request #360 from Spikatrix/patch-1
danghai Oct 6, 2019
90bb2aa
Merge branch 'master' into master
shubhamdp Oct 7, 2019
58a77bd
Adding recusrsive approach to "21. Merge Two Sorted Lists"
shubhamdp Oct 7, 2019
18181fc
Merge pull request #349 from shubhamdpatil/master
danghai Oct 7, 2019
9613909
Merge pull request #274 from SanyuktaSaha/patch-3
danghai Oct 7, 2019
6b0277f
Merge pull request #271 from SanyuktaSaha/master
danghai Oct 7, 2019
1b2e7ec
Fixed bad link in leetcode readme
freddy2659 Oct 7, 2019
4c624a2
Add more solution for leetcode
freddy2659 Oct 7, 2019
35ddd30
Updated leetcode Readme
freddy2659 Oct 7, 2019
c76924f
Merge pull request #3 from jonohein/patch-2
freddy2659 Oct 7, 2019
6da83f9
Merge pull request #2 from jonohein/patch-3
freddy2659 Oct 7, 2019
150962b
Merge pull request #1 from jonohein/patch-4
freddy2659 Oct 7, 2019
3ae2ca0
Update binarysearch.c
mateuseap Oct 7, 2019
87a21ff
Update 5.c
freddy2659 Oct 7, 2019
8c9e593
Update 5.c
freddy2659 Oct 7, 2019
6dc8e6e
Update 5.c
freddy2659 Oct 7, 2019
8ebe4f2
Merge pull request #370 from jonohein/master
danghai Oct 7, 2019
c30a409
Replace gets with fgets
ashwek Oct 8, 2019
426fa28
Merge pull request #138 from maysanchez/master
ashwek Oct 8, 2019
988861f
Update binarysearch.c
mateuseap Oct 8, 2019
9e27c3e
Add header files
ashwek Oct 8, 2019
b3bc8b9
Merge pull request #173 from atul161/master
ashwek Oct 8, 2019
2ffc698
format code (add even spaces)
ashwek Oct 8, 2019
949c164
Merge pull request #195 from the-kaustubh/primes_in_range
ashwek Oct 8, 2019
06ca515
format code
ashwek Oct 8, 2019
606817b
Merge pull request #240 from travellers/patch-1
ashwek Oct 8, 2019
fab3022
Update .gitignore
ashwek Oct 8, 2019
a53354c
Merge pull request #272 from SanyuktaSaha/patch-1
ashwek Oct 8, 2019
5163859
Merge pull request #289 from mateuseap/patch-2
ashwek Oct 8, 2019
7bd4ffc
Merge pull request #276 from Dips2705/master
ashwek Oct 8, 2019
52661dd
Create bubblesort.cpp
soyo-kaze Oct 15, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.swp
*.out
*.log
*.tmp
7 changes: 5 additions & 2 deletions CodingGuidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ What want the program an user informations?

## Code style conventions

See [here](https://users.ece.cmu.edu/~eno/coding/CCodingStandard.html)
Don't push all code in one line!
See [here](https://users.ece.cmu.edu/~eno/coding/CCodingStandard.html)

Don't push all code in one line!
Do use comments to make it more readable and understandable.
use proper names for variables used in program.
Do use functions for consise coding.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@ C
- decimal_to_hexa
- decimal_to_octal
- to_decimal
- octal_to_decimal
- octal_to_binary
- octal_to_hexa
- binary_to_octal
- binary_to_hexa

## Data Structures
- stack
- queue
- array
- dictionary
- linked_list
- singly_link_list_deletion
- stack_using_linkedlists
- binary_trees
- create_node
- recursive_traversals
- trie
- tree
- trie
- graphs


## Searching
Expand All @@ -44,6 +51,10 @@ C
- Fibonacci_Search
- Interpolation_Search
- Modified_Binary_Search
- Hash_search
- Binary_tree_search
- Interval_search
- Exponential_search


## Sorting
Expand Down Expand Up @@ -71,9 +82,13 @@ C
- xor8 (8 bit)
- adler_32 (32 bit)

## Cryptography
- RC4


## Misc
- Binning
- Prime
- Primes in range
- Factorial
- Fibonacci
- isArmstrong
Expand Down
189 changes: 189 additions & 0 deletions cipher/rc4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <argp.h>
#include <argz.h>
#include <unistd.h>

uint8_t S[256];
uint8_t T[256];
int globalI = 0, globalJ = 0;

/* Program documentation. */
static char doc[] = "A program implementing the RC4 encryption/decryption algorithm";

/* A description of the arguments we accept. */
static char args_doc[] = "KEY";

/* The options we understand. */
static struct argp_option options[] = {
{"decrypt", 'd', 0, 0, "Decrypt the given data"},
{"encrypt", 'e', 0, 0, "Encrypt the given data"},
{"output", 'o', "FILE", 0, "Output to FILE instead of standard output"},
{0}
};

/* Used by main to communicate with parse_opt. */
struct arguments {
char *key; /* KEY */
char *output_file;
int encrypt, decrypt;
};

/* Parse a single option. */
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments *arguments = state->input;

switch (key) {
case 'd':
arguments->decrypt = 1;
break;
case 'e':
arguments->encrypt = 1;
break;
case 'o':
if (arg == NULL) {
arguments->output_file = "-";
} else {
arguments->output_file = arg;
}
break;
case ARGP_KEY_ARG:
// it requires exactly one argument, the key
if (state->arg_num > 1) {
argp_usage(state);
}
arguments->key = arg;
break;
case ARGP_KEY_END:
// Not enough arguments.
if (state->arg_num < 1) {
argp_usage(state);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}

/* Our argp parser. */
static struct argp argp = {options, parse_opt, args_doc, doc};

// swaps two values in an array
void swapValues(uint8_t *a, uint8_t i, uint8_t j) {
uint8_t tmp = a[j];
a[j] = a[i];
a[i] = tmp;
}

// initializes the permutation in the array S
void initializeVariables(char *key) {
// key-scheduling algorithm

int keyLen = strlen(key);
for (int i = 0; i < 256; i++) {
S[i] = (char) i;
T[i] = key[i % keyLen];
}

int j = 0;
for (int i = 0; i < 256; i++) {
j = (j + S[i] + T[i]) % 256;
swapValues(S, i, j);
}
}

// the RC4 algorithm
uint8_t rc4Output(const uint8_t data) {
uint8_t outputBuf;
// pseudo random generation algorithm
globalI = (globalI + 1) % 256;
globalJ = (globalJ + S[globalI]) % 256;

swapValues(S, globalI, globalJ);

uint8_t t = S[(S[globalI] + S[globalJ]) % 256];

outputBuf = t ^ data; // the main operation, XOR

return outputBuf;
}

int main(int argc, char **argv) {
struct arguments arguments;

/* Default values. */
arguments.output_file = "-";
arguments.decrypt = 1; // by default it will output data as decryption
arguments.encrypt = 0;

/* Parse our arguments; every option seen by parse_opt will
be reflected in arguments. */
argp_parse(&argp, argc, argv, 0, 0, &arguments);

initializeVariables(arguments.key);

uint8_t buf[1]; // temporary buffer for reading the byte
int totalBytes = 0; // total read bytes
uint8_t *outputData = malloc(sizeof(uint8_t) * 0);

// skipping the first 256 bytes of the stream
for (int i = 0; i < 256; i++) {
rc4Output(0);
}

// reading every byte,
while (read(0, buf, sizeof(buf)) > 0) {
totalBytes++;
outputData = realloc(outputData, totalBytes);
// add every new byte to the newly reallocated array of en(de)crypted bytes
outputData[totalBytes - 1] = rc4Output(buf[0]);
}

// killing the last byte to avoid memory leaks and illegal memory accesses
outputData = realloc(outputData, --totalBytes);

if (arguments.encrypt == 1) {
if (strcmp(arguments.output_file, "-") == 0) { // printing to stdin if no file name specified
printf("Encrypted text:\n");
for (int i = 0; i < totalBytes; i++) {
printf("%02hhX", outputData[i]);
}
} else {
FILE *filePointer;
filePointer = fopen(arguments.output_file, "w");

if (filePointer == NULL) {
fprintf(stderr, "Unable to create file %s.\n", arguments.output_file);
exit(EXIT_FAILURE);
}
fputs(outputData, filePointer);
printf("File successfully encrypted to %s.\n", arguments.output_file);
fclose(filePointer);
}
} else if (arguments.decrypt == 1) {
if (strcmp(arguments.output_file, "-") == 0) { // printing to stdin if no file name specified
printf("Decrypted text:\n");
for (int i = 0; i < totalBytes - 1; i++) {
printf("%c", outputData[i]);
}
} else {
FILE *filePointer;
filePointer = fopen(arguments.output_file, "w");

if (filePointer == NULL) {
fprintf(stderr, "Unable to create file %s.\n", arguments.output_file);
exit(EXIT_FAILURE);
}
fputs(outputData, filePointer);
printf("File successfully decrypted to %s.\n", arguments.output_file);
fclose(filePointer);
}
}
// avoid memory leaks
free(outputData);
return 0;
}
Loading