Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: AzzieDev/CryptKeeper
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.2.0
Choose a base ref
...
head repository: AzzieDev/CryptKeeper
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 6 commits
  • 5 files changed
  • 1 contributor

Commits on Apr 3, 2023

  1. Copy the full SHA
    4d46c24 View commit details
  2. Copy the full SHA
    b02a790 View commit details
  3. show prime factors for phi

    AzzieDev committed Apr 3, 2023
    Copy the full SHA
    196bbf7 View commit details
  4. Copy the full SHA
    e18f6ae View commit details

Commits on Apr 9, 2023

  1. Copy the full SHA
    90509e3 View commit details

Commits on Apr 10, 2023

  1. risk big numbers

    AzzieDev committed Apr 10, 2023
    Copy the full SHA
    48de0b7 View commit details
Showing with 142 additions and 45 deletions.
  1. +1 −0 .idea/.name
  2. +6 −0 .idea/encodings.xml
  3. +5 −3 README.md
  4. +122 −41 crypt.cpp
  5. +8 −1 crypt.h
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Cryptkeeper
# CryptKeeper
## This program is intended for encryption and decryption, including brute force attacks.

The bigint functions use the Boost Multiprecision library. The Boost Math package can be installed by
`sudo apt install libboost-math-dev` with the apt package manager, so you don't need the entire libboots-all-dev package.
`sudo apt install libboost-math-dev` with the apt package manager, so you don't need the entire libboosts-all-dev package.
The [complete package can be downloaded](https://www.boost.org/users/history/version_1_81_0.html) and extracted. The suggested version is 1.81 which contains the necessary libraries.


### Modes:
* Help menu
* Shift/Caesar Cipher
* Euclidean Algorithm (and Extended)
* Big Integer Calculations
* Modular Exponentiation (PowerMod)
* Euler's Totient Function Φ(N)
* Euler's Totient Function Φ(N) (including factorials)
163 changes: 122 additions & 41 deletions crypt.cpp
Original file line number Diff line number Diff line change
@@ -9,13 +9,11 @@

#include "crypt.h"

void euclidAlgo();

using namespace std;

//intro function
int main() {
cout << "Welcome to the Cryptkeeper" << endl;
cout << "Welcome to the CryptKeeper" << endl;
string moder;
while (moder.empty()) {
cout << "Enter a mode, or help to list all modes, quit/exit" << endl;
@@ -265,14 +263,14 @@ void modularExp() {
binary[num_bits - i - 1] = (k >> i) & 1;
}
cout << "| ";
for (int i = 0; i < binary.size(); i++) {
cout << " " << binary[i] << " | ";
for (const auto &i: binary) {
cout << " " << i << " | ";
}
cout << endl;
cout << "And for each bit, find a remainder in a table:" << endl;
cout << "| ";
cpp_int c = 0;
for (int i = 0; i < binary.size(); i++) {
for (size_t i = 0; i < binary.size(); i++) {
if (i == 0) {
c = a;
} else if (binary[i] == 0) {
@@ -284,7 +282,7 @@ void modularExp() {
}
cout << endl << endl;
c = 0;
for (int i = 0; i < binary.size(); i++) {
for (size_t i = 0; i < binary.size(); i++) {
if (i == 0) {
c = a;
cout << "First we start with " << a << endl;
@@ -310,47 +308,105 @@ void modularExp() {
//helper for phi(n)
void eulerHelper() {
cout << "Welcome to Euler's Totient Function mode!" << endl;
// cout << "Enter 'n' for integer, '!' for factorial:" << endl;
// char choice;
// cin >> choice;
// if (choice == 'n') {
cout << "Enter an integer N:" << endl;
cout << "Enter 'n' for integer, '!' for factorial:" << endl;
char choice;
cpp_int n;
cin >> n;
cout << "Phi \u03A6(" << n << ") = " << phi(n) << endl;
//}
set<cpp_int> primeFactorSet;
cin >> choice;
if (choice == 'n') {
cout << "Enter an integer N:" << endl;
cin >> n;
primeFactorSet = primeFactors(n);
} else if (choice == '!') {
cout << "Enter an factorial integer without the !:" << endl;
cin >> n;
primeFactorSet = factorialPrimes(n);
n = factorial(n);
} else {
main();
}
if (!primeFactorSet.empty()) {
cout << "Prime factors: ";
for (const auto &factor: primeFactorSet) {
cout << factor << ", ";
}
cout << endl;
cout << "Phi \u03A6(" << n << ") = " << phi(n, primeFactorSet) << endl;
}
}

//compute Euler's Totient Function
cpp_int phi(cpp_int n) {
// Base case: if n is 1, return 1
if (n == 1) {
return 1;
//finds the set of prime factors for a factorial
set<cpp_int> factorialPrimes(cpp_int n) {
set<cpp_int> primes;
// Check each number from 2 to n for primality
for (cpp_int i = 2; i <= n; i++) {
bool isPrime = true;
// Check if i is divisible by any number from 2 to sqrt(i)
for (cpp_int j = 2; j <= sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
// If i is prime, add it to the set of primes
if (isPrime) {
primes.insert(i);
}
}
return primes;
}

// Initialize result to n
cpp_int result = n;

//retrieve the prime factors for Euler's Totient function
set<cpp_int> primeFactors(cpp_int n) {
set<cpp_int> factors;

// Check for factors of n up to the square root of n
for (cpp_int i = 2; i * i <= n; i++) {
// If i is a factor of n, remove all factors of i from n
if (n % i == 0) {
while (n % i == 0) {
factors.insert(i);
n /= i;
}

// Update result to account for i not being relatively prime to n
result -= result / i;
}
}

// If n is still greater than 1, it must be a prime factor
// so update result accordingly
// If n is still greater than 1, then it must be a prime factor
if (n > 1) {
result -= result / n;
factors.insert(n);
}

// Remove non-prime factors from the set
set<cpp_int> primeFactors;
for (const auto &factor: factors) {
bool is_prime = true;
for (cpp_int i = 2; i * i <= factor; i++) {
if (factor % i == 0) {
is_prime = false;
break;
}
}
if (is_prime) {
primeFactors.insert(factor);
}
}

// Return the final value of result
return primeFactors;
}

//compute Euler's Totient Function
cpp_int phi(cpp_int n, set<cpp_int> primeFactors) {
// Base case: if n is 1, return 1
if (n == 1) {
return 1;
}

// Compute phi using the formula
cpp_int result = n;
for (const auto &factor: primeFactors) {
result -= result / factor;
}
return result;
}

@@ -367,25 +423,50 @@ void bigMaths() {
cpp_int adder = a + b;
cpp_int subB = a - b;
cpp_int subA = b - a;
cpp_int multer = a * b;

cpp_int modB = a % b;
cpp_int divA = (a - modB) / b;

cpp_int modA = b % a;
cpp_int divB = (b - modA) / a;

// cpp_int expA = boost::multiprecision::pow(a, b.convert_to<int>());

// cpp_int expB = boost::multiprecision::pow(a, b.convert_to<int>());


cout << a << " + " << b << " = " << adder << endl;
cout << a << " - " << b << " = " << subB << endl;
cout << b << " - " << a << " = " << subA << endl;
cout << a << " * " << b << " = " << multer << endl;
cout << a << " / " << b << " = " << divA << " R" << modB << endl;
cout << b << " / " << a << " = " << divB << " R" << modA << endl;
//cout << a << " ^ " << b << " = " << expA << endl;
// cout << b << " ^ " << a << " = " << expB << endl;
cout << a << " - " << b << " = " << subB << endl;
cout << b << " - " << a << " = " << subA << endl;
cout << a << " / " << b << " = " << divA << " R" << modB << endl;
cout << b << " / " << a << " = " << divB << " R" << modA << endl;
string input;

cout << "Do you want to compute multiples? y/n" << endl;
cin >> input;
if (input[0] == 'y' || input[0] == 'Y') {
cpp_int multer = a * b;
cout << a << " * " << b << " = " << multer << endl;
cout << "Do you want to compute exponents?" << endl;
cin >> input;
if (input[0] == 'y' || input[0] == 'Y') {
//exponent calculations seem to still fail
cpp_int expA = 1;
for (cpp_int i = 0; i < b; ++i) {
expA *= a;
}
cpp_int expB = 1;
for (cpp_int i = 0; i < a; ++i) {
expB *= b;
}
cout << a << " ^ " << b << " = " << expA << endl;
cout << b << " ^ " << a << " = " << expB << endl;
}
}
}

//calculates the factorial of a given number n
cpp_int factorial(cpp_int n) {
cpp_int result = 1;
// Multiply result by each integer from 1 to n
for (cpp_int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
9 changes: 8 additions & 1 deletion crypt.h
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
#ifndef CRYPTKEEPER_CRYPT_H
#define CRYPTKEEPER_CRYPT_H

#include <set>
#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
@@ -40,7 +41,13 @@ void modularExp();

void eulerHelper();

cpp_int phi(cpp_int n);
set<cpp_int> factorialPrimes(cpp_int n);

set<cpp_int> primeFactors(cpp_int n);

cpp_int factorial(cpp_int n);

cpp_int phi(cpp_int n, set<cpp_int> primeFactors);

class crypt {