This algorithm to generate the hash value using text character(My name is saurabh singh).how i can recover text character again using reverse technique? please give me suggestion?

5 views (last 30 days)
fname=input('Input File (in ASCII format)? ','s');
time1=clock;
%Open the input file and get the first line of data
fid=fopen(fname);
M = fread(fid);
fclose(fid);
ini=dec2bin(M(1),16);
for ii = 2:length(M)
ini=cat(2,ini,dec2bin(M(ii),8));
end
s2=length(ini)/8;
block_temp = [ ini, ...
'1', ...
num2str(zeros(mod(448-1-s2*8,512),1))' ...
dec2bin(s2*8,64) ];
nb=length(block_temp)/512;
block = reshape(block_temp,512,nb)';
% H(0) initialization
H = [ '193','198','195','195';...
'196','197','194','197';...
'199','192','200','197';...
'200','193','200','198';...
'200','195','200','198'] ;
for N = 1:length(block_temp)/512
% Generate Wt
Wt = WtgenSHA1( block(N,:) );
%use previous last H's
a_hex = H(1,:); a = hex2bin(a_hex);
b_hex = H(2,:); b = hex2bin(b_hex);
c_hex = H(3,:); c = hex2bin(c_hex);
d_hex = H(4,:); d = hex2bin(d_hex);
e_hex = H(5,:); e = hex2bin(e_hex);
% Kt initialization
for t = 1:80
T1 = bin2dec2(cls(a,5) );
%Number to Char
if t <= 20
f = xor(b&c, ~b&d);
elseif t <= 40
f = xor(xor(b,c),d);
elseif t <= 60
f = xor(xor(b&c,b&d),c&d);
elseif t <= 80
f = xor(xor(b,c),d);
else
error ('t must be in the range 1 to 80')
end
T2 = bin2dec2(f);
T3 = bin2dec2(e);
if t>=60
T4=floor(2^32*sqrt(10));
elseif t>=40
T4=floor(2^32*sqrt(5));
elseif t>=20
T4=floor(2^32*sqrt(3));
elseif t>0
T4=floor(2^32*sqrt(2));
end
% T4=bin2dec2(Kt(t,:));
T5 = bin2dec2(Wt(t,:) );
T = mod(T1+T2+T3+T4+T5,2^32);
e = d;
d = c;
c = cls(b,30);
b = a;
a = dec2bin(T,32);
%display_resultsSHA1(t,a,b,c,d,e) %receives column bits
end
H = calculate_new_HSHA1(H,a,b,c,d,e);
end
output=H;
time2=clock;
disp(etime(time2,time1));
  5 Comments

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 4 Jun 2013
I'm not sure that I understand the code, but based on what I do understand, I would say that you cannot recover the original text file.
You take the original text file, work with it a bit to get it into a form useful for your algorithm, and end up with block_temp (a vector that is a multiple of 512 in length) and block (the same data but reshaped to columns of length 512).
You then take the SHA1 of each block, storing the result in Wt.
Then, Wt gets encoded into what you are doing, but it does so only via the lines
T5 = bin2dec2(Wt(t,:) );
T = mod(T1+T2+T3+T4+T5,2^32);
with the parts above that in the loop being independent of Wt. Therefore if you knew the result, T, then you could back-calculate mod(T5, 2^32) which is plausibly enough range for each Wt row.
But you have not been clear as to what you know at the output side, so I am not certain that T is known. The way your code is structured it is plausible that what is known is the result of
H = calculate_new_HSHA1(H,a,b,c,d,e);
after the "for t" loop. If that is the case, if all you know is the HSHA1 after the "for t" loop, then because SHA1 is not reversible, you would not be able to effectively figure out the H, a, b, c, d, e that would be needed to create the SHA1 hash, and so would have next to now chance of reversing the mixing that you are doing (because you update your a, b, c, d, e at each "for t" step, with "a" being created from the probably-unrecorded T that results from the mod() that is influence by the wt that is from the SHA1 hash of the block.)
If somehow you were able to reverse from the H all the way back to the the wt, you would still have the problem that wt is the SHA1 hash of the input block of text, not the input block itself. And SHA1 is not reversible in any reasonable number of steps, not even if you were to emit as many SHA1 output bits for each block as the size of the block itself. In cases where your output is fewer bits then the original total file size (the most common mode of operation) then the best you would be able to do is to find some input file that gave you the right hash, rather than recovering the input file for sure.
SHA1 is a hash, not an encryption.
It appears to me that what you showed might be an implementation of SHA1 itself. If so then reversing to find the bits would be an "attack" on SHA1. According to wiki the current best estimate for the number of computations to find a message that produces a given hash is approximate 2^61 trials.
If you were asked to break SHA1 as a homework exercise then either your teacher is sadistic or else you are going to a very elite university.

Categories

Find more on Create Large-Scale Model Components in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!