aboutsummarylogtreecommitdiffstats
path: root/rc4.c
diff options
context:
space:
mode:
authorAntony Kellermann2018-02-28 17:46:09 -0500
committerAntony Kellermann2018-02-28 17:46:09 -0500
commitd8f3a30dedd3ff3bc6a00f19a377917cbe3c292d (patch)
tree49c39ec29f48bdf937358842b8ef2db911b50021 /rc4.c
parent5e54eae4f7f6409103d36c0bb8ff0df23ca1d72d (diff)
downloadaur-d8f3a30dedd3ff3bc6a00f19a377917cbe3c292d.tar.gz
Overhaul to replace many instances of char* and len pairs with Strings instead, as well as moving functions to appropriate files
Diffstat (limited to 'rc4.c')
-rw-r--r--rc4.c72
1 files changed, 68 insertions, 4 deletions
diff --git a/rc4.c b/rc4.c
index 691abf3de7f5..ab9f59d54321 100644
--- a/rc4.c
+++ b/rc4.c
@@ -51,7 +51,7 @@ char* rc4_prga(int keySchedule[KEY_SCHEDULE_LENGTH], size_t len) {
fprintf(stderr, "malloc() failed\n");
return NULL;
}
- for (int k = 0; k < (int)len; k++) {
+ for (int k = 0; k < (int) len; k++) {
i = (i + 1) % KEY_SCHEDULE_LENGTH;
j = (j + keySchedule[i]) % KEY_SCHEDULE_LENGTH;
temp = keySchedule[i];
@@ -62,7 +62,71 @@ char* rc4_prga(int keySchedule[KEY_SCHEDULE_LENGTH], size_t len) {
return output;
}
-void rc4_execute(char* output, char* message, size_t len) {
- for (int i = 0; i < (int)len; i++)
- output[i] ^= message[i];
+void rc4_execute(char* output, String* pString) {
+ for (int i = 0; i < (int) pString->len; i++)
+ output[i] ^= pString->data[i];
+}
+
+char* rc4_encode_string(String* pString, char* password){
+ int keySchedule[256];
+ rc4_key_exchange(keySchedule, password);
+ char* output = rc4_prga(keySchedule, pString->len);
+ rc4_execute(output, pString);
+ return output;
+}
+
+String* rc4_get_crypted_string(String* input_pString, char* password, int option) {
+ String* output_pString = NULL;
+ int free_pw = (password == NULL);
+ Json* jobj = json_tokener_parse(input_pString->data);
+ if (option == ENCRYPT && jobj == NULL) // If trying to encrypt an encrypted portfolio
+ printf("Your portfolio is already encrypted.\n");
+ else if (option == DECRYPT && jobj != NULL) // If trying to decrypt an unencrypted portfolio
+ printf("Your portfolio isn't encrypted.\n");
+ else {
+ if (password == NULL) { // Get password if not provided
+ password = rc4_getPassword();
+ if (option == ENCRYPT) { // When encrypting, ask to enter pw twice to make sure
+ printf("You will be asked to enter your password again to make sure the entries match.\n");
+ sleep(2);
+ char* passwordCheck = rc4_getPassword();
+ if (strcmp(password, passwordCheck) != 0) {
+ printf("Passwords do not match!\n");
+ free(password);
+ free(passwordCheck);
+ json_object_put(jobj);
+ return NULL;
+ }
+ free(passwordCheck);
+ }
+ }
+ output_pString = string_init(); // Set ouput string to en/decrypted data
+ free(output_pString->data);
+ output_pString->data = rc4_encode_string(input_pString, password);
+ output_pString->len = input_pString->len;
+ if (option == DECRYPT) {
+ json_object_put(jobj);
+ output_pString->data = realloc(output_pString->data, output_pString->len + 1);// Realloc to add null terminator for json parsing
+ if (output_pString->data == NULL) { // Dealing with len is annoying so it's easier to just realloc
+ fprintf(stderr, "realloc() failed\n");
+ exit(EXIT_FAILURE);
+ }
+ output_pString->data[output_pString->len] = '\0'; // Null terminate string for parsing
+ jobj = json_tokener_parse(output_pString->data);
+ if (jobj == NULL) { // If after decrypting, the portfolio is not JSON formatted,
+ printf("Wrong password!\n"); // then it's the wrong password
+ free(password);
+ string_destroy(&output_pString);
+ return NULL;
+ }
+ }
+ if (free_pw) { // If being called from main
+ free(password);
+ if (option == ENCRYPT)
+ printf("Successfully encrypted your portfolio.\n");
+ else printf("Successfully decrypted your portfolio.\n");
+ }
+ }
+ json_object_put(jobj);
+ return output_pString;
} \ No newline at end of file