您已获得n 和e,您需要在解码之前找到对应的d。我不明白您为什么要尝试使用e 进行解码。
您的过程至少包含一种类型,使用message[i] 而不是msg[i]。另请参阅下面代码中的 cmets。
找到d 后,我得到编码整数 11393739244 解码为整数 87,它对应于 ASCII 字符“W”(不是您建议的“wha”或“Wha”)。
我不明白你打算对块大小做什么,所以我不得不猜测。下面我展示了 A) 一次一个字符或 B) 一次使用三个字符完成的编码/解码。我相信您意识到一次编码一个字符并不是一个好主意。此外,在另一个论坛的重复帖子中,您写道您不关心针对攻击的安全性。 (您还在那里写道,这不是家庭作业,但在这里看起来更像是,IMO。)
如果您在编写和使用 rsa 过程时遇到问题,那么您可能会发现各种拆分/集中/填充操作也很困难。
您在评论中写道,当您尝试在程序rsa 中使用您的初始尝试时,“它不会返回任何东西”。如果它作为未评估的调用返回,那么您创建 proc 并分配它的尝试可能实际上并没有奏效。如果您在文档中使用 Maple 的默认 2D 输入模式时遇到问题,请考虑将您的首选项切换到工作表中的 1D Maple Notation 输入。这是 Maple 标准 Java GUI 的两个首选项。
注意。我使用 Maple 的numtheory[lambda] 命令来查找“最小整数 i,使得所有 g 与 n 互质,g^i 与 1 模数一致”。在最近的 Maple 版本中,这也可以作为命令 NumberThoery:-CarmichaelLambda 使用。另见here。
restart;
# The procedure `rsa` below can be used to both encode or
# decode an integer.
#
# Conversion from/to ASCII is done separately, before/after.
rsa := proc(key::list(posint), msg::list(posint))
local ct, pe, pm, i;
pm := key[1];
pe := key[2];
## The original used `message` instead of `msg`, which was
## a careless typo. But iterated list concatenation like this
## is inefficient. Better to just use `seq`, as below.
## Also, use inert `&^` instead of `^` in the call to `mod`
## since the latter inefficiently computes the power
## explicitly (before taking the modulus).
#ct := [];
# for i to nops(msg) do ct := [op(ct), `mod`(msg[i] &^ pe, pm)]
#end do;
ct := map(u->`mod`(u &^ pe, pm), msg);
return ct;
end proc:
# You supplied (n,e) and you'll need to find d in order to decode.
n:=26271227347;
n := 26271227347
L := numtheory[lambda](n);
L := 13135445468
e:=11546465;
e := 11546465
evalb( e < L ); # a requirement
true
evalb( gcd(e, L) = 1); # a requirement
true
d := 1/e mod L;
d := 7567915453
# Now decode the number you supplied.
res := rsa([n,d],[11393739244]);
res := [87]
with(StringTools):
# So what ASCII character is that?
convert(res,bytes);
"W"
s := "Wha":
sb := map(convert,convert(s,bytes),string);
sb := ["87", "104", "97"]
sbn := map(parse,sb);
sbn := [87, 104, 97]
encoded := rsa([n,e],sbn);
encoded := [11393739244, 9911682959, 21087186892]
decoded := rsa([n,d],encoded);
decoded := [87, 104, 97]
pad := proc(str::string)
local r;
r := irem(length(str),3);
cat(seq("0",i=1..`if`(r=0,0,3-r)), str);
end proc:
map(pad, map(convert,decoded,string));
["087", "104", "097"]
cat(op(map(u->convert(map(parse,[LengthSplit(convert(u,string),3)]),
bytes), %)));
"Wha"
newsb := [cat(op(map(SubstituteAll,map(PadLeft,sb,3)," ","0")))];
newsb := ["087104097"]
newsbn := map(parse,newsb);
newsbn := [87104097]
encoded := rsa([n,e],newsbn);
encoded := [15987098394]
decoded := rsa([n,d],%);
decoded := [87104097]
map(pad, map(convert,decoded,string));
["087104097"]
cat(op(map(u->convert(map(parse,[LengthSplit(convert(u,string),3)]),
bytes), %)));
"Wha"