MATLAB实现SHA-512/SHA-256等加密算法以及生成随机的0/1二进制码流

牟辰龙
2023-12-01

SHA (Secure Hash Algorithm,译作安全散列算法) 是美国国家安全局 (NSA) 设计,美国国家标准与技术研究院 (NIST) 发布的一系列密码散列函数。

特点是一次一密,无法破解

下面直接上MATLAB代码


首先是生成Hash函数的m文件

inp -> 你的密钥

meth -> 加密方案,可选MD2, MD5, SHA-1, SHA-256, SHA-384, SHA-512

function h = hash(inp,meth)
% HASH - Convert an input variable into a message digest using any of
%        several common hash algorithms
%
% USAGE: h = hash(inp,'meth')
%
% inp  = input variable, of any of the following classes:
%        char, uint8, logical, double, single, int8, uint8,
%        int16, uint16, int32, uint32, int64, uint64
% h    = hash digest output, in hexadecimal notation
% meth = hash algorithm, which is one of the following:
%        MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512
%
% NOTES: (1) If the input is a string or uint8 variable, it is hashed
%            as usual for a byte stream. Other classes are converted into
%            their byte-stream values. In other words, the hash of the
%            following will be identical:
%                     'abc'
%                     uint8('abc')
%                     char([97 98 99])
%            The hash of the follwing will be different from the above,
%            because class "double" uses eight byte elements:
%                     double('abc')
%                     [97 98 99]
%            You can avoid this issue by making sure that your inputs
%            are strings or uint8 arrays.
%        (2) The name of the hash algorithm may be specified in lowercase
%            and/or without the hyphen, if desired. For example,
%            h=hash('my text to hash','sha256');
%        (3) Carefully tested, but no warranty. Use at your own risk.
%        (4) Michael Kleder, Nov 2005
%
% EXAMPLE:
%
% algs={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'};
% for n=1:6
%     h=hash('my sample text',algs{n});
%     disp([algs{n} ' (' num2str(length(h)*4) ' bits):'])
%     disp(h)
% end

inp=inp(:);
% convert strings and logicals into uint8 format
if ischar(inp) || islogical(inp)
    inp=uint8(inp);
else % convert everything else into uint8 format without loss of data
    inp=typecast(inp,'uint8');
end

% verify hash method, with some syntactical forgiveness:
meth=upper(meth);
switch meth
    case 'SHA1'
        meth='SHA-1';
    case 'SHA256'
        meth='SHA-256';
    case 'SHA384'
        meth='SHA-384';
    case 'SHA512'
        meth='SHA-512';
    otherwise
end
algs={'MD2','MD5','SHA-1','SHA-256','SHA-384','SHA-512'};
if isempty(strcmp(algs,meth))
    error(['Hash algorithm must be ' ...
        'MD2, MD5, SHA-1, SHA-256, SHA-384, or SHA-512']);
end

% create hash
x=java.security.MessageDigest.getInstance(meth);
x.update(inp);
h=typecast(x.digest,'uint8');
h=dec2hex(h)';
if(size(h,1))==1 % remote possibility: all hash bytes  128, so pad:
    h=[repmat('0',[1 size(h,2)]);h];
end
h=lower(h(:)');
clear x
return



下面是利用得到的哈希密文来生成0/1二进制码流,主要思想是,利用得到的密文的每一位,来当做密钥,用hash函数再加密.最后判断密文是字母还是数字.字母记为1,数字记为0,这样就得到了一串二进制码流.

function random_sequence = get_hash_sequence(K,meth)
    clc;
    hash_str = hash(K,meth);
    hash_str_len = length(hash_str);
    random_sequence = zeros([1,hash_str_len^2]);
    random_sequence_index = 1;
    count_1 = 1;
    for i=1:hash_str_len
        hash_temp_str = hash(hash_str(i:i),meth);
        hash_temp_str = isletter(hash_temp_str);
        for j=1:hash_str_len
            random_sequence(random_sequence_index) = hash_temp_str(j);
            if hash_temp_str(j) == 1
                count_1 = count_1 + 1;
            end
            random_sequence_index = random_sequence_index + 1;
        end
    end
    
    % 查看我们生成的二进制码流有多少个1
    disp(count_1);
    
    % 查看我们生成的二进制码流组成图片是什么样的.
    img_hash = zeros([hash_str_len,hash_str_len]);
    for i =1:hash_str_len
        for j =1:hash_str_len
            img_hash(i,j) = random_sequence((i-1)*hash_str_len + j);
        end
    end
    figure;
    imshow(img_hash);    
end

参考: https://www.ilovematlab.cn/thread-147875-1-1.html

 类似资料: