Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
醒狮指南
JavaGuide
提交
a17a2800
J
JavaGuide
项目概览
醒狮指南
/
JavaGuide
与 Fork 源项目一致
从无法访问的项目Fork
通知
5
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
JavaGuide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
a17a2800
编写于
7月 31, 2018
作者:
S
Snailclimb
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加了常见安全算法的源代码
上级
e81ef419
变更
16
隐藏空白更改
内联
并排
Showing
16 changed file
with
844 addition
and
0 deletion
+844
-0
数据结构与算法/source code/securityAlgorithm/.classpath
数据结构与算法/source code/securityAlgorithm/.classpath
+26
-0
数据结构与算法/source code/securityAlgorithm/.gitignore
数据结构与算法/source code/securityAlgorithm/.gitignore
+1
-0
数据结构与算法/source code/securityAlgorithm/.project
数据结构与算法/source code/securityAlgorithm/.project
+23
-0
数据结构与算法/source code/securityAlgorithm/.settings/org.eclipse.core.resources.prefs
...urityAlgorithm/.settings/org.eclipse.core.resources.prefs
+4
-0
数据结构与算法/source code/securityAlgorithm/.settings/org.eclipse.jdt.core.prefs
...de/securityAlgorithm/.settings/org.eclipse.jdt.core.prefs
+5
-0
数据结构与算法/source code/securityAlgorithm/.settings/org.eclipse.m2e.core.prefs
...de/securityAlgorithm/.settings/org.eclipse.m2e.core.prefs
+4
-0
数据结构与算法/source code/securityAlgorithm/pom.xml
数据结构与算法/source code/securityAlgorithm/pom.xml
+37
-0
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/Base64Demo.java
.../java/com/snailclimb/ks/securityAlgorithm/Base64Demo.java
+49
-0
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/DesDemo.java
...ain/java/com/snailclimb/ks/securityAlgorithm/DesDemo.java
+101
-0
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/IDEADemo.java
...in/java/com/snailclimb/ks/securityAlgorithm/IDEADemo.java
+46
-0
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/MD5.java
...rc/main/java/com/snailclimb/ks/securityAlgorithm/MD5.java
+161
-0
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/MD5Demo.java
...ain/java/com/snailclimb/ks/securityAlgorithm/MD5Demo.java
+50
-0
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/RSADemo.java
...ain/java/com/snailclimb/ks/securityAlgorithm/RSADemo.java
+250
-0
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/SHA1Demo.java
...in/java/com/snailclimb/ks/securityAlgorithm/SHA1Demo.java
+45
-0
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/readme
.../src/main/java/com/snailclimb/ks/securityAlgorithm/readme
+4
-0
数据结构与算法/source code/securityAlgorithm/src/test/java/com/snailclimb/ks/securityAlgorithm/AppTest.java
...est/java/com/snailclimb/ks/securityAlgorithm/AppTest.java
+38
-0
未找到文件。
数据结构与算法/source code/securityAlgorithm/.classpath
0 → 100644
浏览文件 @
a17a2800
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry
kind=
"src"
output=
"target/classes"
path=
"src/main/java"
>
<attributes>
<attribute
name=
"optional"
value=
"true"
/>
<attribute
name=
"maven.pomderived"
value=
"true"
/>
</attributes>
</classpathentry>
<classpathentry
kind=
"src"
output=
"target/test-classes"
path=
"src/test/java"
>
<attributes>
<attribute
name=
"optional"
value=
"true"
/>
<attribute
name=
"maven.pomderived"
value=
"true"
/>
</attributes>
</classpathentry>
<classpathentry
kind=
"con"
path=
"org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"
>
<attributes>
<attribute
name=
"maven.pomderived"
value=
"true"
/>
</attributes>
</classpathentry>
<classpathentry
kind=
"con"
path=
"org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"
>
<attributes>
<attribute
name=
"maven.pomderived"
value=
"true"
/>
</attributes>
</classpathentry>
<classpathentry
kind=
"output"
path=
"target/classes"
/>
</classpath>
数据结构与算法/source code/securityAlgorithm/.gitignore
0 → 100644
浏览文件 @
a17a2800
/target/
数据结构与算法/source code/securityAlgorithm/.project
0 → 100644
浏览文件 @
a17a2800
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>
securityAlgorithm
</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>
org.eclipse.jdt.core.javabuilder
</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>
org.eclipse.m2e.core.maven2Builder
</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>
org.eclipse.jdt.core.javanature
</nature>
<nature>
org.eclipse.m2e.core.maven2Nature
</nature>
</natures>
</projectDescription>
数据结构与算法/source code/securityAlgorithm/.settings/org.eclipse.core.resources.prefs
0 → 100644
浏览文件 @
a17a2800
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
数据结构与算法/source code/securityAlgorithm/.settings/org.eclipse.jdt.core.prefs
0 → 100644
浏览文件 @
a17a2800
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5
数据结构与算法/source code/securityAlgorithm/.settings/org.eclipse.m2e.core.prefs
0 → 100644
浏览文件 @
a17a2800
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
数据结构与算法/source code/securityAlgorithm/pom.xml
0 → 100644
浏览文件 @
a17a2800
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.snailclimb.ks
</groupId>
<artifactId>
securityAlgorithm
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<packaging>
jar
</packaging>
<name>
securityAlgorithm
</name>
<url>
http://maven.apache.org
</url>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
3.8.1
</version>
<scope>
test
</scope>
</dependency>
<!-- Base64 -->
<dependency>
<groupId>
commons-codec
</groupId>
<artifactId>
commons-codec
</artifactId>
<version>
1.8
</version>
</dependency>
<dependency>
<groupId>
org.bouncycastle
</groupId>
<artifactId>
bcprov-jdk15on
</artifactId>
<version>
1.56
</version>
</dependency>
</dependencies>
</project>
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/Base64Demo.java
0 → 100644
浏览文件 @
a17a2800
package
com.snailclimb.ks.securityAlgorithm
;
import
java.io.UnsupportedEncodingException
;
import
java.util.Base64
;
public
class
Base64Demo
{
public
static
void
main
(
String
[]
args
)
throws
UnsupportedEncodingException
{
// TODO Auto-generated method stub
CommonsCodecDemo
();
bouncyCastleDemo
();
jdkDemo
();
}
static
String
str
=
"你若安好,便是晴天"
;
/**
* commons codec实现Base64加密解密
*/
public
static
void
CommonsCodecDemo
()
{
// 加密:
byte
[]
encodeBytes
=
org
.
apache
.
commons
.
codec
.
binary
.
Base64
.
encodeBase64
(
str
.
getBytes
());
System
.
out
.
println
(
"commons codec实现base64加密: "
+
new
String
(
encodeBytes
));
// 解密:
byte
[]
decodeBytes
=
org
.
apache
.
commons
.
codec
.
binary
.
Base64
.
decodeBase64
(
encodeBytes
);
System
.
out
.
println
(
"commons codec实现base64解密: "
+
new
String
(
decodeBytes
));
}
/**
* bouncy castle实现Base64加密解密
*/
public
static
void
bouncyCastleDemo
()
{
// 加密
byte
[]
encodeBytes
=
org
.
bouncycastle
.
util
.
encoders
.
Base64
.
encode
(
str
.
getBytes
());
System
.
out
.
println
(
"bouncy castle实现base64加密: "
+
new
String
(
encodeBytes
));
// 解密
byte
[]
decodeBytes
=
org
.
bouncycastle
.
util
.
encoders
.
Base64
.
decode
(
encodeBytes
);
System
.
out
.
println
(
"bouncy castle实现base64解密:"
+
new
String
(
decodeBytes
));
}
public
static
void
jdkDemo
()
throws
UnsupportedEncodingException
{
// 加密
String
encodeBytes
=
Base64
.
getEncoder
().
encodeToString
(
str
.
getBytes
(
"UTF-8"
));
System
.
out
.
println
(
"JDK实现的base64加密: "
+
encodeBytes
);
//解密
byte
[]
decodeBytes
=
Base64
.
getDecoder
().
decode
(
encodeBytes
.
getBytes
(
"UTF-8"
));
System
.
out
.
println
(
"JDK实现的base64解密: "
+
new
String
(
decodeBytes
));
}
}
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/DesDemo.java
0 → 100644
浏览文件 @
a17a2800
package
com.snailclimb.ks.securityAlgorithm
;
import
java.io.UnsupportedEncodingException
;
import
java.security.SecureRandom
;
import
javax.crypto.spec.DESKeySpec
;
import
javax.crypto.SecretKeyFactory
;
import
javax.crypto.SecretKey
;
import
javax.crypto.Cipher
;
/**
* DES加密介绍 DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法。DES加密算法出自IBM的研究,
* 后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少,因为DES使用56位密钥,以现代计算能力,
* 24小时内即可被破解。虽然如此,在某些简单应用中,我们还是可以使用DES加密算法,本文简单讲解DES的JAVA实现 。
* 注意:DES加密和解密过程中,密钥长度都必须是8的倍数
*/
public
class
DesDemo
{
public
DesDemo
()
{
}
// 测试
public
static
void
main
(
String
args
[])
{
// 待加密内容
String
str
=
"cryptology"
;
// 密码,长度要是8的倍数
String
password
=
"95880288"
;
byte
[]
result
;
try
{
result
=
DesDemo
.
encrypt
(
str
.
getBytes
(),
password
);
System
.
out
.
println
(
"加密后:"
+
new
String
(
result
));
byte
[]
decryResult
=
DesDemo
.
decrypt
(
result
,
password
);
System
.
out
.
println
(
"解密后:"
+
new
String
(
decryResult
));
}
catch
(
UnsupportedEncodingException
e2
)
{
// TODO Auto-generated catch block
e2
.
printStackTrace
();
}
catch
(
Exception
e1
)
{
e1
.
printStackTrace
();
}
}
// 直接将如上内容解密
/**
* 加密
*
* @param datasource
* byte[]
* @param password
* String
* @return byte[]
*/
public
static
byte
[]
encrypt
(
byte
[]
datasource
,
String
password
)
{
try
{
SecureRandom
random
=
new
SecureRandom
();
DESKeySpec
desKey
=
new
DESKeySpec
(
password
.
getBytes
());
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
SecretKeyFactory
keyFactory
=
SecretKeyFactory
.
getInstance
(
"DES"
);
SecretKey
securekey
=
keyFactory
.
generateSecret
(
desKey
);
// Cipher对象实际完成加密操作
Cipher
cipher
=
Cipher
.
getInstance
(
"DES"
);
// 用密匙初始化Cipher对象,ENCRYPT_MODE用于将 Cipher 初始化为加密模式的常量
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
securekey
,
random
);
// 现在,获取数据并加密
// 正式执行加密操作
return
cipher
.
doFinal
(
datasource
);
// 按单部分操作加密或解密数据,或者结束一个多部分操作
}
catch
(
Throwable
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 解密
*
* @param src
* byte[]
* @param password
* String
* @return byte[]
* @throws Exception
*/
public
static
byte
[]
decrypt
(
byte
[]
src
,
String
password
)
throws
Exception
{
// DES算法要求有一个可信任的随机数源
SecureRandom
random
=
new
SecureRandom
();
// 创建一个DESKeySpec对象
DESKeySpec
desKey
=
new
DESKeySpec
(
password
.
getBytes
());
// 创建一个密匙工厂
SecretKeyFactory
keyFactory
=
SecretKeyFactory
.
getInstance
(
"DES"
);
// 返回实现指定转换的
// Cipher
// 对象
// 将DESKeySpec对象转换成SecretKey对象
SecretKey
securekey
=
keyFactory
.
generateSecret
(
desKey
);
// Cipher对象实际完成解密操作
Cipher
cipher
=
Cipher
.
getInstance
(
"DES"
);
// 用密匙初始化Cipher对象
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
securekey
,
random
);
// 真正开始解密操作
return
cipher
.
doFinal
(
src
);
}
}
\ No newline at end of file
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/IDEADemo.java
0 → 100644
浏览文件 @
a17a2800
package
com.snailclimb.ks.securityAlgorithm
;
import
java.security.Key
;
import
java.security.Security
;
import
javax.crypto.Cipher
;
import
javax.crypto.KeyGenerator
;
import
javax.crypto.SecretKey
;
import
javax.crypto.spec.SecretKeySpec
;
import
org.apache.commons.codec.binary.Base64
;
import
org.bouncycastle.jce.provider.BouncyCastleProvider
;
public
class
IDEADemo
{
public
static
void
main
(
String
args
[])
{
bcIDEA
();
}
public
static
void
bcIDEA
()
{
String
src
=
"www.xttblog.com security idea"
;
try
{
Security
.
addProvider
(
new
BouncyCastleProvider
());
//生成key
KeyGenerator
keyGenerator
=
KeyGenerator
.
getInstance
(
"IDEA"
);
keyGenerator
.
init
(
128
);
SecretKey
secretKey
=
keyGenerator
.
generateKey
();
byte
[]
keyBytes
=
secretKey
.
getEncoded
();
//转换密钥
Key
key
=
new
SecretKeySpec
(
keyBytes
,
"IDEA"
);
//加密
Cipher
cipher
=
Cipher
.
getInstance
(
"IDEA/ECB/ISO10126Padding"
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
key
);
byte
[]
result
=
cipher
.
doFinal
(
src
.
getBytes
());
System
.
out
.
println
(
"bc idea encrypt : "
+
Base64
.
encodeBase64String
(
result
));
//解密
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
key
);
result
=
cipher
.
doFinal
(
result
);
System
.
out
.
println
(
"bc idea decrypt : "
+
new
String
(
result
));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/MD5.java
0 → 100644
浏览文件 @
a17a2800
package
com.snailclimb.ks.securityAlgorithm
;
public
class
MD5
{
/*
*四个链接变量
*/
private
final
int
A
=
0x67452301
;
private
final
int
B
=
0xefcdab89
;
private
final
int
C
=
0x98badcfe
;
private
final
int
D
=
0x10325476
;
/*
*ABCD的临时变量
*/
private
int
Atemp
,
Btemp
,
Ctemp
,
Dtemp
;
/*
*常量ti
*公式:floor(abs(sin(i+1))×(2pow32)
*/
private
final
int
K
[]={
0xd76aa478
,
0xe8c7b756
,
0x242070db
,
0xc1bdceee
,
0xf57c0faf
,
0x4787c62a
,
0xa8304613
,
0xfd469501
,
0x698098d8
,
0x8b44f7af
,
0xffff5bb1
,
0x895cd7be
,
0x6b901122
,
0xfd987193
,
0xa679438e
,
0x49b40821
,
0xf61e2562
,
0xc040b340
,
0x265e5a51
,
0xe9b6c7aa
,
0xd62f105d
,
0x02441453
,
0xd8a1e681
,
0xe7d3fbc8
,
0x21e1cde6
,
0xc33707d6
,
0xf4d50d87
,
0x455a14ed
,
0xa9e3e905
,
0xfcefa3f8
,
0x676f02d9
,
0x8d2a4c8a
,
0xfffa3942
,
0x8771f681
,
0x6d9d6122
,
0xfde5380c
,
0xa4beea44
,
0x4bdecfa9
,
0xf6bb4b60
,
0xbebfbc70
,
0x289b7ec6
,
0xeaa127fa
,
0xd4ef3085
,
0x04881d05
,
0xd9d4d039
,
0xe6db99e5
,
0x1fa27cf8
,
0xc4ac5665
,
0xf4292244
,
0x432aff97
,
0xab9423a7
,
0xfc93a039
,
0x655b59c3
,
0x8f0ccc92
,
0xffeff47d
,
0x85845dd1
,
0x6fa87e4f
,
0xfe2ce6e0
,
0xa3014314
,
0x4e0811a1
,
0xf7537e82
,
0xbd3af235
,
0x2ad7d2bb
,
0xeb86d391
};
/*
*向左位移数,计算方法未知
*/
private
final
int
s
[]={
7
,
12
,
17
,
22
,
7
,
12
,
17
,
22
,
7
,
12
,
17
,
22
,
7
,
12
,
17
,
22
,
5
,
9
,
14
,
20
,
5
,
9
,
14
,
20
,
5
,
9
,
14
,
20
,
5
,
9
,
14
,
20
,
4
,
11
,
16
,
23
,
4
,
11
,
16
,
23
,
4
,
11
,
16
,
23
,
4
,
11
,
16
,
23
,
6
,
10
,
15
,
21
,
6
,
10
,
15
,
21
,
6
,
10
,
15
,
21
,
6
,
10
,
15
,
21
};
/*
*初始化函数
*/
private
void
init
(){
Atemp
=
A
;
Btemp
=
B
;
Ctemp
=
C
;
Dtemp
=
D
;
}
/*
*移动一定位数
*/
private
int
shift
(
int
a
,
int
s
){
return
(
a
<<
s
)|(
a
>>>(
32
-
s
));
//右移的时候,高位一定要补零,而不是补充符号位
}
/*
*主循环
*/
private
void
MainLoop
(
int
M
[]){
int
F
,
g
;
int
a
=
Atemp
;
int
b
=
Btemp
;
int
c
=
Ctemp
;
int
d
=
Dtemp
;
for
(
int
i
=
0
;
i
<
64
;
i
++){
if
(
i
<
16
){
F
=(
b
&
c
)|((~
b
)&
d
);
g
=
i
;
}
else
if
(
i
<
32
){
F
=(
d
&
b
)|((~
d
)&
c
);
g
=(
5
*
i
+
1
)%
16
;
}
else
if
(
i
<
48
){
F
=
b
^
c
^
d
;
g
=(
3
*
i
+
5
)%
16
;
}
else
{
F
=
c
^(
b
|(~
d
));
g
=(
7
*
i
)%
16
;
}
int
tmp
=
d
;
d
=
c
;
c
=
b
;
b
=
b
+
shift
(
a
+
F
+
K
[
i
]+
M
[
g
],
s
[
i
]);
a
=
tmp
;
}
Atemp
=
a
+
Atemp
;
Btemp
=
b
+
Btemp
;
Ctemp
=
c
+
Ctemp
;
Dtemp
=
d
+
Dtemp
;
}
/*
*填充函数
*处理后应满足bits≡448(mod512),字节就是bytes≡56(mode64)
*填充方式为先加一个0,其它位补零
*最后加上64位的原来长度
*/
private
int
[]
add
(
String
str
){
int
num
=((
str
.
length
()+
8
)/
64
)+
1
;
//以512位,64个字节为一组
int
strByte
[]=
new
int
[
num
*
16
];
//64/4=16,所以有16个整数
for
(
int
i
=
0
;
i
<
num
*
16
;
i
++){
//全部初始化0
strByte
[
i
]=
0
;
}
int
i
;
for
(
i
=
0
;
i
<
str
.
length
();
i
++){
strByte
[
i
>>
2
]|=
str
.
charAt
(
i
)<<((
i
%
4
)*
8
);
//一个整数存储四个字节,小端序
}
strByte
[
i
>>
2
]|=
0x80
<<((
i
%
4
)*
8
);
//尾部添加1
/*
*添加原长度,长度指位的长度,所以要乘8,然后是小端序,所以放在倒数第二个,这里长度只用了32位
*/
strByte
[
num
*
16
-
2
]=
str
.
length
()*
8
;
return
strByte
;
}
/*
*调用函数
*/
public
String
getMD5
(
String
source
){
init
();
int
strByte
[]=
add
(
source
);
for
(
int
i
=
0
;
i
<
strByte
.
length
/
16
;
i
++){
int
num
[]=
new
int
[
16
];
for
(
int
j
=
0
;
j
<
16
;
j
++){
num
[
j
]=
strByte
[
i
*
16
+
j
];
}
MainLoop
(
num
);
}
return
changeHex
(
Atemp
)+
changeHex
(
Btemp
)+
changeHex
(
Ctemp
)+
changeHex
(
Dtemp
);
}
/*
*整数变成16进制字符串
*/
private
String
changeHex
(
int
a
){
String
str
=
""
;
for
(
int
i
=
0
;
i
<
4
;
i
++){
str
+=
String
.
format
(
"%2s"
,
Integer
.
toHexString
(((
a
>>
i
*
8
)%(
1
<<
8
))&
0xff
)).
replace
(
' '
,
'0'
);
}
return
str
;
}
/*
*单例
*/
private
static
MD5
instance
;
public
static
MD5
getInstance
(){
if
(
instance
==
null
){
instance
=
new
MD5
();
}
return
instance
;
}
private
MD5
(){};
public
static
void
main
(
String
[]
args
){
String
str
=
MD5
.
getInstance
().
getMD5
(
"你若安好,便是晴天"
);
System
.
out
.
println
(
str
);
}
}
\ No newline at end of file
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/MD5Demo.java
0 → 100644
浏览文件 @
a17a2800
package
com.snailclimb.ks.securityAlgorithm
;
import
java.security.MessageDigest
;
public
class
MD5Demo
{
// test
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
getMD5Code
(
"你若安好,便是晴天"
));
}
private
MD5Demo
()
{
}
// md5加密
public
static
String
getMD5Code
(
String
message
)
{
String
md5Str
=
""
;
try
{
//创建MD5算法消息摘要
MessageDigest
md
=
MessageDigest
.
getInstance
(
"MD5"
);
//生成的哈希值的字节数组
byte
[]
md5Bytes
=
md
.
digest
(
message
.
getBytes
());
md5Str
=
bytes2Hex
(
md5Bytes
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
md5Str
;
}
// 2进制转16进制
public
static
String
bytes2Hex
(
byte
[]
bytes
)
{
StringBuffer
result
=
new
StringBuffer
();
int
temp
;
try
{
for
(
int
i
=
0
;
i
<
bytes
.
length
;
i
++)
{
temp
=
bytes
[
i
];
if
(
temp
<
0
)
{
temp
+=
256
;
}
if
(
temp
<
16
)
{
result
.
append
(
"0"
);
}
result
.
append
(
Integer
.
toHexString
(
temp
));
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
result
.
toString
();
}
}
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/RSADemo.java
0 → 100644
浏览文件 @
a17a2800
package
com.snailclimb.ks.securityAlgorithm
;
import
org.apache.commons.codec.binary.Base64
;
import
java.security.*
;
import
java.security.spec.PKCS8EncodedKeySpec
;
import
java.security.spec.X509EncodedKeySpec
;
import
java.util.HashMap
;
import
java.util.Map
;
import
javax.crypto.Cipher
;
/**
* Created by humf.需要依赖 commons-codec 包
*/
public
class
RSADemo
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Map
<
String
,
Key
>
keyMap
=
initKey
();
String
publicKey
=
getPublicKey
(
keyMap
);
String
privateKey
=
getPrivateKey
(
keyMap
);
System
.
out
.
println
(
keyMap
);
System
.
out
.
println
(
"-----------------------------------"
);
System
.
out
.
println
(
publicKey
);
System
.
out
.
println
(
"-----------------------------------"
);
System
.
out
.
println
(
privateKey
);
System
.
out
.
println
(
"-----------------------------------"
);
byte
[]
encryptByPrivateKey
=
encryptByPrivateKey
(
"123456"
.
getBytes
(),
privateKey
);
byte
[]
encryptByPublicKey
=
encryptByPublicKey
(
"123456"
,
publicKey
);
System
.
out
.
println
(
new
String
(
encryptByPrivateKey
));
System
.
out
.
println
(
"-----------------------------------"
);
System
.
out
.
println
(
new
String
(
encryptByPublicKey
));
System
.
out
.
println
(
"-----------------------------------"
);
String
sign
=
sign
(
encryptByPrivateKey
,
privateKey
);
System
.
out
.
println
(
sign
);
System
.
out
.
println
(
"-----------------------------------"
);
boolean
verify
=
verify
(
encryptByPrivateKey
,
publicKey
,
sign
);
System
.
out
.
println
(
verify
);
System
.
out
.
println
(
"-----------------------------------"
);
byte
[]
decryptByPublicKey
=
decryptByPublicKey
(
encryptByPrivateKey
,
publicKey
);
byte
[]
decryptByPrivateKey
=
decryptByPrivateKey
(
encryptByPublicKey
,
privateKey
);
System
.
out
.
println
(
new
String
(
decryptByPublicKey
));
System
.
out
.
println
(
"-----------------------------------"
);
System
.
out
.
println
(
new
String
(
decryptByPrivateKey
));
}
public
static
final
String
KEY_ALGORITHM
=
"RSA"
;
public
static
final
String
SIGNATURE_ALGORITHM
=
"MD5withRSA"
;
private
static
final
String
PUBLIC_KEY
=
"RSAPublicKey"
;
private
static
final
String
PRIVATE_KEY
=
"RSAPrivateKey"
;
public
static
byte
[]
decryptBASE64
(
String
key
)
{
return
Base64
.
decodeBase64
(
key
);
}
public
static
String
encryptBASE64
(
byte
[]
bytes
)
{
return
Base64
.
encodeBase64String
(
bytes
);
}
/**
* 用私钥对信息生成数字签名
*
* @param data
* 加密数据
* @param privateKey
* 私钥
* @return
* @throws Exception
*/
public
static
String
sign
(
byte
[]
data
,
String
privateKey
)
throws
Exception
{
// 解密由base64编码的私钥
byte
[]
keyBytes
=
decryptBASE64
(
privateKey
);
// 构造PKCS8EncodedKeySpec对象
PKCS8EncodedKeySpec
pkcs8KeySpec
=
new
PKCS8EncodedKeySpec
(
keyBytes
);
// KEY_ALGORITHM 指定的加密算法
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
KEY_ALGORITHM
);
// 取私钥匙对象
PrivateKey
priKey
=
keyFactory
.
generatePrivate
(
pkcs8KeySpec
);
// 用私钥对信息生成数字签名
Signature
signature
=
Signature
.
getInstance
(
SIGNATURE_ALGORITHM
);
signature
.
initSign
(
priKey
);
signature
.
update
(
data
);
return
encryptBASE64
(
signature
.
sign
());
}
/**
* 校验数字签名
*
* @param data
* 加密数据
* @param publicKey
* 公钥
* @param sign
* 数字签名
* @return 校验成功返回true 失败返回false
* @throws Exception
*/
public
static
boolean
verify
(
byte
[]
data
,
String
publicKey
,
String
sign
)
throws
Exception
{
// 解密由base64编码的公钥
byte
[]
keyBytes
=
decryptBASE64
(
publicKey
);
// 构造X509EncodedKeySpec对象
X509EncodedKeySpec
keySpec
=
new
X509EncodedKeySpec
(
keyBytes
);
// KEY_ALGORITHM 指定的加密算法
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
KEY_ALGORITHM
);
// 取公钥匙对象
PublicKey
pubKey
=
keyFactory
.
generatePublic
(
keySpec
);
Signature
signature
=
Signature
.
getInstance
(
SIGNATURE_ALGORITHM
);
signature
.
initVerify
(
pubKey
);
signature
.
update
(
data
);
// 验证签名是否正常
return
signature
.
verify
(
decryptBASE64
(
sign
));
}
public
static
byte
[]
decryptByPrivateKey
(
byte
[]
data
,
String
key
)
throws
Exception
{
// 对密钥解密
byte
[]
keyBytes
=
decryptBASE64
(
key
);
// 取得私钥
PKCS8EncodedKeySpec
pkcs8KeySpec
=
new
PKCS8EncodedKeySpec
(
keyBytes
);
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
KEY_ALGORITHM
);
Key
privateKey
=
keyFactory
.
generatePrivate
(
pkcs8KeySpec
);
// 对数据解密
Cipher
cipher
=
Cipher
.
getInstance
(
keyFactory
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
privateKey
);
return
cipher
.
doFinal
(
data
);
}
/**
* 解密<br>
* 用私钥解密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public
static
byte
[]
decryptByPrivateKey
(
String
data
,
String
key
)
throws
Exception
{
return
decryptByPrivateKey
(
decryptBASE64
(
data
),
key
);
}
/**
* 解密<br>
* 用公钥解密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public
static
byte
[]
decryptByPublicKey
(
byte
[]
data
,
String
key
)
throws
Exception
{
// 对密钥解密
byte
[]
keyBytes
=
decryptBASE64
(
key
);
// 取得公钥
X509EncodedKeySpec
x509KeySpec
=
new
X509EncodedKeySpec
(
keyBytes
);
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
KEY_ALGORITHM
);
Key
publicKey
=
keyFactory
.
generatePublic
(
x509KeySpec
);
// 对数据解密
Cipher
cipher
=
Cipher
.
getInstance
(
keyFactory
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
publicKey
);
return
cipher
.
doFinal
(
data
);
}
/**
* 加密<br>
* 用公钥加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public
static
byte
[]
encryptByPublicKey
(
String
data
,
String
key
)
throws
Exception
{
// 对公钥解密
byte
[]
keyBytes
=
decryptBASE64
(
key
);
// 取得公钥
X509EncodedKeySpec
x509KeySpec
=
new
X509EncodedKeySpec
(
keyBytes
);
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
KEY_ALGORITHM
);
Key
publicKey
=
keyFactory
.
generatePublic
(
x509KeySpec
);
// 对数据加密
Cipher
cipher
=
Cipher
.
getInstance
(
keyFactory
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
publicKey
);
return
cipher
.
doFinal
(
data
.
getBytes
());
}
/**
* 加密<br>
* 用私钥加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public
static
byte
[]
encryptByPrivateKey
(
byte
[]
data
,
String
key
)
throws
Exception
{
// 对密钥解密
byte
[]
keyBytes
=
decryptBASE64
(
key
);
// 取得私钥
PKCS8EncodedKeySpec
pkcs8KeySpec
=
new
PKCS8EncodedKeySpec
(
keyBytes
);
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
KEY_ALGORITHM
);
Key
privateKey
=
keyFactory
.
generatePrivate
(
pkcs8KeySpec
);
// 对数据加密
Cipher
cipher
=
Cipher
.
getInstance
(
keyFactory
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
privateKey
);
return
cipher
.
doFinal
(
data
);
}
/**
* 取得私钥
*
* @param keyMap
* @return
* @throws Exception
*/
public
static
String
getPrivateKey
(
Map
<
String
,
Key
>
keyMap
)
throws
Exception
{
Key
key
=
(
Key
)
keyMap
.
get
(
PRIVATE_KEY
);
return
encryptBASE64
(
key
.
getEncoded
());
}
/**
* 取得公钥
*
* @param keyMap
* @return
* @throws Exception
*/
public
static
String
getPublicKey
(
Map
<
String
,
Key
>
keyMap
)
throws
Exception
{
Key
key
=
keyMap
.
get
(
PUBLIC_KEY
);
return
encryptBASE64
(
key
.
getEncoded
());
}
/**
* 初始化密钥
*
* @return
* @throws Exception
*/
public
static
Map
<
String
,
Key
>
initKey
()
throws
Exception
{
KeyPairGenerator
keyPairGen
=
KeyPairGenerator
.
getInstance
(
KEY_ALGORITHM
);
keyPairGen
.
initialize
(
1024
);
KeyPair
keyPair
=
keyPairGen
.
generateKeyPair
();
Map
<
String
,
Key
>
keyMap
=
new
HashMap
(
2
);
keyMap
.
put
(
PUBLIC_KEY
,
keyPair
.
getPublic
());
// 公钥
keyMap
.
put
(
PRIVATE_KEY
,
keyPair
.
getPrivate
());
// 私钥
return
keyMap
;
}
}
\ No newline at end of file
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/SHA1Demo.java
0 → 100644
浏览文件 @
a17a2800
package
com.snailclimb.ks.securityAlgorithm
;
import
java.io.UnsupportedEncodingException
;
import
java.security.MessageDigest
;
import
java.security.NoSuchAlgorithmException
;
public
class
SHA1Demo
{
public
static
void
main
(
String
[]
args
)
{
// TODO Auto-generated method stub
System
.
out
.
println
(
getSha1
(
"你若安好,便是晴天"
));
}
public
static
String
getSha1
(
String
str
)
{
if
(
null
==
str
||
0
==
str
.
length
())
{
return
null
;
}
char
[]
hexDigits
=
{
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
};
try
{
//创建SHA1算法消息摘要对象
MessageDigest
mdTemp
=
MessageDigest
.
getInstance
(
"SHA1"
);
//使用指定的字节数组更新摘要。
mdTemp
.
update
(
str
.
getBytes
(
"UTF-8"
));
//生成的哈希值的字节数组
byte
[]
md
=
mdTemp
.
digest
();
//SHA1算法生成信息摘要关键过程
int
j
=
md
.
length
;
char
[]
buf
=
new
char
[
j
*
2
];
int
k
=
0
;
for
(
int
i
=
0
;
i
<
j
;
i
++)
{
byte
byte0
=
md
[
i
];
buf
[
k
++]
=
hexDigits
[
byte0
>>>
4
&
0xf
];
buf
[
k
++]
=
hexDigits
[
byte0
&
0xf
];
}
return
new
String
(
buf
);
}
catch
(
NoSuchAlgorithmException
e
)
{
e
.
printStackTrace
();
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
}
return
"0"
;
}
}
数据结构与算法/source code/securityAlgorithm/src/main/java/com/snailclimb/ks/securityAlgorithm/readme
0 → 100644
浏览文件 @
a17a2800
Des算法参考:http://blog.csdn.net/super_cui/article/details/70820983
IDEA算法参考:https://www.xttblog.com/?p=1121
RSA算法实现参考:https://www.cnblogs.com/xlhan/p/7120488.html
\ No newline at end of file
数据结构与算法/source code/securityAlgorithm/src/test/java/com/snailclimb/ks/securityAlgorithm/AppTest.java
0 → 100644
浏览文件 @
a17a2800
package
com.snailclimb.ks.securityAlgorithm
;
import
junit.framework.Test
;
import
junit.framework.TestCase
;
import
junit.framework.TestSuite
;
/**
* Unit test for simple App.
*/
public
class
AppTest
extends
TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public
AppTest
(
String
testName
)
{
super
(
testName
);
}
/**
* @return the suite of tests being tested
*/
public
static
Test
suite
()
{
return
new
TestSuite
(
AppTest
.
class
);
}
/**
* Rigourous Test :-)
*/
public
void
testApp
()
{
assertTrue
(
true
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录