Технический форум
Вернуться   Технический форум > Программирование > Форум программистов > Помощь студентам


Ответ
 
Опции темы Опции просмотра
Старый 11.12.2014, 16:00   #1 (permalink)
denisova
Member
 
Регистрация: 13.04.2013
Сообщений: 12
Сказал(а) спасибо: 0
Поблагодарили 0 раз(а) в 0 сообщениях
Репутация: 10
Unhappy Нужна помощь в Matlab

Расшифруйте закон, зашифрованный в следующей строке:
s = '/ b_t3{$ H~MO 6JTQI>
Зашифруйте текст , содержащийся в файле gettysburg .txt , и
текст самой функции encrypt.m.

Функция encrypt:
function encrypt(filein,fileout)
%ENCRYPT Apply the CRYPTO function to a text file.
% ENCRYPT
% with no arguments prompts for a input file name.
% ENCRYPT inputfilename
% encrypts the text in the input file and displays the results.
% ENCRYPT inputfilename outputfilename
% stores the encrypted text in the output file.
% See also CRYPTO.

% Copyright 2012 Cleve Moler and The MathWorks, Inc.

if nargin < 1
filein = input('Input file = ','s');
end
fin = fopen(filein);

if nargin == 2
fout = fopen(fileout,'w');
else
fout = 1;
end

while ~feof(fin)
s = fgetl(fin);
t = crypto(s);
fprintf(fout,'%s\n',t);
end

fclose all;

Функция crypto:
function y = crypto(x)
% CRYPTO Cryptography example.
% y = crypto(x) converts an ASCII text string into another, coded string.
% The function is its own inverse, so crypto(crypto(x)) gives x back.
% See also: ENCRYPT.

% Copyright 2012 Cleve Moler and The MathWorks, Inc.

% Use a two-character Hill cipher with arithmetic modulo 97, a prime.
p = 97;

% Choose two characters above ASCII 128 to expand set from 95 to 97.
c1 = char(169);
c2 = char(174);
x(x==c1) = 127;
x(x==c2) = 128;

% Convert to integers mod p.
x = mod(real(x-32),p);

% Reshape into a matrix with 2 rows and floor(length(x)/2) columns.
n = 2*floor(length(x)/2);
X = reshape(x(1:n),2,n/2);

% Encode with matrix multiplication modulo p.
A = [71 2; 2 26];
Y = mod(A*X,p);

% Reshape into a single row.
y = reshape(Y,1,n);

% If length(x) is odd, encode the last character.
if length(x) > n
y(n+1) = mod((p-1)*x(n+1),p);
end

% Convert to ASCII characters.
y = char(y+32);
y(y==127) = c1;
y(y==128) = c2;
Вложения
Тип файла: txt gettysburg.txt (1.4 Кб, 519 просмотров)
denisova вне форума   Ответить с цитированием

Старый 11.12.2014, 16:00
Helpmaster
Member
 
Аватар для Helpmaster
 
Регистрация: 08.03.2016
Сообщений: 0

Информативные ответы вы можете найти в схожих обсуждениях

Помогите с задачей в MATLAB
Нужна помощь по МК
Помогите пожалуйста MATLAB

Ads

Яндекс

Member
 
Регистрация: 31.10.2006
Сообщений: 40200
Записей в дневнике: 0
Сказал(а) спасибо: 0
Поблагодарили 0 раз(а) в 0 сообщениях
Репутация: 55070
Ответ

Опции темы
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Выкл.
HTML код Выкл.
Trackbacks are Вкл.
Pingbacks are Вкл.
Refbacks are Выкл.




Часовой пояс GMT +4, время: 21:47.

Powered by vBulletin® Version 6.2.5.
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.