求问大神此程序哪里有问题 40C

报错显示
调用 "load_plaintext" 时,未对输出参数 "plaintext_matrix" (可能还包括其他参数)赋值
代码如下

 key_dir = 'D:/Users/Fault/aes/AES/';
 keyfile_name = 'key.mat';
 Kname = sprintf('%s\\%s', key_dir, keyfile_name);
 load(Kname);

 plaintext_dir = 'D:/Users/Fault/heihei/';
 plaintext_name = 'plaintext';
 plaintext_length = 128;
 amount = 5000;
 unit_length = 8;

  plaintext_matrix = load_plaintext( plaintext_dir, plaintext_name, plaintext_length, amount, unit_length );
 plaintext_hex = P;
plaintext_hex = {'00' '11' '22' '33' '44' '55' '66' '77' ...
                  '88' '99' 'aa' 'bb' 'cc' 'dd' 'ee' 'ff'};

 [s_box, inv_s_box, w, poly_mat, inv_poly_mat] = aes_init(key, 14);

num_P = size(P,1);
 plaintext = cell(num_P,1);
 for i = 1:num_P
    plaintext{i,:} = (hex2dec (plaintext_hex(i,:)))';
 end

  Nr = 14;
  inductor = round(rand(1,1+(Nr-1)*4+3));
  x = inductor(1);
  fault_point = randi(3);
  selector = randi(1+(Nr-1)*4+3);
  bitmap= inductor;
  for i =1:fault_point
    bitmap(selector)=1;
    bitmap();
  end
for i = 1:num_P
     fault = 0;
     C_normal{i,:} = cipher (plaintext{i,:}, w, s_box, poly_mat, 14, fault);

     fault = 1;
     C_fault{i,:} = cipher (plaintext{i,:}, w, s_box, poly_mat, 14, fault);

     T_base_start{i,:} = bitxor(C_normal{i,:}, C_fault{i,:});
 end


 w_base = w;
 w_base(57:60,:) = 0;

 for i = 1:num_P
     [P_normal, normal_base] = inv_cipher (C_normal{i,:}, w_base, inv_s_box, inv_poly_mat, 14);
     inter_normal_base{i,:} = normal_base;

     [P_fault, fault_base] = inv_cipher (C_fault{i,:}, w_base, inv_s_box, inv_poly_mat, 14);
     inter_fault_base{i,:} = fault_base;

     T_base_end{i,:} = bitxor(inter_normal_base{i,:}, inter_fault_base{i,:});

     [P_normal, inter_normal] = inv_cipher (C_normal{i,:}, w, inv_s_box, inv_poly_mat, 14);
    T_k_normal{i,:} = bitxor(inter_normal, inter_normal_base{i,:});

     [P_normal, inter_fault] = inv_cipher (C_fault{i,:}, w, inv_s_box, inv_poly_mat, 14);
    T_k_fault{i,:} = bitxor(inter_fault, inter_fault_base{i,:});
 end

 for j = 1:num_P
     for i = 1:pow2(8)
         guess = i - 1;
        w_guess = w_base;
         w_guess(57,1) = guess;

         [P_normal, inter_normal_guess] = inv_cipher (C_normal{j,:}, w_guess, inv_s_box, inv_poly_mat, 14);
         T_k_normal_guess{i,j} = bitxor(inter_normal_guess, inter_normal_base{j,:});

         [P_normal, inter_fault_guess] = inv_cipher (C_fault{j,:}, w_guess, inv_s_box, inv_poly_mat, 14);
         T_k_fault_guess{i,j} = bitxor(inter_fault_guess, inter_fault_base{j,:});
     end
 end

 for j = 1:num_P
     for i = 1:pow2(8)
         temp = bitxor(bitxor(T_k_normal{j,:},T_k_normal_guess{i,j}),bitxor(T_k_fault{j,:},T_k_fault_guess{i,j}));
         T_k_diff(i,j) = temp(1,1);
     end
 end

 product = 0;
 Q = zeros(1,num_P);
 for j = 1:num_P
     tmp = T_k_diff(:,j);
     ind = find(tmp == 0);
     product = product + size(ind,1);
     Q(1,j) = product/num_P;
 end
 product = product/num_P;
 plot(Q);

load('D:/Users/Fault/AES DATA/Q.mat');
plot(Q);

load_plaintext代码如下

%UNTITLED4 此处显示有关此函数的摘要load_plaintext
%   index_dir: 索引文件所在目录
%   name:索引文件名
%   format: 索引文件格式
%       -- key_length:密钥比特长度
%       -- plaintext_length:明文比特长度
%       -- offset_length:偏移量比特长度
%   amount: 所需的明文数据量


%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
if ~(exist('index_dir', 'var'))
    error('The directory of index files must be defined');
end

if ~(exist('name', 'var'))
   error('The name of index files must be defined'); 
end

if ~(exist('length', 'var'))
   error('The length of index must be defined'); 
end

if ~(exist('amount', 'var'))
   error('The amount of index must be defined'); 
end


if exist(index_dir, 'dir')
    index_file = sprintf('%s%s.txt',index_dir, name);
    if exist(index_file, 'file')
        fid = fopen(index_file);
        %tmp = textscan(fid, '%s %s %s %s %s %s');
        %plaintext_origin = tmp{:,2};
        tmp = textscan(fid, '%s');
        plaintext_origin = tmp{:,1}; 
        clear tmp;

        num = size(plaintext_origin,1);
        for i = 1:num
           for j = 1:(length/unit_length)
               A = plaintext_origin{i,:};
               plaintext(i,j) = {A(j*2-1:j*2)};
           end
        end
        if amount == 'all'
           plaintext_matrix = plaintext; 
        else
            plaintext_matrix = plaintext(1:amount,:); 
        end

    end

else
    error('index file is not exist');
end

end


2个回答

检查 load_plaintext 不是所有路径都有返回值

可以在最后一个end前面加上返回一个默认值的代码

当你的 if exist(index_file, 'file') 是Flase的时候plaintext_matrix就没有值呀

liujianchen_linux
liujianchen_linux 回复刘震昊: 看看这个文件在不在 D:/Users/Fault/heihei/plaintext.txt
8 个月之前 回复
liuheihei11
liuheihei11 为啥会是Flase呢
8 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问