Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
一只小余
Java--RSA算法
提交
16c07c66
J
Java--RSA算法
项目概览
一只小余
/
Java--RSA算法
与 Fork 源项目一致
Fork自
inscode / Java
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
Java--RSA算法
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
16c07c66
编写于
5月 08, 2023
作者:
6
64586190168b7e02f64e79c2
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Auto commit
上级
5b0ce43e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
92 addition
and
11 deletion
+92
-11
Main.java
Main.java
+92
-5
inscode.nix
inscode.nix
+0
-6
未找到文件。
Main.java
浏览文件 @
16c07c66
class
Main
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Hello world!"
);
}
}
\ No newline at end of file
import
java.io.PrintWriter
;
import
java.math.BigInteger
;
import
java.util.Scanner
;
/**
* RSA算法
*/
public
class
RSA
{
private
static
final
BigInteger
a
=
BigInteger
.
valueOf
(
'a'
);
private
static
final
Scanner
sc
=
new
Scanner
(
System
.
in
);
private
static
final
PrintWriter
print
=
new
PrintWriter
(
System
.
out
,
true
);
// 公钥
public
static
BigInteger
n
;
public
static
BigInteger
e
;
// 私钥
public
static
BigInteger
d
;
// 帮助信息
private
static
final
String
help
=
"1. 生成密钥 2. RSA加密 3. RSA解密 4.重置数据 5.输入密钥 -1. 退出"
;
public
static
void
main
(
String
[]
args
)
{
while
(
true
)
{
print
.
println
(
"当前数据"
+
(
n
==
null
?
""
:
" n = "
+
n
)
+
(
e
==
null
?
""
:
" e = "
+
e
)
+
(
d
==
null
?
""
:
" d = "
+
d
));
print
.
println
(
help
);
int
i
=
sc
.
nextInt
();
switch
(
i
)
{
case
1
->
createN
();
case
2
->
getCipher
();
case
3
->
getPlain
();
case
4
->
init
();
case
5
->
{
print
.
println
(
"请输入密钥 n,e,d"
);
n
=
sc
.
nextBigInteger
();
e
=
sc
.
nextBigInteger
();
d
=
sc
.
nextBigInteger
();
}
case
-
1
->
System
.
exit
(
0
);
}
}
}
private
static
void
init
()
{
n
=
null
;
e
=
null
;
d
=
null
;
}
private
static
void
getPlain
()
{
if
(
d
==
null
)
{
print
.
println
(
"请输入密钥"
);
d
=
sc
.
nextBigInteger
();
}
print
.
println
(
"请输入密文"
);
getModPow
(
d
);
}
private
static
void
getModPow
(
BigInteger
d
)
{
sc
.
next
().
chars
().
mapToObj
(
c
->
BigInteger
.
valueOf
(
c
-
'a'
)).
forEach
(
i
->
{
BigInteger
j
=
i
.
modPow
(
d
,
n
);
j
=
j
.
add
(
a
);
print
.
print
((
char
)
j
.
intValue
());
});
print
.
println
();
}
private
static
void
createN
()
{
print
.
println
(
"请输入两个质数p,q"
);
BigInteger
p
=
sc
.
nextBigInteger
();
BigInteger
q
=
sc
.
nextBigInteger
();
n
=
p
.
multiply
(
q
);
BigInteger
f
=
p
.
subtract
(
BigInteger
.
ONE
).
multiply
(
q
.
subtract
(
BigInteger
.
ONE
));
e
=
BigInteger
.
TWO
;
while
(!
f
.
gcd
(
e
).
equals
(
BigInteger
.
ONE
))
{
e
=
e
.
add
(
BigInteger
.
ONE
);
}
d
=
e
.
modInverse
(
f
);
print
.
println
(
"公钥: ("
+
n
+
","
+
e
+
")"
);
print
.
println
(
"私钥: ("
+
n
+
","
+
d
+
")"
);
}
private
static
void
getCipher
()
{
if
(
n
==
null
||
e
==
null
)
{
print
.
println
(
"请输入公钥n,e"
);
n
=
sc
.
nextBigInteger
();
e
=
sc
.
nextBigInteger
();
}
print
.
println
(
"请输入明文"
);
getModPow
(
e
);
}
}
inscode.nix
已删除
100644 → 0
浏览文件 @
5b0ce43e
{
pkgs
}:
{
deps
=
[
pkgs
.
graalvm17-ce
pkgs
.
maven
];
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录