I'm trying to implement a simple RSA encryption/decryption process, and I'm pretty sure I've got the equations around the right way. Although it doesn't seem to be printing out the correct decrypted value after the encryption. Any ideas?.
//test program
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
int gcd(int a, int b);
int main(){
char character = 'A'; //character that is to be encrypted
int p = 7;
int q = 5;
int e = 0; // just initializing to 0, assigning actual e value in the 1st for loop
int n = p*q;
int phi = (p-1)*(q-1);
int d = 0; // " " 2nd for loop
//---------------------------finding 'e' with phi. where "1 < e < phi(n)"
for (int i=2; i < phi; i++){
if (gcd(i,phi) == 1){ //if gcd is 1
e = i;
break;
}
}
//----------------------------
//---------------------------finding 'd'
for (int i = 2; i < phi; i++){
int temp = (e*i)%phi;
if (temp == 1){
d = i;
break;
}
}
printf("n:%d , e:%d , phi:%d , d:%d \n",n,e,phi,d);
printf("\npublic key is:[%d,%d]\n",e,n);
printf("private key is:[%d,%d]\n",d,n);
int m = static_cast<int>(character); //converting to a number
printf("\nconverted character num:%d\n",m);
//Encryption part ie. c = m^e MOD n
int power = pow(m,e); // m^e
int c = power%n; // c = m^e MOD n. ie. encrypted character
printf("\n\nEncrypted character number:%d\n",c);
//decryption part, ie. m = c^d MOD n
power = pow(c,d);
int m2 = power%n;
printf("\n\ndecrypted character number:%d\n",m2);
return 0;
}
int gcd(int a, int b){
int r;
if (a < 0) a = -a;
if (b < 0) b = -b;
if (b > a) {
r = b; b = a; a = r;
}
while (b > 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
(The prime numbers being used are 5 and 7, for the test)
Here I'm converting the character 'A' to its numeric value which is of course 65. When I encrypt this value using c = m^e MOD n (where m is the converted value, i.e. 65) it gives me c as 25.
Now, to reverse the process, I do m = c^d MOD n, which gives me m as 30 ... which really isn't correct because it should be 65, no?
Where exactly have I gone wrong?
[edit]
Is my calculation of d correct?
The encrypted message m must be less than n. You can't use values larger than n, because the calculations are done modulo n. In your case m=65 and n=35. So you are actually getting the correct answer modulo n, because 65 % 35 == 30.
It is caused by having m greater than or equal to n like #interjay already answered.
But I found another problem with your code, my gcc4.1.2 compiler output 24 for the encrypted value not 25. It is because you use pow() function and then convert the result (which is type double) to int that causes precision loss.
Don't use pow() function, instead use square and multiply modulo n algorithm to compute c = m^e MOD n and m = c^d MOD n. It is faster than pow() and you won't need to unsafely downcast the result to integer.
Related
Given 3 numbers a b c get a^b , b^a , c^x where x is abs diff between b and a cout each one but mod 10^9+7 in ascending order.
well I searched web for how to use the distributive property but didn't understand it since I am beginner,
I use very simple for loops so understanding this problem is a bit hard for me so how can I relate these mod rules with powers too in loops? If anyone can help me I would be so happy.
note time limit is 1 second which makes it harder
I tried to mod the result every time in the loop then times it by the original number.
for example if 2^3 then 1st loop given variables cin>>a,a would be 2, num =a would be like this
a = (a % 10^9 + 7) * num this works for very small inputs but large ones it exceed time
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
long long a,b,c,one,two,thr;
long long x;
long long mod = 1e9+7;
cin>>a>>b>>c;
one = a;
two = b;
thr = c;
if (a>=b)
x = a - b;
else
x = b - a;
for(int i = 0; i < b-1;i++)
{
a = ((a % mod) * (one%mod))%mod;
}
for(int j = 0; j < a-1;j++)
{
b = ((b % mod) * (two%mod))%mod;
}
for(int k = 0; k < x-1;k++)
{
c = ((c % mod) * (thr%mod))%mod;
}
}
I use very simple for loops [...] this works for very small inputs, but large ones it exceeds time.
There is an algorithm called "exponentiation by squaring" that has a logarithmic time complexity, rather then a linear one.
It works breaking down the power exponent while increasing the base.
Consider, e.g. x355. Instead of multiplying x 354 times, we can observe that
x355 = x·x354 = x·(x2)177 = x·x2·(x2)176 = x·x2·(x4)88 = x·x2·(x8)44 = x·x2·(x16)22 = x·x2·(x32)11 = x·x2·x32·(x32)10 = x·x2·x32·(x64)5 = x·x2·x32·x64·(x64)4 = x·x2·x32·x64·(x128)2 = x1·x2·x32·x64·x256
That took "only" 12 steps.
To implement it, we only need to be able to perform modular multiplications safely, without overflowing. Given the value of the modulus, a type like std::int64_t is wide enough.
#include <iostream>
#include <cstdint>
#include <limits>
#include <cassert>
namespace modular
{
auto exponentiation(std::int64_t base, std::int64_t exponent) -> std::int64_t;
}
int main()
{
std::int64_t a, b, c;
std::cin >> a >> b >> c;
auto const x{ b < a ? a - b : b - a };
std::cout << modular::exponentiation(a, b) << '\n'
<< modular::exponentiation(b, a) << '\n'
<< modular::exponentiation(c, x) << '\n';
return 0;
}
namespace modular
{
constexpr std::int64_t M{ 1'000'000'007 };
// We need the mathematical modulo
auto from(std::int64_t x)
{
static_assert(M > 0);
x %= M;
return x < 0 ? x + M : x;
}
// It assumes that both a and b are already mod M
auto multiplication_(std::int64_t a, std::int64_t b)
{
assert( 0 <= a and a < M and 0 <= b and b < M );
assert( b == 0 or a <= std::numeric_limits<int64_t>::max() / b );
return (a * b) % M;
}
// Implements exponentiation by squaring
auto exponentiation(std::int64_t base, std::int64_t exponent) -> std::int64_t
{
assert( exponent >= 0 );
auto b{ from(base) };
std::int64_t x{ 1 };
while ( exponent > 1 )
{
if ( exponent % 2 != 0 )
{
x = multiplication_(x, b);
--exponent;
}
b = multiplication_(b, b);
exponent /= 2;
}
return multiplication_(b, x);
}
}
I am solving a problem of code forces. Here is the problem link -> Problem Link
My code passes 9 test cases out of 10 and the 10th case is this
100
??b?a?a???aca?c?a?ca??????ac?b???aabb?c?ac??cbca???a?b????baa?ca??b???cbc??c??ab?ac???c?bcbb?c??abac
and the error I got is this
wrong answer expected '331264319', found '-2013109745'
Diagnostics detected issues [cpp.clang++-diagnose]: p71.cpp:14:20: runtime error: signed integer overflow: 3 * 965628297 cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior p71.cpp:14:20 in
Other test cases
6 ac?b?c output - 24
7 ??????? output - 2835
9 cccbbbaaa output - 0
100 accbaccabccbbbbabacabaaccacbcbcababbbcbcbcccabcbbc?caaabcabcaaccbccabaaaaccacabbaabcbbccbbababaac output - 14634
This all test cases gives the right answer except the 1st on
and my code which I was submitted is this
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; cin>>n;
string s; cin>>s;
int e=1, a=0, ab=0, abc=0;
for(int i=0; i<n; i++){
if(s[i] == 'a') a+=e;
else if(s[i]=='b') ab+=a;
else if(s[i]=='c') abc+=ab;
else if(s[i]=='?') {
abc = 3*abc+ab;
ab = 3*ab+a;
a = 3*a+e;
e = 3*e;
}
}
cout<<abc<<endl;
return 0;
}
I have tried these things -> Change int to long long int.
Here the output changes but is still wrong and negative. Output -> -1959750440526388721.
Then I tried using unsigned while declaring variables. This also gives me wrong and but not negative. Output -> 2281857551.
Since you need the result "modulo 10^9+7", you can reduce the result of all additions and multiplications "modulo 10^9+7" (i.e. find the remainder after division by 10^9+7 - this is what the % operator does).
In the code, you can either do this in each calculation or at the end of the loop. Applying the first option (and a few good habits) looks like this:
#include <iostream>
#include <string>
// Avoid using namespace std;
int main() {
unsigned n; std::cin >> n;
std::string s; std::cin >> s;
unsigned e = 1, a = 0, ab = 0, abc = 0; // We do not need negative numbers
unsigned m = 1000000007; // Calculate result modulo 10^9+7
for(unsigned i = 0; i < n; i++) {
if(s[i] == 'a') a = (a + e) % m;
else if(s[i]=='b') ab = (ab + a) % m;
else if(s[i]=='c') abc = (abc + ab) % m;
else if(s[i]=='?') {
abc = (3 * abc + ab) % m;
ab = (3 * ab + a) % m;
a = (3 * a + e) % m;
e = (3 * e) % m;
}
}
std::cout << abc << std::endl;
return 0;
}
Basically, not every integer is created equal. They have a max size in memory.
The issue is that there's not enough memory to represent such a large number, so the computer doesn't have enough space to represent your number.
EDIT:
A better solution would be to use the % operator to avoid these issues. According to the exercise, that's what's recommended
Old solution:
A solution would be to use a different type of int like a int64_t (or if exact width isn't needed then long long would work too)
I am trying to implement RSA encryption and whenever the decryption part is implemented, I get the wrong answer.
Everything up until encryption gives me the correct values: n is 187, phi is 160, e is 3, private key d is 107 and the cipher-text c is 183. Afterwards, I compute c^d first (which gives me -9223372036854775808) then do mod(n) on that result to get -162 (supposed decryption).
I presume that the error is in the c^d portion but I cannot put my finger on what is going wrong. Any help would be appreciated.
int main()
{
long p = 11;
long q = 17;
long n = p * q;
double phi = (p-1) * (q-1);
int e = 3;
while(e < phi) {
if(GCD(e, phi) == 1) break; //GCD is a function that returns the GCD
else e++;
}
int k = 2;
// private key computation
double d = (1+(k*phi))/e;
double msg = 72;
long c = pow(msg, e);
// c mod(n)
c %= n;
long decr = pow(c, d);
decr %= n;
return 0;
}
You can use following code to compute the modulus (O(n) complexity)
//Compute a^b mod n
int powermod(int a, int b, int n) {
int result = 1;
for (int i=1;i<=b;++i) {
result *= (a%n);
result %= n;
}
return result%n;
}
You can modify fast power in similar way to compute mod of exponent. It uses the following formula
a^b mod n = (a mod n)^b mod n
I was given a task to write a program that displays:
I coded this:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, n = 1, f = 1;
float s = 0;
cin >> a;
while(n <= a)
{
f = f * n;
s += 1 / (float)f;
n = n + 1;
}
cout << s;
getch();
}
So this displays -
s = 1 + 1/2! + 1/3! + 1/4! .... + 1/a!, including odd and even factorials.
For the past two hours I am trying to figure out how can I modify this code so that it displays the desired result. But I couldn't figure it out yet.
Question:
What changes should I make to my code?
You need to accumulate the sum while checking the counter n and only calculate the even factorials:
int n;
double sum = 1;
cin >> n;
for(int i = 2; i < n; ++i{
if(i % 2 == 0) sum += 1 / factorial(i);
}
In your code:
while(n <= a)
{
f = f * n;
// checks if n is even;
// n even if the remainder of the division by 2 is zero
if(n % 2 == 0){
s += 1 / (float)f;
}
n = n + 1;
}
12! is the largest value that fits in an 32 bit integer. You should use double for all the numbers. For even factorials, starting with f = 1 (0!), f = f * (n-1) * n, where n = 2, 4, 6, 8, ... .
You have almost everything you need in place (assuming you don't want to make design changes based on the issues brought up in the comments).
All you need to change is what you multiply f by in each step. To build up n! you are multiplying by n in each step. To build up (2n)! you would multiply by 2*n*(2*n-1)
Edit: Your second theory about what the instructor wants would need only slightly more of a change. Your inner loop could be replaced by
while(n < a)
{
f = f * n * (n+1);
s += 1 / f;
n = n + 2;
}
Edit2: To run your program I made several changes for I/O things you did that don't work in my copy of GCC. Hopefully those won't distract from the main point of the following code. I also added a second, more complicated and more accurate method of computing the answer to see how much was lost in floating point rounding.
So this code computes the answer twice, once by the method I suggested you change your code to and once by a more accurate method (using double instead of float and adding the numbers in the more accurate sequence via a recursive function). Then it display your answer and the difference between the two answers.
Running that shows the version I suggested gets all the displayed digits correct and is only wrong for the values of a I tried by tiny amounts that would need more display precision to notice:
#include<iostream>
using namespace std;
double fac_sum(int n, int a, double f)
{
if ( n > a )
return 0;
f *= n * (n-1);
return fac_sum(n+2, a, f) + 1 / f;
}
int main()
{
int a, n = 1;
float f = 1;
float s = 0;
cin >> a;
while(n < a)
{
f = f * n * (n+1);
s += 1 / f;
n = n + 2;
}
cout << s;
cout << " approx error was " << fac_sum( 2, a, 1.0)-s;
return 0;
}
For 8 that displays 0.54308 approx error was -3.23568e-08
I hope you understand the e-08 notation meaning the error is in the 8'th digit to the right of the .
Edit3: I changed f to float in this post because I had copied/tested thinking f was float, so parts of my answer didn't make sense when f was int
I need to use pow in my c++ program and if i call the pow() function this way:
long long test = pow(7, e);
Where
e is an integer value with the value of 23.
I always get 821077879 as a result. If i calculate it with the windows calculator i get 27368747340080916343.. Whats wrong here? ):
I tried to cast to different types but nothing helped here... What could be the reason for this? How i can use pow() correctly?
Thanks!
The result is doesn't fit in long long.
If you want to deal with very big numbers then use a library like GMP
Or store it as a floating point (which won't be as precise).
Applying modulo:
const unsigned int b = 5; // base
const unsigned int e = 27; // exponent
const unsigned int m = 7; // modulo
unsigned int r = 1; // remainder
for (int i = 0; i < e; ++i)
r = (r * b) % m;
// r is now (pow(5,27) % 7)
723 is too big to fit into a long long (assuming it's 64 bits). The value is getting truncated.
Edit: Oh, why didn't you say that you wanted pow(b, e) % m instead of just pow(b, e)? That makes things a whole lot simpler, because you don't need bigints after all. Just do all your arithmetic mod m. Pubby's solution works, but here's a faster one (O(log e) instead of O(e)).
unsigned int powmod(unsigned int b, unsigned int e, unsigned int m)
{
assert(m != 0);
if (e == 0)
{
return 1;
}
else if (e % 2 == 0)
{
unsigned int squareRoot = powmod(b, e / 2, m);
return (squareRoot * squareRoot) % m;
}
else
{
return (powmod(b, e - 1, m) * b) % m;
}
}
See it live: https://ideone.com/YsG7V
#include<iostream>
#include<cmath>
int main()
{
long double ldbl = pow(7, 23);
double dbl = pow(7, 23);
std::cout << ldbl << ", " << dbl << std::endl;
}
Output: 2.73687e+19, 2.73687e+19