2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > matlab中rsa matlab - Matlab中的RSA代码 - 堆栈内存溢出

matlab中rsa matlab - Matlab中的RSA代码 - 堆栈内存溢出

时间:2022-01-03 19:34:30

相关推荐

matlab中rsa matlab - Matlab中的RSA代码 - 堆栈内存溢出

我想加密一条消息,例如“ HELO1234” ,然后解密以获取原始消息。我已经在matlab中编写了无法正常工作的RSA代码。

参数计算

temp=1;

range=1:10;

k=isprime(range)

prime_mat=range(find(k))

p=randsample(prime_mat,1);

q=randsample(prime_mat,1);

if(p~=q)

n=p*q;

phi_n=(p-1)*(q-1);

u=1:phi_n -1;

end

while temp

enckey=randsample(u,1);

deckey=randsample(u,1);

if(enckey~=deckey)

if(gcd(enckey,phi_n)~=1 ...

&& gcd(deckey,phi_n)~=1 ...

&&gcd(enckey*deckey,phi_n)~=1)

temp=1;

else

temp=0;

end

end

end

加密过程

char t= 'hello123';

t=uint8(t);

len=length(t)

pow=[];

cipher_text=[];

for i=1:len

pow=[pow;t(i).^(enckey)]; %each element of the pt matrix(plain text) is raised to the power of encryption key(enc_key) and stored in pow matrix(power matrix)

cipher_text=[cipher_text;mod(pow(i),n)];% cipher text is calculate

d

加密过程的输出

k =

0 1 1 0 1 0 1 0 0 0

prime_mat =

2 3 5 7

p =

7

q =

2

n =

14

enckey =

5

甲板=

1

phi_n =

6

len =

28

cipher_text =

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

解密过程

plain_text=[];

pow1=[];

len1=length(cipher_text);

for i=1:len

pow1=[pow1;cipher_text(i).^(deckey)]

plain_text=[plain_text;mod(pow1(i),n)]

uint8(纯文字);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。