Skip to content
Snippets Groups Projects
Commit 605f8960 authored by mc2zk's avatar mc2zk
Browse files

Adding more test case source.

Former-commit-id: 63bb2bdd80d7d75efb692ea49ebff38d49f965f2
parent b652f8e0
No related branches found
No related tags found
No related merge requests found
Showing
with 1634 additions and 0 deletions
......@@ -102,6 +102,25 @@ dry_run_examples/TC_C_121_v922/testData/keyc_good -text
dry_run_examples/TC_C_121_v922/testData/keyd_good -text
dry_run_examples/TC_C_121_v922/testData/keye_good -text
dry_run_examples/TC_C_121_v922/testData/keyf_bad -text
dry_run_examples/TC_C_170_v905/.cproject -text
dry_run_examples/TC_C_170_v905/.project -text
dry_run_examples/TC_C_170_v905/Debug/makefile -text
dry_run_examples/TC_C_170_v905/Debug/objects.mk -text
dry_run_examples/TC_C_170_v905/Debug/sources.mk -text
dry_run_examples/TC_C_170_v905/Debug/src/solitaire.d -text
dry_run_examples/TC_C_170_v905/Debug/src/subdir.mk -text
dry_run_examples/TC_C_170_v905/Release/makefile -text
dry_run_examples/TC_C_170_v905/Release/objects.mk -text
dry_run_examples/TC_C_170_v905/Release/sources.mk -text
dry_run_examples/TC_C_170_v905/Release/src/solitaire.d -text
dry_run_examples/TC_C_170_v905/Release/src/subdir.mk -text
dry_run_examples/TC_C_170_v905/io-specifications/io-TC_C_170_v905_G1.xml -text
dry_run_examples/TC_C_170_v905/io-specifications/io-TC_C_170_v905_G2.xml -text
dry_run_examples/TC_C_170_v905/io-specifications/io-TC_C_170_v905_G3.xml -text
dry_run_examples/TC_C_170_v905/io-specifications/io-TC_C_170_v905_G4.xml -text
dry_run_examples/TC_C_170_v905/metaData.xml -text
dry_run_examples/TC_C_170_v905/src/solitaire.c -text
dry_run_examples/TC_C_170_v905/testData/key -text
dry_run_examples/TC_C_191_v304/Release/makefile -text
dry_run_examples/TC_C_191_v304/Release/objects.mk -text
dry_run_examples/TC_C_191_v304/Release/sources.mk -text
......
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TC_C_170_v905</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
<dictionary>
<key>?name?</key>
<value></value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.append_environment</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildArguments</key>
<value></value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildCommand</key>
<value>make</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.buildLocation</key>
<value>${workspace_loc:/TC_C_170_v905/Release}</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.contents</key>
<value>org.eclipse.cdt.make.core.activeConfigSettings</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableAutoBuild</key>
<value>false</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableCleanBuild</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.enableFullBuild</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.stopOnError</key>
<value>true</value>
</dictionary>
<dictionary>
<key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
<value>false</value>
</dictionary>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include subdir.mk
-include src/subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif
-include ../makefile.defs
# Add inputs and outputs from these tool invocations to the build variables
# All Target
all: TC_C_170_v905
# Tool invocations
TC_C_170_v905: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: MinGW C Linker'
gcc -o"TC_C_170_v905" $(OBJS) $(USER_OBJS) $(LIBS) $(SS_LDFLAGS) $(SS_OS_SPECIFIC_LDFLAGS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(OBJS)$(C_DEPS)$(EXECUTABLES) TC_C_170_v905
-@echo ' '
.PHONY: all clean dependents
.SECONDARY:
-include ../makefile.targets
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
USER_OBJS :=
LIBS :=
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
O_SRCS :=
C_SRCS :=
S_UPPER_SRCS :=
OBJ_SRCS :=
ASM_SRCS :=
OBJS :=
C_DEPS :=
EXECUTABLES :=
# Every subdirectory with source files must be described here
SUBDIRS := \
src \
src/solitaire.d src/solitaire.o: ../src/solitaire.c
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
C_SRCS += \
../src/solitaire.c
OBJS += \
./src/solitaire.o
C_DEPS += \
./src/solitaire.d
# Each subdirectory must supply rules for building sources it contributes
src/%.o: ../src/%.c
@echo 'Building file: $<'
@echo 'Invoking: GCC C Compiler'
gcc -O0 -g3 -Wall -c $(SS_CFLAGS) $(SS_SPECIFIC_CFLAGS) -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
@echo 'Finished building: $<'
@echo ' '
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include subdir.mk
-include src/subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif
-include ../makefile.defs
# Add inputs and outputs from these tool invocations to the build variables
# All Target
all: TC_C_170_v905
# Tool invocations
TC_C_170_v905: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: MinGW C Linker'
$(SS_LNK) -o"TC_C_170_v905" $(OBJS) $(USER_OBJS) $(LIBS) $(SS_LDFLAGS) $(SS_OS_SPECIFIC_LDFLAGS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(OBJS)$(C_DEPS)$(EXECUTABLES) TC_C_170_v905
-@echo ' '
.PHONY: all clean dependents
.SECONDARY:
-include ../makefile.targets
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
USER_OBJS :=
LIBS :=
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
O_SRCS :=
C_SRCS :=
S_UPPER_SRCS :=
OBJ_SRCS :=
ASM_SRCS :=
OBJS :=
C_DEPS :=
EXECUTABLES :=
# Every subdirectory with source files must be described here
SUBDIRS := \
src \
src/solitaire.d src/solitaire.o: ../src/solitaire.c
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
C_SRCS += \
../src/solitaire.c
OBJS += \
./src/solitaire.o
C_DEPS += \
./src/solitaire.d
# Each subdirectory must supply rules for building sources it contributes
src/%.o: ../src/%.c
@echo 'Building file: $<'
@echo 'Invoking: GCC C Compiler'
$(SS_CC) -O0 -Wall $(SS_CFLAGS) $(SS_OS_SPECIFIC_CFLAGS) -c -fmessage-length=0 -std=c99 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
@echo 'Finished building: $<'
@echo ' '
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<io_pair>
<io_pair_category_id>1</io_pair_category_id>
<name>TC_C_170_v905_G1</name>
<technical_impact_id>1</technical_impact_id>
<platform_id>3</platform_id>
<io_groups>
<co_process/>
<io_parameters>
<feature_id>9</feature_id>
<io_values>
<val>key</val>
<val_type_id>1</val_type_id>
<order>1</order>
</io_values>
<io_values>
<val>adafafsacasdw41rfq24y3ew</val>
<val_type_id>1</val_type_id>
<order>2</order>
</io_values>
<order>1</order>
</io_parameters>
<io_parameters>
<feature_id>3</feature_id>
<io_values>
<val>Fred Flintstone&#13;
/quit&#13;
</val>
<val_type_id>1</val_type_id>
<order>1</order>
</io_values>
<order>2</order>
</io_parameters>
</io_groups>
<io_groups>
<co_process/>
<io_parameters>
<feature_id>139</feature_id>
<io_values>
<val>Enter message to be encrypted (type '/help' for help):&#13;
Unencrypted: Fred Flintstone&#13;
Encrypted: EBTRJTXOOKJFXA&#13;
Enter message to be encrypted (type '/help' for help):&#13;
</val>
<val_type_id>1</val_type_id>
<order>1</order>
</io_values>
<order>1</order>
</io_parameters>
</io_groups>
<description/>
<id>951</id>
</io_pair>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<io_pair>
<io_pair_category_id>1</io_pair_category_id>
<name>TC_C_170_v905_G2</name>
<technical_impact_id>1</technical_impact_id>
<platform_id>3</platform_id>
<io_groups>
<co_process/>
<io_parameters>
<feature_id>9</feature_id>
<io_values>
<val>key</val>
<val_type_id>1</val_type_id>
<order>1</order>
</io_values>
<io_values>
<val>adafafsacasdw41rfq24y3ew</val>
<val_type_id>1</val_type_id>
<order>2</order>
</io_values>
<order>1</order>
</io_parameters>
<io_parameters>
<feature_id>3</feature_id>
<io_values>
<val>Barney Rubble&#13;
/quit&#13;
</val>
<val_type_id>1</val_type_id>
<order>1</order>
</io_values>
<order>2</order>
</io_parameters>
</io_groups>
<io_groups>
<co_process/>
<io_parameters>
<feature_id>139</feature_id>
<io_values>
<val>Enter message to be encrypted (type '/help' for help):&#13;
Unencrypted: Barney Rubble&#13;
Encrypted: AKGBIGGVWTBV&#13;
Enter message to be encrypted (type '/help' for help):&#13;
</val>
<val_type_id>1</val_type_id>
<order>1</order>
</io_values>
<order>1</order>
</io_parameters>
</io_groups>
<description/>
<id>953</id>
</io_pair>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<io_pair>
<io_pair_category_id>1</io_pair_category_id>
<name>TC_C_170_v905_G3</name>
<technical_impact_id>1</technical_impact_id>
<platform_id>3</platform_id>
<io_groups>
<co_process/>
<io_parameters>
<feature_id>9</feature_id>
<io_values>
<val>key</val>
<val_type_id>1</val_type_id>
<order>1</order>
</io_values>
<io_values>
<val>adafafsacasdw41rfq24y3ew</val>
<val_type_id>1</val_type_id>
<order>2</order>
</io_values>
<order>1</order>
</io_parameters>
<io_parameters>
<feature_id>3</feature_id>
<io_values>
<val>Wilma Flintstone&#13;
/quit&#13;
</val>
<val_type_id>1</val_type_id>
<order>1</order>
</io_values>
<order>2</order>
</io_parameters>
</io_groups>
<io_groups>
<co_process/>
<io_parameters>
<feature_id>139</feature_id>
<io_values>
<val>Enter message to be encrypted (type '/help' for help):&#13;
Unencrypted: Betty Rubble&#13;
Encrypted: AOIHCZJCWDU&#13;
Enter message to be encrypted (type '/help' for help):&#13;
</val>
<val_type_id>1</val_type_id>
<order>1</order>
</io_values>
<order>1</order>
</io_parameters>
</io_groups>
<description/>
<id>952</id>
</io_pair>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<io_pair>
<io_pair_category_id>1</io_pair_category_id>
<name>TC_C_170_v905_G4</name>
<technical_impact_id>1</technical_impact_id>
<platform_id>3</platform_id>
<io_groups>
<co_process/>
<io_parameters>
<feature_id>9</feature_id>
<io_values>
<val>key</val>
<val_type_id>1</val_type_id>
<order>1</order>
</io_values>
<io_values>
<val>adafafsacasdw41rfq24y3ew</val>
<val_type_id>1</val_type_id>
<order>2</order>
</io_values>
<order>1</order>
</io_parameters>
<io_parameters>
<feature_id>3</feature_id>
<io_values>
<val>Betty Rubble&#13;
/quit&#13;
</val>
<val_type_id>1</val_type_id>
<order>1</order>
</io_values>
<order>2</order>
</io_parameters>
</io_groups>
<io_groups>
<co_process/>
<io_parameters>
<feature_id>139</feature_id>
<io_values>
<val>Enter message to be encrypted (type '/help' for help):&#13;
Unencrypted: Betty Rubble&#13;
Encrypted: AOIHCZJCWDU&#13;
Enter message to be encrypted (type '/help' for help):&#13;
</val>
<val_type_id>1</val_type_id>
<order>1</order>
</io_values>
<order>1</order>
</io_parameters>
</io_groups>
<description/>
<id>954</id>
</io_pair>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project>
<cwe_entry_id>170</cwe_entry_id>
<derived_from_id>0</derived_from_id>
<name>TC_C_170_v905</name>
<language_id>2</language_id>
<generation_method_id>1</generation_method_id>
<run_command>Release/TC_C_170_v905 </run_command>
<build_command/>
<run_directory/>
<build_directory/>
<line_count>4771</line_count>
<annotations>
<name>feature-ADDRESS_AS_A_CONSTANT</name>
<name>TC_C_170_v905\src\solitaire.c</name>
<lineNumber>282</lineNumber>
</annotations>
<annotations>
<name>feature-VARIABLE_ARGUMENT_LIST</name>
<name>TC_C_170_v905\src\solitaire.c</name>
<lineNumber>277</lineNumber>
</annotations>
<annotations>
<name>CROSSOVER_POINT</name>
<name>TC_C_170_v905\src\solitaire.c</name>
<lineNumber>347</lineNumber>
</annotations>
<annotations>
<name>feature-STDIN</name>
<name>TC_C_170_v905\src\solitaire.c</name>
<lineNumber>341</lineNumber>
</annotations>
<annotations>
<name>INTERACTION_POINT</name>
<name>TC_C_170_v905\src\solitaire.c</name>
<lineNumber>341</lineNumber>
</annotations>
<annotations>
<name>feature-UNSIGNED_BYTE</name>
<name>TC_C_170_v905\src\solitaire.c</name>
<lineNumber>340</lineNumber>
</annotations>
<annotations>
<name>TRIGGER_POINT</name>
<name>TC_C_170_v905\src\solitaire.c</name>
<lineNumber>286</lineNumber>
</annotations>
<description/>
<id>1017</id>
</project>
/*******************************************
**
** Copyright (C) 2011 The MITRE Corporation. ALL RIGHTS RESERVED
**
** Author: Mitchell Wills
** Date: 8/4/2011
**
** Base Test Program -- solitare.c
**
** <base test program description, note that this description
** should not talk about things like source taint and any other
** variant categories as those details will change>
**
** Variant Test Case Program
**
** The program does this:
** 1) Reads in up to 25 bytes from a key file, and converts those characters into a 32-bit unsigned integer
** 2) Using that 32-bit unsigned integer as a seed, creates a shuffled deck of 54 cards (with jokers)
** 3) Read in a message to be encoded
** 4) Strip the message of all non-alphabet-letters
** 5) Read a new keystream value from the deck by
** a) Moving the first joker one spot down the deck
** b) Moving the second joker two spots down the deck
** c) Cutting the deck into three parts, and reversing the order of them relative to each other, delimited by the jokers
** d) Cutting the deck by as many cards as the value of the bottom card
** e) The next keystream value is the nth card from the top, where n is the card at the top of the deck
** f) The deck stays as it ends up
** 6) Encrypt the first character by adding the keystream value to it, mod 26
** 7) Perform steps 5-6 until done
**
** Decryption works similarly.
**
** See http://en.wikipedia.org/wiki/Solitaire_(cipher)
**
**
** STONESOUP Weakness Class: Buffer overflows/underflows/out of bounds/accesses/memory safety errors
** CWE ID: CWE-170
** Variant Spreadsheet Rev #: 2
** Variant Spreadsheet ID: 905
**
** Variant Features:
** Source Taint: STDIN
** Data Type: UNSIGNED_BYTE
** Control Flow: VARIABLE_ARGUMENT_LIST
** Data Flow: ADDRESS_AS_A_CONSTANT
**
** Initial Testing: (x means yes, - means no)
** Tested in MS Windows XP 32bit x
** Tested in MS Windows 7 64bit -
** Tested in Ubuntu10_10 Linux 32bit -
** Tested in Ubuntu10_10 Linux 64bit -
**
** Workflow:
** Created:8/4/2011
** 1st Vett: <programmer> on <date>
** 2nd Vett: <peer> on <date>
** 3rd Vett: <teamleader> on <date>
** 4th Vett: Fortify Issues Fixed on <date>
** 5th Vett: Tested in Harness on <date>
**
**
** I/0 Pairs:
** Good: 1st Set:
** 2nd Set:
** 3rd Set:
** 4th Set:
** 5th Set:
** Bad: 1st Set:
*********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#define KEY_LENGTH 25
#define DECK_SIZE 54
#define ALPHA_SIZE 26
#define FIRST_JOKER 53
#define SECOND_JOKER 54
#define MAX_MESSAGE_LENGTH 20
typedef char Card;
void print_deck (Card *deck) {
int i;
for (i = 0; i < DECK_SIZE; i++) {
printf("%d ", deck[i]);
}
printf("\n");
}
//In-place Fischer-Yates shuffle
void shuffle(Card *deck, unsigned int seed) {
int cards_left = DECK_SIZE;
int next_index;
int cur_index;
Card temp;
srand(seed);
while (cards_left != 0) {
next_index = DECK_SIZE - rand() % cards_left - 1;
cur_index = DECK_SIZE - cards_left;
temp = deck[cur_index];
deck[cur_index] = deck[next_index];
deck[next_index] = temp;
cards_left--;
}
}
//turn the key into a 32-bit unsigned int via questionably required
unsigned int seedify (char *key) {
int i;
unsigned int seed = 0;
for (i = 0; i < KEY_LENGTH; i++) {
seed += (unsigned int)(key[i]) << (i % 32);
}
return seed;
}
//randomly initializes the deck
void init_deck(Card *deck, char *key) {
int i;
for (i = 0; i < DECK_SIZE; i++) {
deck[i] = (Card)(i+1);
}
shuffle(deck, seedify(key));
}
int indexof (Card *deck, Card c) {
int i;
for (i = 0; i < DECK_SIZE; i++) {
if (deck[i] == c) {
return i;
}
}
return -1;
}
void joker_shift(Card *deck) {
int fji = indexof(deck, FIRST_JOKER);
if (fji < DECK_SIZE - 1 && fji >= 0) {
deck[fji] = deck[fji+1];
deck[fji+1] = FIRST_JOKER;
fji++;
}
int sji = indexof(deck, SECOND_JOKER);
if (sji < DECK_SIZE - 1 && sji >= 0) {
deck[sji] = deck[sji+1];
deck[sji+1] = SECOND_JOKER;
sji++;
}
if (sji < DECK_SIZE - 1 && sji >= 0) {
deck[sji] = deck[sji+1];
deck[sji+1] = SECOND_JOKER;
sji++;
}
}
void triple_cut(Card *deck) {
Card helper_deck[DECK_SIZE];
int fji = indexof(deck, FIRST_JOKER);
int sji = indexof(deck, SECOND_JOKER);
if (fji > DECK_SIZE || fji < 0 || sji > DECK_SIZE || sji < 0) {
printf("Something bad happened... jokers are not there right.\n");
return;
}
int top_joker_index;
int bottom_joker_index;
if (fji < sji) {
top_joker_index = fji;
bottom_joker_index = sji;
} else {
top_joker_index = sji;
bottom_joker_index = fji;
}
int write_index = 0;
int read_index = bottom_joker_index + 1;
while (read_index < DECK_SIZE) {
helper_deck[write_index] = deck[read_index];
read_index++;
write_index++;
}
read_index = top_joker_index;
while (read_index <= bottom_joker_index) {
helper_deck[write_index] = deck[read_index];
read_index++;
write_index++;
}
read_index = 0;
while (read_index < top_joker_index) {
helper_deck[write_index] = deck[read_index];
read_index++;
write_index++;
}
memcpy(deck, helper_deck, DECK_SIZE*sizeof(Card));
}
void bottom_value_cut(Card *deck) {
Card helper_deck[DECK_SIZE];
int bottom_value = (int)deck[DECK_SIZE - 1];
if (bottom_value >= DECK_SIZE) {
bottom_value = DECK_SIZE - 1;
}
int read_index = bottom_value;
int write_index = 0;
while (read_index < DECK_SIZE) {
helper_deck[write_index] = deck[read_index];
read_index++;
write_index++;
}
read_index = 0;
while (read_index < bottom_value) {
helper_deck[write_index] = deck[read_index];
read_index++;
write_index++;
}
read_index = DECK_SIZE - 1;
while (read_index < DECK_SIZE) {
helper_deck[write_index] = deck[read_index];
read_index++;
write_index++;
}
memcpy(deck, helper_deck, DECK_SIZE*sizeof(Card));
}
//Does all the algorithmy stuff described in the header
int next_keystream_value (Card *deck) {
joker_shift(deck);
triple_cut(deck);
bottom_value_cut(deck);
int return_index = deck[0];
if (return_index >= DECK_SIZE) {
return_index = DECK_SIZE - 1;
}
return deck[return_index];
}
char *encrypt(Card *deck, char *message, char *encrypted) {
int i = 0; //message index
int j = 0; //encrypted index
char c;
while (message[i] != '\0' && i<MAX_MESSAGE_LENGTH) {
c = message[i];
if (isalpha(c)) {
c = toupper(c) - 65;
int keystream = next_keystream_value(deck);
encrypted[j] = (((int)c + keystream) % ALPHA_SIZE) + 65;
//printf("%d\n", keystream);
j++;
}
i++;
}
encrypted[j] = '\0';
return encrypted;
}
char *decrypt(Card *deck, char *message, char *decrypted) {
int i = 0; //message index
int j = 0; //decrypted index
char c;
while (message[i] != '\0' && i<MAX_MESSAGE_LENGTH) {
c = message[i];
if (isalpha(c)) {
c = toupper(c) - 65;
int keystream = next_keystream_value(deck);
decrypted[j] = (((int)c + (ALPHA_SIZE - (keystream % ALPHA_SIZE))) % ALPHA_SIZE) + 65;
//printf("%d\n", keystream);
j++;
}
i++;
}
decrypted[j] = '\0';
return decrypted;
}
void processStuff(int mode, ...){//STONESOUP:feature-VARIABLE_ARGUMENT_LIST
va_list varargs;
va_start(varargs, mode);
Card* deck = va_arg(varargs, Card*);
const char* in_message = va_arg(varargs, char*);//STONESOUP:feature-ADDRESS_AS_A_CONSTANT
char* out_message = va_arg(varargs, char*);
if (!mode) {
printf("Unencrypted: %s\n", in_message);//STONESOUP:TRIGGER_POINT
printf("Encrypted: %s\n", encrypt(deck, in_message, out_message));
} else {
printf("Encrypted: %s\n", in_message);
printf("Decrypted: %s\n", decrypt(deck, in_message, out_message));
}
/*for(i=0; 1==1; ++i){
printf("%i - %c\n", in_message[i], in_message[i]);
fflush(stdout);
if(in_message[i]=='\0')
break;
}
for(i=0; 1==1; ++i){
printf("%i - %c\n", out_message[i], out_message[i]);
fflush(stdout);
if(in_message[i]=='\0')
break;
}*/
va_end(varargs);
}
int main (int argc, char *argv[]) {
char key[KEY_LENGTH]; //char array, not a string
Card deck[DECK_SIZE];
char out_message[MAX_MESSAGE_LENGTH];
Card starting_deck[DECK_SIZE];
char in_message[MAX_MESSAGE_LENGTH];
FILE *keyfile;
int mode = 0; //encrypt = 0, decrypt = 1
keyfile = fopen("key", "r");
if (!keyfile) {
printf("Unable to open key file\n");
exit(1);
}
if (!fgets(key, KEY_LENGTH, keyfile)) {
printf("Reading from key file failed\n");
fclose(keyfile);
exit(1);
}
fclose(keyfile);
init_deck(starting_deck, key);
while (1) {
memcpy(deck, starting_deck, sizeof(Card) * DECK_SIZE); //set the deck to the key starting position
//print_deck(deck);
if (!mode) {
printf("Enter message to be encrypted (type '/help' for help):\n");
} else {
printf("Enter message to be decrypted (type '/help' for help):\n");
}
fflush(stdout);
int i;
unsigned char c = '\0';//STONESOUP:feature-UNSIGNED_BYTE
for (i = 0; (c = getchar()) != '\0' && i<MAX_MESSAGE_LENGTH; i++) {//STONESOUP:INTERACTION_POINT //STONESOUP:feature-STDIN
in_message[i] = c;
if (in_message[i] == '\n') {
in_message[i] = '\0';
break;
}
}//STONESOUP:CROSSOVER_POINT
fflush(stdout);
if (!strcmp(in_message, "/quit")) {
break;
} else if (!strcmp(in_message, "/encrypt")) {
mode = 0;
printf("Encryption mode\n");
} else if (!strcmp(in_message, "/decrypt")) {
mode = 1;
printf("Decryption mode\n");
} else if (!strcmp(in_message, "/help")) {
printf("/encrypt: switch to encryption mode\n"
"/decrypt: switch to decryption mode\n"
"/quit: quit program\n");
} else {
processStuff(mode, deck, in_message, out_message);
}
}
return 0;
}
adafafsacasdw41rfq24y3ew
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment