Code
#include <iostream>
#include "safer.h"
#include <string>
#include "boxes.h"
#include <stdlib.h>
using namespace std;
int main(int argc, char * argv[]) {
string hex_key = argv[1];
string message, temp;
byte priv_key[8], y[8], block[8];
byte stage_keys[2*r+1][8];
if (hex_key.length() != 16 ) {
cout << "Invalid key size: Use a 64-bit hex-character key." << endl;
return(1);
}
/* let's convert that damn hex to unsigned chars.... */
for (int i = 0; i < hex_key.length() / 2; i++) {
temp = hex_key.substr(2*i, 2);
priv_key[i] = (byte)strtoul(temp.c_str(), NULL, 16);
}
/* a function to generate our stage keys! */
key_generate(r, priv_key, stage_keys);
cout << "Please enter the message to encrypt:" << endl;
cin >> message;
/* This loop extracts 8 byte blocks and runs them through the stages... */
for (int i = 0; i < message.length() / 8; i++) {
temp = message.substr(8*i, 8);
for (int j = 0; j < temp.length(); j++) {
block[j] = (byte)temp.at(j);
}
safer_stage(block, stage_keys, y);
}
/* ...if we have any bytes left, we create one more block with padded 0's */
if (message.length() % 8 != 0) {
int mod = message.length() % 8;
message.erase(0, message.length() / 8 * 8);
temp = message.substr(0, mod);
for (int i = 0; i < temp.length(); i++) {
block[i] = (byte)temp.at(i);
}
for (int j = mod; j < 8; j++) {
block[j] = 0;
}
safer_stage(block, stage_keys, y);
}
return(0);
}
this is the main portion of my safer sk-64 implementation
found a neat way to do a left end-around 3 bit rotate online which helped:
Code
byte rotate(byte element) {
return ((element<<3)|(element>>5)) & 255;
}
This post was edited by Eep on Jul 16 2013 11:16pm