In today’s world, data security and encryption are critical. Bob needs to send a secret code (S) to his boss securely. To achieve this, he encrypts the code using two key values (N and M) through a mathematical formula:Encrypted Code=(((SNmod 10)M)mod 1000000007)\text{Encrypted Code} = (((S^N \mod 10)^M) \mod 1000000007)Encrypted Code=(((SNmod10)M)mod1000000007)
This encryption ensures that even if someone intercepts the message, decoding it without the key values is nearly impossible.
The encryption formula can be broken down into three steps:
last_digit = S % 10
last_digit
when raised to different powers.effective_exponent = (N % cycle_length)
.X = (last_digit ^ effective_exponent) % 10
.encrypted_code = (X ^ M) % 1000000007
.def mod_exponentiation(base, exp, mod):
result = 1
while exp > 0:
if exp % 2 == 1:
result = (result * base) % mod
base = (base * base) % mod
exp //= 2
return result
def encrypt_code(S, N, M):
last_digit = S % 10
# Identify last digit cycle
cycles = {0: [0], 1: [1], 2: [2, 4, 8, 6], 3: [3, 9, 7, 1], 4: [4, 6],
5: [5], 6: [6], 7: [7, 9, 3, 1], 8: [8, 4, 2, 6], 9: [9, 1]}
cycle_list = cycles[last_digit]
cycle_length = len(cycle_list)
effective_exponent = mod_exponentiation(N, 1, cycle_length) - 1
X = cycle_list[effective_exponent]
# Compute final encrypted code
encrypted_code = mod_exponentiation(X, M, 1000000007)
return encrypted_code
# Example usage
S, N, M = map(int, input("Enter S, N, M: ").split())
print("Encrypted Code:", encrypt_code(S, N, M))
Why this works efficiently:
#include <iostream>
#include <vector>
using namespace std;
#define MOD 1000000007
// Function to perform modular exponentiation
long long mod_exponentiation(long long base, long long exp, long long mod) {
long long result = 1;
while (exp > 0) {
if (exp % 2 == 1) result = (result * base) % mod;
base = (base * base) % mod;
exp /= 2;
}
return result;
}
// Encryption function
long long encrypt_code(long long S, long long N, long long M) {
int last_digit = S % 10;
vector<vector<int>> cycles = {
{0}, {1}, {2, 4, 8, 6}, {3, 9, 7, 1}, {4, 6},
{5}, {6}, {7, 9, 3, 1}, {8, 4, 2, 6}, {9, 1}
};
vector<int> cycle_list = cycles[last_digit];
int cycle_length = cycle_list.size();
long long effective_exponent = mod_exponentiation(N, 1, cycle_length) - 1;
long long X = cycle_list[effective_exponent];
return mod_exponentiation(X, M, MOD);
}
int main() {
long long S, N, M;
cout << "Enter S, N, M: ";
cin >> S >> N >> M;
cout << "Encrypted Code: " << encrypt_code(S, N, M) << endl;
return 0;
}
import java.math.BigInteger;
import java.util.Scanner;
public class SecretCodeEncryption {
static final BigInteger MOD = BigInteger.valueOf(1000000007);
// Function to perform modular exponentiation
public static BigInteger modExponentiation(BigInteger base, BigInteger exp, BigInteger mod) {
BigInteger result = BigInteger.ONE;
while (exp.compareTo(BigInteger.ZERO) > 0) {
if (exp.mod(BigInteger.TWO).equals(BigInteger.ONE)) {
result = result.multiply(base).mod(mod);
}
base = base.multiply(base).mod(mod);
exp = exp.divide(BigInteger.TWO);
}
return result;
}
public static BigInteger encryptCode(BigInteger S, BigInteger N, BigInteger M) {
int lastDigit = S.mod(BigInteger.TEN).intValue();
int[][] cycles = {
{0}, {1}, {2, 4, 8, 6}, {3, 9, 7, 1}, {4, 6},
{5}, {6}, {7, 9, 3, 1}, {8, 4, 2, 6}, {9, 1}
};
int[] cycleList = cycles[lastDigit];
int cycleLength = cycleList.length;
int effectiveExponent = modExponentiation(N, BigInteger.ONE, BigInteger.valueOf(cycleLength)).intValue() - 1;
int X = cycleList[effectiveExponent];
return modExponentiation(BigInteger.valueOf(X), M, MOD);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter S, N, M: ");
BigInteger S = sc.nextBigInteger();
BigInteger N = sc.nextBigInteger();
BigInteger M = sc.nextBigInteger();
sc.close();
System.out.println("Encrypted Code: " + encryptCode(S, N, M));
}
}
This guide provided an optimized and scalable approach to encrypt Bob’s secret code using modular exponentiation. The techniques used ensure fast computation even for extremely large numbers.