I have the following lines of code :
%for RSA Algorithm
clc;
disp('Implementation of RSA Algorithm');
clear all;
close all;
p = input('\nEnter value of p: ');
q = input('\nEnter value of q: ');
[Pk,Phi,d,e] = init(p,q);
M = input('\nEnter message: ','s');
x=length(M);
c=0;
for j= 1:x
for i=0:122
if strcmp(M(j),char(i))
c(j)=i;
end
end
end
disp('ASCII Code of the entered Message:');
disp(c);
% For Encryption
for j= 1:x
cipher(j)= crypto(c(j),Pk,e);
end
disp('Cipher Text of the entered Message:');
disp(cipher);
How can I know the text of encrypted message ? I mean what are the characters of the encryption ? I want the ASCII code of cipher to be converted back into characters. Can someone tell me the matlab code for this ?
Modular exponentiation - the main mathematical primitive used for RSA encryption/decryption uses integer values as input and output. The RSA algorithms themselves (in PKCS#1 v2.1 onwards) first convert messages to octets, then pads these objects according to one of two padding schemes, then perform modular exponentiation on the result converted to integer. The resulting integer in turn is converted to octets again.
Now depending on the actually performed calculation described above, you should have either an integer or octets. As it is not hard to convert an integer to octets - or to print an integer - lets focus only on octets. You need to create a string (text) from something that is inherently not text. There are specific encoding schemes that do this, but the most common one is base 64.
Note that usually you only perform one RSA encryption per plaintext. If the plaintext does not fit (or may not fit in the future, or if you just want to adhere to standards) then you should use hybrid encryption instead of RSA "in ECB mode".
Related
I've been working in an algorithm that I found in this link: Pure Lua implementation of md5 to use in an embedded system, but it uses strings as input.
For my specific application, the hash need to receive an hex array like that: 85202599ee7e0165ee32be43336755595955544554554747.
How can I change the function bellow to calculate the hash using the array above?
function md5.Calc(s)
local msgLen=string.len(s)
local padLen=56- msgLen % 64
if msgLen % 64 > 56 then padLen=padLen+64 end
if padLen==0 then padLen=64 end
s=s..string.char(128)..string.rep(string.char(0),padLen-1)
s=s..leIstr(8*msgLen)..leIstr(0)
assert(string.len(s) % 64 ==0)
local t=md5.consts
local a,b,c,d=t[65],t[66],t[67],t[68]
for i=1,string.len(s),64 do
local X=leStrCuts(string.sub(s,i,i+63),4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4)
assert(#X==16)
X[0]=table.remove(X,1) -- zero based!
a,b,c,d=md5.transform(a,b,c,d,X)
end
local swap=function (w) return beInt(leIstr(w)) end
return string.format("%08x%08x%08x%08x",swap(a),swap(b),swap(c),swap(d))
end
return md5.Calc("85202599ee7e0165ee32be43336755595955544554554747"); -- returns: cc238dfd3cf48d588714774efeaf9a1f
-- but I need a return like that calculated in https://cryptii.com/pipes/md5-hash: c7e9f863554dc9a410c414f8758b307d
Lua strings can hold binary data: each "character" then is one byte. This is what is being used in above code. You can see that characters 128 (0x80 or 0b10000000) and zero are used for the padding.
So you should not change the above code at all; instead you should decode the hexadecimal string to a binary string and use that as input to above function.
You can find an interesting, concise method of hex decoding here.
I am working on compressing an arbitrary vector with MATLAB, which provides factory methods for Huffman Coding: huffmandict, huffmanenco, huffmandeco.
The huffmandict function produces a lookup table mapping each symbol in the signal we want to encode to its corresponding codeword which is needed to encode and then decode the signal.
It is trivial to generate the dictionary when you know the input vector. But say I'm compressing to send from Alice to Bob - I can't assume Bob knows the dictionary too - so Alice needs to send the dictionary along with the huffman code!
Is there a way in MATLAB of generating a bitstream representation of the dictionary to be prepended to our huffman code to allow for it to be decoded at the other end?
What I'm thinking is the resulting code looks like if N is the length of the encoded dictionary:
(N encoded as 8 bits)(huffman dict encoded in N bits)(huffman code)
It seems odds that MATLAB provides quite powerful factory methods for the encoding but then does not even bother to make it actually usable in digital transmission with a lot of extra work.
I understand that in the theory, a huffman tree is often built - is there a way to generate this in MATLAB, and then convert such tree back to a dictionary?
I know of two efficient code expression methods used in JPEG and gzip but as I understand they require the dictionary to be canonical, meaning that every branch on the right side (starting with 1) have to be longer.
So you have to convert the code to a canonical form since there are 2^n (n being the number of codewords) designs.
Canonical has the same expected length.
Then you can express each symbol by the length of its branch, limiting to a reasonable number like 2^4 (meaning 4 bits for each symbol).
Ok, let's get to the code, for the vector to be sent:
for i = 1:size(dict,1)
L(i) = numel(dict{i,2})
end
In the receiving side you have to do a little more (I assume there is some fixed order in your codewords labels):
k = 0;
for l = 1:16
k = k * 2;
for j = find(L==l)
d{j,1} = j;
d{j,2} = de2bi(k, 'left-msb', l);
k = k + 1;
end
end
For converting to canonical form you need only to convert your tree to vector format and back.
Here is the code:
x = rand(5)*100;
save('pqfile.txt','x','-ascii','-tabs')
The above works, but:
x = rand(5)*100;
x = uint8(x);
save('pqfile.txt','x','-ascii','-tabs')
says:
Warning: Attempt to write an unsupported data type to an ASCII file.
Variable 'x' not written to file.
Does anyone know why this happens? How come I can't save the data when it is uint8. I have to read data into a VHDL testbench so was experimenting. I guess the only option is to save my 8 bit unsigned integer values in 2d array using printf then read into the test bench.
ASCII option
The save method is somewhat restrictive in what it can support, and then it uses floating point notation to represent your numbers which bloats your file when dealing with a limited range of numbers like you are (i.e. uint8, 0 to 255).
Check out dlmwrite as an alternative (documentation here).
It takes the filename to write/save to, the variable to store, and some additional parameters, like the delimiter you want to separate your values with.
For your example, it looks like this
x = rand(5)*100;
x = uint8(x);
dlmwrite('pqfile.txt',x,'\t');
Binary option
If you are looking to stored your uint8 data as single bytes then you probably want go with a custom binary file instead instead of ASCII. (Yes, you can convert uint8 to single ASCII characters but you run into issues with these values being interpreted with your delimiters; newlines or tabs.)
fid=fopen('pqfile.dat','wb');
if(fid>2)
fwrite(fid,size(x),'*uint8'); % Note: change data type here you are dealing with more than 255 rows or columns
fwrite(fid,x','*uint8'); % Transpose x (with x') so it is stored in row order.
fclose(fid);
else
fprintf(1,'Could not open the file for writing.\n');
end
I'm not sure what type of parser you are using for your VHDL, but this will pack your data into a file with a short header of the expected dimensions followed by one long row of your serialized data.
To read it back in with MATLAB, you can do this:
fid = fopen('pqfile.dat','rb');
szX = fread(fid,2,'uint8');
x = fread(fid,szX,'*uint8')'; % transpose back if you are dealing with matlab.
fclose(fid);
The transpose operations are necessary for MATLAB because it reads data column-wise, whereas most other languages (in my experience) read row-wise.
I am trying to implement block truncation coding (BTC) on an image in matlab. In order to do BTW you have to calculate the mean and standard deviation of each 4x4 block of pixels. However, I need to store the mean as a variable number of bits as in the number of bits that the mean will be stored in is passed into the function that calculates the mean. I'm not sure how to do this part, can anyone help?
An easy and clean approach to variable bit lengths-encoding would require the use of the fixed-point toolbox. E.g. as follows
function o = encode1(val, numBits)
o = fi(val, 0, numBits, 0)
If you are rather bound to pure Matlab you could just and them away and 'simulate' the precision loss if you only want to benchmark your encoding.
function o = encode2(val, numBits)
o = bitand(uint8(val), 256 - 2^(8-numBits));
On the other hand, if you're planning to actually encode into a file and not just simulate the encoding, you would need to establish a bit-stream which is not byte-aligned. This can be a bit tiring to do. Trading off efficiency for ease of implementation, you could use dec2bin to work with a string of '0' and '1' characters. Again, toolboxes can be of help here, e.g. the communication systems toolbox provides the de2bi function.
I'm trying to understand how a system I'm working on creates some hash code starting from a numeric code. I'm collecting some of these pairs “small_number, big_number”, but I cannot figure out how the system encodes small_number to obtain big_number. Decoding is possible, because system can obtain small_number from big_number.
Numbers look like these:
197 >> 29337857947107767585
1078 >> 84080031635590040762
1083 >> 32373003898332638476
1409 >> 79402294967209014727
1498 >> 25254493910542918727
2945 >> 85687067382221703925
2946 >> 88767616208189692328
I have no clue at all. Can you point to some reading too?
Thank you
If the system can reverse the function, the function isn't a hashing function at all, but a cipher.
It looks like the output is always a 20 digit number - did you try testing it on non-numeric input, or strings larger than 20 digits?
In either case, its likely that the system uses a well known encryption algorithm (my guess is AES or DES), so without the key, it's infeasible for you to guess at the function.
Worse, if the system is not directly taking the input but adding some other information, you might have the right algorithm and key but still not realize it.