Skip to content

Commit

Permalink
Fix #13 Support for other languages
Browse files Browse the repository at this point in the history
  • Loading branch information
philipperemy committed Oct 6, 2022
1 parent 300e54e commit 390b752
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 59 deletions.
220 changes: 220 additions & 0 deletions Base64.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
///////////////////////////////////////////////////////////////////////
// Base64.cpp declares class that encodes binary data into a string //
// ver 2.0 and decodes the string back to binary data //
// //
// Language: Visual C++, ver 7.1 //
// Platfrom: Dell Dimension 8300, Windows XP, SP1 //
// Application: CSE687 - Object Oriented Design, Spring 2004 //
// Author: Jim Fawcett, CST 2-187, Syracuse University //
// (315) 443-3948, [email protected] //
///////////////////////////////////////////////////////////////////////
// based on: //
//*********************************************************************
//* Base64 - a simple base64 encoder and decoder.
//*
//* Copyright (c) 1999, Bob Withers - [email protected]
//*
//* This code may be freely used for any purpose, either personal
//* or commercial, provided the authors copyright notice remains
//* intact.
//*********************************************************************

#include "Base64.h"
#include <fstream>

const char fillchar = '=';

// 00000000001111111111222222
// 01234567890123456789012345
static std::string cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

// 22223333333333444444444455
// 67890123456789012345678901
"abcdefghijklmnopqrstuvwxyz"

// 555555556666
// 234567890123
"0123456789+/";

//
//----< convert vector of bytes to std::string >---------------------

std::string Base64::encode(const std::vector<char>& data)
{
std::string::size_type i;
char c;
unsigned int len = data.size();
std::string ret;

for (i = 0; i < len; ++i)
{
c = (data[i] >> 2) & 0x3f;
ret.append(1, cvt[c]);
c = (data[i] << 4) & 0x3f;
if (++i < len)
c |= (data[i] >> 4) & 0x0f;

ret.append(1, cvt[c]);
if (i < len)
{
c = (data[i] << 2) & 0x3f;
if (++i < len)
c |= (data[i] >> 6) & 0x03;

ret.append(1, cvt[c]);
}
else
{
++i;
ret.append(1, fillchar);
}

if (i < len)
{
c = data[i] & 0x3f;
ret.append(1, cvt[c]);
}
else
{
ret.append(1, fillchar);
}
}

return(ret);
}
//
//----< convert std::string to vector of bytes >---------------------

std::vector<char> Base64::decode(const std::string& data)
{
std::string::size_type i;
char c;
char c1;
std::string::size_type len = data.length();
std::vector<char> ret;

for (i = 0; i < len; ++i)
{
c = (char) cvt.find(data[i]);
++i;
c1 = (char) cvt.find(data[i]);
c = (c << 2) | ((c1 >> 4) & 0x3);
ret.push_back(c);
if (++i < len)
{
c = data[i];
if (fillchar == c)
break;
c = (char) cvt.find(c);
c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
ret.push_back(c1);
}
if (++i < len)
{
c1 = data[i];
if (fillchar == c1)
break;
c1 = (char) cvt.find(c1);
c = ((c << 6) & 0xc0) | c1;
ret.push_back(c);
}
}
return(ret);
}

//
//----< create encoded string from binary file contents >------------

std::string Base64::encodeFromFile(const std::string& inFileName)
{
std::ifstream in;
in.open(inFileName.c_str(),std::ios::binary);
if(!in.good())
{
throw std::invalid_argument(std::string("can't open file ") + inFileName);
}
std::vector<char> fBytes;
while(in.good())
fBytes.push_back(in.get());
fBytes.pop_back();
in.close();

return Base64::encode(fBytes);
}
//----< create new binary file from encoded string >-----------------

void Base64::decodeToFile(
const std::string& outFileName, const std::string& encodedString
)
{
std::ofstream out;
out.open(outFileName.c_str(),std::ios::binary);
if(!out.good())
{
throw std::invalid_argument(std::string("can't open file ") + outFileName);
}
std::vector<char> fdecodedBytes = Base64::decode(encodedString);

for(unsigned int i=0; i<fdecodedBytes.size(); ++i)
out.put(fdecodedBytes[i]);
out.close();
}

//
#ifdef TEST_BASE64

#include <iostream>
//#include <fstream>

void main()
{
std::cout << "\n Demonstrate Base64 Encoding and Decoding "
<< "\n ==========================================\n";

///////////////////////////////////////////////////////////////////
// encoding ascii string

std::string msg
= "We're not in Kansas anymore, Toto!";

std::vector<char> vmsg(msg.begin(),msg.end());
std::string encodedMsg = Base64::encode(vmsg);
std::vector<char> vdecoded = Base64::decode(encodedMsg);
std::string decodedMsg(vdecoded.begin(),vdecoded.end());

std::cout << "\n original: " << msg;
std::cout << "\n encoded: " << encodedMsg;
std::cout << "\n decoded: " << decodedMsg;
std::cout << "\n";

////////////////////////////////////////////////////////////////////
// encoding binary file

const std::string ifile = "debug\\Base64Encoding.exe";
const std::string ofile1 = "debug\\temp.dat";
const std::string ofile2 = "debug\\temp.exe";

try
{
std::cout << "\n creating encoded string from binary file "
<< ifile << std::endl;
std::string encodedString = Base64::encodeFromFile(ifile);

std::string temp(encodedString.begin(), encodedString.begin() + 50);
std::cout << "\n First fifty bytes of encoded string:\n "
<< temp << "\n";

std::cout << "\n writing decoded string to binary file "
<< ofile2 << "\n";
Base64::decodeToFile(ofile2,encodedString);

std::cout << "\n Use fc to demonstrate that "
<< ifile << "\n is identical to " << ofile2 << "\n\n";
}
catch(std::exception& e)
{
std::cout << "\n " << e.what() << "\n\n";
}
}

#endif
75 changes: 75 additions & 0 deletions Base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#ifndef BASE64_H
#define BASE64_H
///////////////////////////////////////////////////////////////////////
// Base64.h - declares class that encodes binary data into a string //
// ver 2.0 and decodes the string back to binary data //
// //
// Language: Visual C++, ver 7.1 //
// Platfrom: Dell Dimension 8300, Windows XP, SP1 //
// Application: CSE687 - Object Oriented Design, Spring 2004 //
// Author: Jim Fawcett, CST 2-187, Syracuse University //
// (315) 443-3948, [email protected] //
///////////////////////////////////////////////////////////////////////
// based on: //
//*********************************************************************
//* C_Base64 - a simple base64 encoder and decoder.
//*
//* Copyright (c) 1999, Bob Withers - [email protected]
//*
//* This code may be freely used for any purpose, either personal
//* or commercial, provided the authors copyright notice remains
//* intact.
//*********************************************************************
/*
* Module Operation:
* =================
* The Base64 class provides static encode and decode functions that
* use base64 encoding to render a vector of bytes into a std::string
* of ASCII characters and back again. This supports the transmission
* of binary data as XML messages.
*
* Public Interface:
* =================
* std::vector<char> vBytes;
* // vBytes filed, perhaps from a file
* std::string encodedString = Base64::encode(vBytes);
* // do something with string, perhaps send as XML message
* std::vector<char> vDecodedBytes = Base64::decode(encodedMsg);
*
* Build Process:
* ==============
* Required files - Base64.h, Base64.cpp
*
* compiler command - cl /EHsc /DTEST_BASE64 Base64.cpp
*
* Maintenance History:
* ====================
* ver 1.0 : 25 Mar 04
* - first release, based on code by Bob Withers
* ver 2.0 : 20 Apr 04
* - changed interface from std::strings for both input and output
* to std::vector<char> for binary data and std::string for its
* endcoding. Test stub shows how to use this to encode binary
* files.
*
* Planned Modifications:
* ======================
* - add functions to support chunking of large files
*/

//
#include <string>
#include <vector>

class Base64
{
public:
static std::string encode(const std::vector<char>& data);
static std::vector<char> decode(const std::string& data);
static std::string encodeFromFile(const std::string& inFileName);
static void decodeToFile(
const std::string& outFileName, const std::string& encodedString
);
};

#endif
47 changes: 0 additions & 47 deletions b64.h

This file was deleted.

27 changes: 15 additions & 12 deletions encrypt.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#include "b64.h"
#include "Base64.h"
#include "vigenere.h"

std::string encrypt(std::string& msg, std::string& key) {
std::string b64_str = base64_encode(msg);
std::string vigenere_msg = encrypt_vigenere(b64_str, key);
// std::cout << vigenere_msg << std::endl;
return vigenere_msg;
std::string encrypt(std::string& msg, std::string& key)
{
std::vector<char> msg2(msg.begin(), msg.end());
std::string b64_str = Base64::encode(msg2);
std::string vigenere_msg = encrypt_vigenere(b64_str, key);
return vigenere_msg;
}


std::string decrypt(std::string& encrypted_msg, std::string& key) {
std::string newKey = extend_key(encrypted_msg, key);
std::string b64_encoded_str = decrypt_vigenere(encrypted_msg, newKey);
std::string b64_decode_str = base64_decode(b64_encoded_str);
return b64_decode_str;
}
std::string decrypt(std::string& encrypted_msg, std::string& key)
{
std::string newKey = extend_key(encrypted_msg, key);
std::string b64_encoded_str = decrypt_vigenere(encrypted_msg, newKey);
std::vector<char> b64_decode_vec = Base64::decode(b64_encoded_str);
std::string b64_decode_str(b64_decode_vec.begin(), b64_decode_vec.end());
return b64_decode_str;
}

0 comments on commit 390b752

Please sign in to comment.