How can i apply hash function on a string and reverse process to generate the same string from the hash value.

 Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 18 Apr. 2020

2 Stimmen

Hashing is not a reversible process. You can do encoding and decoding. For example
A = 'The quick brown fox jumps over the lazy dog';
encoded_str = matlab.net.base64encode(A);
decoded_str = char(matlab.net.base64decode(encoded_str));
Result
>> encoded_str
encoded_str =
'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw=='
>> decoded_str
decoded_str =
'The quick brown fox jumps over the lazy dog'

10 Kommentare

Balkar Singh
Balkar Singh am 19 Apr. 2020
The length of string A is 44 while the length of encoded string is 60. Can we find the relation between the both length.
You can read more about the encoding here: https://tools.ietf.org/html/rfc4648#section-4. The number of characters in an encoded string is given by
A = 'Ameer Hamza KhanTmer the lazy dog';
encoded_str = matlab.net.base64encode(A);
n1 = numel(encoded_str);
n2 = ceil(numel(A)/3)*4;
Result:
>> n1
n1 =
60
>> n2
n2 =
60
Thanks sir
s3 = containers.Map({'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','1','2','3','4','5','6','7','8','9',' ','!'},{'110000','110001','110010','110011','110100','110101','110110','110111','111000','111001','111010','111011','111100','111101','111110','111111','000000','000001','000010','000011','000100','000101','000110','000111','001000','001001','010000','010001','010010','010011','010100','010101','010110','010111','011000','011001','011010','011011','011100','011101','011110','011111','100000','100001','100010','100011','100100','100101','100110','100111','101000','101001','001011','001100','001101','001110','001111','101010','101011','101100','101101','101110','101111'});
When i compare the encoded_str with this container.I got the following error
Error using containers.Map/subsref
Specified key type does not match the type expected for this container.
what is the solution of this problem.
Ameer Hamza
Ameer Hamza am 20 Apr. 2020
Can you show which line gives this error?
Balkar Singh
Balkar Singh am 20 Apr. 2020
Thanks sir..I solved it.
s3 = containers.Map({'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','1','2','3','4','5','6','7','8','9','','!','0'},{'110000','110001','110010','110011','110100','110101','110110','110111','111000','111001','111010','111011','111100','111101','111110','111111','000000','000001','000010','000011','000100','000101','000110','000111','001000','001001','010000','010001','010010','010011','010100','010101','010110','010111','011000','011001','011010','011011','011100','011101','011110','011111','100000','100001','100010','100011','100100','100101','100110','100111','101000','101001','001011','001100','001101','001110','001111','101010','101011','101100','101101','101110','101111','001010'});
encoded_str=mlreportgen.utils.hash(cover_str);
i=1;
cnt = length(encoded_str);
HBS='';
while(cnt>0)
encoded_str(1,i)
val=s3(encoded_str(1,i)) Error occured here
i=i+1
cnt = cnt-1;
%input("Hello")
HBS=strcat(HBS,val);
end
Error using containers.Map/subsref
The specified key is not present in this container.
Ameer Hamza
Ameer Hamza am 20 Apr. 2020
What value of cover_str are you using?
Balkar Singh
Balkar Singh am 20 Apr. 2020
Bearbeitet: Ameer Hamza am 20 Apr. 2020
cover_str = 'Most teachers waste their time by asking questions that are indended to discover what the pupil does not know, whereas the true art of questioning is to discover what the pupil does know or is capable of knowing.';
Balkar, mlreportgen.utils.hash returns a string, which is different from char array in MATLAB. length(encoded_str) returns 1 always. Change it to char array by changing the line
encoded_str=mlreportgen.utils.hash(cover_str);
to
encoded_str=char(mlreportgen.utils.hash(cover_str));
Balkar Singh
Balkar Singh am 20 Apr. 2020
OK sir..Thank you very much

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by