Skip to content

Commit a17a280

Browse files
committed
增加了常见安全算法的源代码
1 parent e81ef41 commit a17a280

File tree

16 files changed

+840
-0
lines changed

16 files changed

+840
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
16+
<attributes>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="output" path="target/classes"/>
26+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>securityAlgorithm</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/test/java=UTF-8
4+
encoding/<project>=UTF-8
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
3+
org.eclipse.jdt.core.compiler.compliance=1.5
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.5
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.snailclimb.ks</groupId>
6+
<artifactId>securityAlgorithm</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>securityAlgorithm</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>3.8.1</version>
22+
<scope>test</scope>
23+
</dependency>
24+
<!-- Base64 -->
25+
<dependency>
26+
<groupId>commons-codec</groupId>
27+
<artifactId>commons-codec</artifactId>
28+
<version>1.8</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.bouncycastle</groupId>
32+
<artifactId>bcprov-jdk15on</artifactId>
33+
<version>1.56</version>
34+
</dependency>
35+
36+
</dependencies>
37+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.snailclimb.ks.securityAlgorithm;
2+
3+
import java.io.UnsupportedEncodingException;
4+
import java.util.Base64;
5+
6+
public class Base64Demo {
7+
8+
public static void main(String[] args) throws UnsupportedEncodingException {
9+
// TODO Auto-generated method stub
10+
CommonsCodecDemo();
11+
bouncyCastleDemo();
12+
jdkDemo();
13+
}
14+
15+
static String str = "你若安好,便是晴天";
16+
17+
/**
18+
* commons codec实现Base64加密解密
19+
*/
20+
public static void CommonsCodecDemo() {
21+
// 加密:
22+
byte[] encodeBytes = org.apache.commons.codec.binary.Base64.encodeBase64(str.getBytes());
23+
System.out.println("commons codec实现base64加密: " + new String(encodeBytes));
24+
// 解密:
25+
byte[] decodeBytes = org.apache.commons.codec.binary.Base64.decodeBase64(encodeBytes);
26+
System.out.println("commons codec实现base64解密: " + new String(decodeBytes));
27+
}
28+
29+
/**
30+
* bouncy castle实现Base64加密解密
31+
*/
32+
public static void bouncyCastleDemo() {
33+
// 加密
34+
byte[] encodeBytes = org.bouncycastle.util.encoders.Base64.encode(str.getBytes());
35+
System.out.println("bouncy castle实现base64加密: " + new String(encodeBytes));
36+
// 解密
37+
byte[] decodeBytes = org.bouncycastle.util.encoders.Base64.decode(encodeBytes);
38+
System.out.println("bouncy castle实现base64解密:" + new String(decodeBytes));
39+
}
40+
41+
public static void jdkDemo() throws UnsupportedEncodingException {
42+
// 加密
43+
String encodeBytes = Base64.getEncoder().encodeToString(str.getBytes("UTF-8"));
44+
System.out.println("JDK实现的base64加密: " + encodeBytes);
45+
//解密
46+
byte[] decodeBytes = Base64.getDecoder().decode(encodeBytes.getBytes("UTF-8"));
47+
System.out.println("JDK实现的base64解密: "+new String(decodeBytes));
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.snailclimb.ks.securityAlgorithm;
2+
3+
import java.io.UnsupportedEncodingException;
4+
import java.security.SecureRandom;
5+
import javax.crypto.spec.DESKeySpec;
6+
import javax.crypto.SecretKeyFactory;
7+
import javax.crypto.SecretKey;
8+
import javax.crypto.Cipher;
9+
10+
/**
11+
* DES加密介绍 DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法。DES加密算法出自IBM的研究,
12+
* 后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少,因为DES使用56位密钥,以现代计算能力,
13+
* 24小时内即可被破解。虽然如此,在某些简单应用中,我们还是可以使用DES加密算法,本文简单讲解DES的JAVA实现 。
14+
* 注意:DES加密和解密过程中,密钥长度都必须是8的倍数
15+
*/
16+
public class DesDemo {
17+
public DesDemo() {
18+
}
19+
20+
// 测试
21+
public static void main(String args[]) {
22+
// 待加密内容
23+
String str = "cryptology";
24+
// 密码,长度要是8的倍数
25+
String password = "95880288";
26+
27+
byte[] result;
28+
try {
29+
result = DesDemo.encrypt(str.getBytes(), password);
30+
System.out.println("加密后:" + new String(result));
31+
byte[] decryResult = DesDemo.decrypt(result, password);
32+
System.out.println("解密后:" + new String(decryResult));
33+
} catch (UnsupportedEncodingException e2) {
34+
// TODO Auto-generated catch block
35+
e2.printStackTrace();
36+
} catch (Exception e1) {
37+
e1.printStackTrace();
38+
}
39+
}
40+
41+
// 直接将如上内容解密
42+
43+
/**
44+
* 加密
45+
*
46+
* @param datasource
47+
* byte[]
48+
* @param password
49+
* String
50+
* @return byte[]
51+
*/
52+
public static byte[] encrypt(byte[] datasource, String password) {
53+
try {
54+
SecureRandom random = new SecureRandom();
55+
DESKeySpec desKey = new DESKeySpec(password.getBytes());
56+
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
57+
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
58+
SecretKey securekey = keyFactory.generateSecret(desKey);
59+
// Cipher对象实际完成加密操作
60+
Cipher cipher = Cipher.getInstance("DES");
61+
// 用密匙初始化Cipher对象,ENCRYPT_MODE用于将 Cipher 初始化为加密模式的常量
62+
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
63+
// 现在,获取数据并加密
64+
// 正式执行加密操作
65+
return cipher.doFinal(datasource); // 按单部分操作加密或解密数据,或者结束一个多部分操作
66+
} catch (Throwable e) {
67+
e.printStackTrace();
68+
}
69+
return null;
70+
}
71+
72+
/**
73+
* 解密
74+
*
75+
* @param src
76+
* byte[]
77+
* @param password
78+
* String
79+
* @return byte[]
80+
* @throws Exception
81+
*/
82+
public static byte[] decrypt(byte[] src, String password) throws Exception {
83+
// DES算法要求有一个可信任的随机数源
84+
SecureRandom random = new SecureRandom();
85+
// 创建一个DESKeySpec对象
86+
DESKeySpec desKey = new DESKeySpec(password.getBytes());
87+
// 创建一个密匙工厂
88+
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 返回实现指定转换的
89+
// Cipher
90+
// 对象
91+
// 将DESKeySpec对象转换成SecretKey对象
92+
SecretKey securekey = keyFactory.generateSecret(desKey);
93+
// Cipher对象实际完成解密操作
94+
Cipher cipher = Cipher.getInstance("DES");
95+
// 用密匙初始化Cipher对象
96+
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
97+
// 真正开始解密操作
98+
return cipher.doFinal(src);
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.snailclimb.ks.securityAlgorithm;
2+
3+
import java.security.Key;
4+
import java.security.Security;
5+
6+
import javax.crypto.Cipher;
7+
import javax.crypto.KeyGenerator;
8+
import javax.crypto.SecretKey;
9+
import javax.crypto.spec.SecretKeySpec;
10+
11+
import org.apache.commons.codec.binary.Base64;
12+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
13+
14+
public class IDEADemo {
15+
public static void main(String args[]) {
16+
bcIDEA();
17+
}
18+
public static void bcIDEA() {
19+
String src = "www.xttblog.com security idea";
20+
try {
21+
Security.addProvider(new BouncyCastleProvider());
22+
23+
//生成key
24+
KeyGenerator keyGenerator = KeyGenerator.getInstance("IDEA");
25+
keyGenerator.init(128);
26+
SecretKey secretKey = keyGenerator.generateKey();
27+
byte[] keyBytes = secretKey.getEncoded();
28+
29+
//转换密钥
30+
Key key = new SecretKeySpec(keyBytes, "IDEA");
31+
32+
//加密
33+
Cipher cipher = Cipher.getInstance("IDEA/ECB/ISO10126Padding");
34+
cipher.init(Cipher.ENCRYPT_MODE, key);
35+
byte[] result = cipher.doFinal(src.getBytes());
36+
System.out.println("bc idea encrypt : " + Base64.encodeBase64String(result));
37+
38+
//解密
39+
cipher.init(Cipher.DECRYPT_MODE, key);
40+
result = cipher.doFinal(result);
41+
System.out.println("bc idea decrypt : " + new String(result));
42+
} catch (Exception e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)