Matlab RSA变量乘法溢出

Matlab RSA变量乘法溢出,matlab,Matlab,我正试图为RSA算法编写一个代码,我的代码正确地生成(e,d,n),但当我计算密码时问题就出现了。 密码=(普通^e)模n。 因为(普通^e)是一个非常大的数字。 计算器给出了正确的结果,而Matlab没有。有人有主意吗 clc e=61; d=21; n= 187; Neuler=160; Plain='ABCD'; Plain=uint64(Plain); %%Encrypting Progress for i=1:length (Plain); Cypher(i)=mod((Plain(i

我正试图为RSA算法编写一个代码,我的代码正确地生成(e,d,n),但当我计算密码时问题就出现了。 密码=(普通^e)模n。 因为(普通^e)是一个非常大的数字。 计算器给出了正确的结果,而Matlab没有。有人有主意吗

clc
e=61;
d=21;
n= 187;
Neuler=160;
Plain='ABCD';
Plain=uint64(Plain);
%%Encrypting Progress
for i=1:length (Plain);
Cypher(i)=mod((Plain(i)^e),n);
end
Numeri_Cypher=uint64(Cypher);
for i=1:length (Numeri_Cypher);
RPlain(i)=mod((Numeri_Cypher(i)^d),n);
end
Result=char(RPlain)

内置函数
mod
无法处理大整数。所以我创建了一个模块求幂的小实现,如果你使用这个函数,你应该没有问题

function result = modpow(base,exp,m) 
   result = 1;
   while (exp > 0) 
      if bitand(exp,1) > 0
         result = mod((result * base),m);
      end
      exp = bitshift(exp,-1);
      base = mod(base^2,m);
   end
end
示例:

使用内置的mod功能:

mod(3^233249,4)

ans = 0 %incorrect result
使用modpow函数

modpow(3,233249,4) 

ans = 1 %correct result

你能告诉我们你尝试过的实际代码吗?请将你的代码编辑成你的原始代码question@Hesham你应该编辑你的问题并在那里添加代码。顺便说一句,您应该使用模幂算法而不是内置模。@Suever'a的ASCII为65(65^61)=3.87003377459384292737866010116E+110