RSA Encryption problem, C, e and N are given - rsa

I have the follwing task: the public key(e,N) = (5,299) and the encrypted message C = 60
Now I have to find the original message without knowing the private key d. So I come up with the following equation according to how C is calculated:
60 = M^5 mod 299, where M < n.
But I have no idea how to continue, trying each number from 0 until 298 isn't really a good method. Any help would be appreciated!

You can factorize 299 into 13 and 23, so you have p and q. And from there you can calculate phi(n), which is 12*22=264. Now you need to calculate the inverse of e, or solve the expression e * d = 1 mod 264. As 264 is 265-1 and e is 5 you can simply get to 265 by calculating 5*53. This will be your d. And now you have everything you need, as you can calculate 60^53 mod 299, which is 21, the original message M.
To verify the solution, calculate 21^5, which is 4084101. That mod 299 is 60, your original encrypted message C.

Related

Calculating d value in RSA

I saw a couple questions about this but most of them were answered in unhelpful way or didn't get a proper answer at all. I have these variables:
p = 31
q = 23
e - public key exponent = 223
phi - (p-1)*(q-1) = 660
Now I need to calculate d variable (which I know is equal 367). The problem is that I don't know how. I found this equation on the internet but it doesn't work (or I can't use it):
e⋅d=1modϕ(n)
When I see that equation i think that it means this:
d=(1modϕ(n))/e
But apparently it doesn't because 367 (1modϕ(n))/e = 1%660/223 = 1/223 != 367
Maybe I don't understand and I did something wrong - that's why I ask.
I did some more research and I found second equation:
d=1/e mod ϕ(n)
or
d=e^-1 mod ϕ(n)
But in the end it gives the same result:
1/e mod ϕ(n) = 1/223 % 660 = 1/223 != 367
Then I saw some guy saying that to solve that equation you need extended Euclidean algorithm If anyone knows how to use it to solve that problem then I'd be very thankful if you help me.
If you want to calculate something like a / b mod p, you can't just divide it and take division remainder from it. Instead, you have to find such b-1 that b-1 = 1/b mod p (b-1 is a modular multiplicative inverse of b mod p). If p is a prime, you can use Fermat's little theorem. It states that for any prime p, ap = a mod p <=> a(p - 2) = 1/a mod p. So, instead of a / b mod p, you have to compute something like a * b(p - 2) mod p. b(p - 2) can be computed in O(log(p))
using exponentiation by squaring. If p is not a prime, modular multiplicative inverse exists if and only if GCD(b, p) = 1. Here, we can use extended euclidean algorithm to solve equation bx + py = 1 in logarithmic time. When we have bx + py = 1, we can take it mod p and we have bx = 1 mod p <=> x = 1/b mod p, so x is our b-1. If GCD(b, p) ≠ 1, b-1 mod p doesn't exist.
Using either Fermat's theorem or the euclidean algorithm gives us same result in same time complexity, but the euclidean algorithm can also be used when we want to compute something modulo number that's not a prime (but it has to be coprime with numer want to divide by).

How to Increment a 3 digit Number(User Input) by 1 in Brainfuck?

How to Increment a 3 digit Number by 1 in Brainfuck?
For Example. Getting a User input of 699 this code should output 700.
This is something i Tried but its not working .
,>,>,+
<<.>.>.
The reason it doesn't work is because you don't have a 3 digit number in memory. You have three ASCII values from 48-57 (characters 0-9). I will explain how to do what you need to, but won't provider the actual BF code for multiplication etc, you can find those elsewhere.
Let's call the characters X, Y and Z
First of all we need to convert them to a number between 0 and 9. Assuming that the user enters digits only, we can do this by subtracting 48 (ASCII character 0) from the ASCII value.
Ok so now we have three numbers from 0-9 as follows:
A = X - 48
B = Y - 48
C = Z - 48
Problem is they're still three separate digits and not one number. What's the actual number? In this case the number can be built in the following way:
N = 100A + 10B + C
So, you need to multiply A by 100, B by 10, C by 1, and then adding them all together. Could be done as follows:
N = A
N = (N * 10) + B
N = (N * 10) + C
After you've done that, you have an actual number in one cell, and you can increment it by doing N = N + 1 which in BF is a single +
Now this would work for numbers up to 255, which is the largest number that a BF cell can hold. You want to work with larger numbers? It gets more complicated, because now you have to split that number up into two or more cells, propagating the carries yourself etc. I won't go into that here because that gets much more complex (though there are algorithms you can find online to help as well) and I think this is enough to get you started.
EDIT: I realized that your code is also trying to print the incremented value. This requires more work, because the . command outputs the value of that cell as is as an ASCII character. But you need to output three digits. To do that you'd need to reverse the process above to split the number into three digits as follows:
C = N % 10
N = N / 10
B = N % 10
N = N / 10
A = N
Then you need to convert them from a number 0-9 to ASCII characters again as follows:
X = A + 48
Y = B + 48
Z = C + 48
and then finally you can output characters X, Y and Z in that order which would be the human readable incremented number.

How should I use Cramer’s rule in Matlab?

The relationship between the two matrices is shown is Ax=B.
How do I find x using Cramer’s rule?
A=[521 202 -176 612;-761 41 -655 712;314 102 -234 891;612 291 209 -318]
B=[718;408;215;356]
You can use Cramer's rule like this for your specific 4x4 case. The element at index i of the result x is given by the ratio of 2 determinants (See the wikipedia link for a full explanation) - you can create the result with the following loop
x = ones(4,1);
a_det = det(A);
for i = 1:4
C = A;
C(:,i) = B;
x(i,1) = det(C)/a_det;
end
the column vector x should now be your result. There could be a faster way to do this but this should work. You can verify this by comparing the result with
x = A\B;

Algorithm for short IDs

I'm looking for an algorithm - or should I better say: encoding? - to compress integer numbers to short string IDs like URL shorteners use: http://goo.gl/0puu
Url safe base 64 comes close to it, but maybe there is something better.
Requirements:
as short as possible
url safe
"yi_H" called base64 "perfect" and after a bit more research I came to the same conclusion, since only the following characters could be used in URLs without worry:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 - _ . ~
Thats 66 characters, whereas base64 only uses 64 characters. The two more possible characters wouldn't be practical because 66 is not based on 2.
Conclusion: URL safe base64 (offered as part of Apache Commons for example) is perfect for short IDs.

Generate a random number in a certain range in MATLAB

How can I generate a random number in MATLAB between 13 and 20?
If you are looking for Uniformly distributed pseudorandom integers use:
randi([13, 20])
http://www.mathworks.com/help/techdoc/ref/rand.html
n = 13 + (rand(1) * 7);
r = 13 + 7.*rand(100,1);
Where 100,1 is the size of the desidered vector
ocw.mit.edu is a great resource that has helped me a bunch. randi is the best option, but if your into number fun try using the floor function with rand to get what you want.
I drew a number line and came up with
floor(rand*8) + 13
You can also use:
round(mod(rand.*max,max-1))+min
Generate values from the uniform distribution on the
interval [a, b].
r = a + (b-a).*rand(100,1);
Best solution is randint , but this function produce integer numbers.
You can use rand with rounding function
r = round(a + (b-a).*rand(m,n));
This produces Real random number between a and b , size of output matrix is m*n
if you are looking to generate all the number within a specific rang randomly then you can try
r = randi([a b],1,d)
a = start point
b = end point
d = how many number you want to generate but keep in mind that d should be less than or equal to b-a
If you need a floating random number between 13 and 20
(20-13).*rand(1) + 13
If you need an integer random number between 13 and 20
floor((21-13).*rand(1) + 13)
Note: Fix problem mentioned in comment "This excludes 20" by replacing 20 with 21