- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Java中常用加密/解密方法详解由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
安全问题已经成为一个越来越重要的问题,在Java中如何对重要数据进行加密解密是本文的主要内容.
1、常用的加密/解密算法 。
1.Base64 。
严格来说Base64并不是一种加密/解密算法,而是一种编码方式。Base64不生成密钥,通过Base64编码后的密文就可以直接“翻译”为明文,但是可以通过向明文中添加混淆字符来达到加密的效果.
2.DES 。
DES是一种基于56位密钥的对称算法,1976年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),随后在国际上广泛流传开来。现在DES已经不是一种安全的加密算法,已被公开破解,现在DES已经被高级加密标准(AES)所代替.
3.3DES 。
3DES是DES的一种派生算法,主要提升了DES的一些实用所需的安全性.
4.AES 。
AES是现在对称加密算法中最流行的算法之一.
2、实现所需的一些库 。
为了实现上述的算法,我们可以实用JDK自带的实现,也可以使用一些开源的第三方库,例如Bouncy Castle(https://www.bouncycastle.org/)和comnons codec(https://commons.apache.org/proper/commons-codec/).
3、具体实现 。
1.Base64 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package
com.tancky.security;
import
java.io.IOException;
import
sun.misc.BASE64Decoder;
import
sun.misc.BASE64Encoder;
public
class
Base64Demo {
private
static
String src =
"TestBase64"
;
public
static
void
main(String[] args) {
Base64Demo.jdkBase64();
Base64Demo.commonsCodecBase64 ();
Base64Demo.bouncyCastleBase64 ();
}
//使用JDK的base64实现,
public
static
void
jdkBase64 (){
BASE64Encoder encoder =
new
BASE64Encoder();
String encode = encoder.encode(Base64Demo.src.getBytes());
System.out.println(
"encode: "
+ encode);
BASE64Decoder decoder =
new
BASE64Decoder();
try
{
String decode =
new
String ( decoder.decodeBuffer(encode));
System.out.println(
"decode: "
+ decode);
}
catch
(IOException e) {
e.printStackTrace();
}
}
//使用apache的commonsCodec实现
public
static
void
commonsCodecBase64 (){
byte
[] encodeBytes = org.apache.commons.codec.binary.Base64.encodeBase64(Base64Demo.src.getBytes());
String encode =
new
String (encodeBytes);
System.out.println(
"encode: "
+ encode);
byte
[] decodeBytes = org.apache.commons.codec.binary.Base64.decodeBase64(encode);
String decode =
new
String(decodeBytes);
System.out.println(
"decode: "
+ decode);
}
//使用bouncyCastlede实现
public
static
void
bouncyCastleBase64 () {
byte
[] encodeBytes = org.bouncycastle.util.encoders.Base64.encode(Base64Demo.src.getBytes()) ;
String encode =
new
String (encodeBytes);
System.out.println(
"encode: "
+ encode);
byte
[] decodeBytes = org.bouncycastle.util.encoders.Base64.decode(encode);
String decode =
new
String(decodeBytes);
System.out.println(
"decode: "
+ decode);
}
}
|
2.DES 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
package
com.tancky.security;
import
java.security.InvalidKeyException;
import
java.security.Key;
import
java.security.NoSuchAlgorithmException;
import
java.security.NoSuchProviderException;
import
java.security.Security;
import
java.security.spec.InvalidKeySpecException;
import
javax.crypto.BadPaddingException;
import
javax.crypto.Cipher;
import
javax.crypto.IllegalBlockSizeException;
import
javax.crypto.KeyGenerator;
import
javax.crypto.NoSuchPaddingException;
import
javax.crypto.SecretKey;
import
javax.crypto.SecretKeyFactory;
import
javax.crypto.spec.DESKeySpec;
import
org.bouncycastle.jce.provider.BouncyCastleProvider;
import
org.bouncycastle.util.encoders.Hex;
public
class
DESDemo {
private
static
String src =
"TestDES"
;
public
static
void
jdkDES () {
try
{
//生成密钥Key
KeyGenerator keyGenerator = KeyGenerator.getInstance(
"DES"
);
keyGenerator.init(
56
);
SecretKey secretKey = keyGenerator.generateKey();
byte
[] bytesKey = secretKey.getEncoded();
//KEY转换
DESKeySpec deSedeKeySpec =
new
DESKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance(
"DES"
);
Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
//加密
Cipher cipher = Cipher.getInstance(
"DES/ECB/PKCS5Padding"
);
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte
[] encodeResult = cipher.doFinal(DESDemo.src.getBytes());
System.out.println(
"DESEncode :"
+ Hex.toHexString(encodeResult));
//解密
cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);
byte
[] DecodeResult = cipher.doFinal(encodeResult);
System.out.println(
"DESDncode :"
+
new
String (DecodeResult));
}
catch
(NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch
(InvalidKeyException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(InvalidKeySpecException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(NoSuchPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(IllegalBlockSizeException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(BadPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public
static
void
bcDES (){
try
{
//使用BouncyCastle 的DES加密
Security.addProvider(
new
BouncyCastleProvider());
//生成密钥Key
KeyGenerator keyGenerator = KeyGenerator.getInstance(
"DES"
,
"BC"
);
keyGenerator.init(
56
);
SecretKey secretKey = keyGenerator.generateKey();
byte
[] bytesKey = secretKey.getEncoded();
//KEY转换
DESKeySpec deSedeKeySpec =
new
DESKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance(
"DES"
);
Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
//加密
Cipher cipher = Cipher.getInstance(
"DES/ECB/PKCS5Padding"
);
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte
[] encodeResult = cipher.doFinal(DESDemo.src.getBytes());
System.out.println(
"DESEncode :"
+ Hex.toHexString(encodeResult));
//解密
cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);
byte
[] DecodeResult = cipher.doFinal(encodeResult);
System.out.println(
"DESDncode :"
+
new
String (DecodeResult));
}
catch
(NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch
(InvalidKeyException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(InvalidKeySpecException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(NoSuchPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(IllegalBlockSizeException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(BadPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(NoSuchProviderException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public
static
void
main(String[] args) {
DESDemo.jdkDES ();
DESDemo.bcDES();
}
}
|
3.3DES 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
package
com.tancky.security;
import
java.security.InvalidKeyException;
import
java.security.Key;
import
java.security.NoSuchAlgorithmException;
import
java.security.NoSuchProviderException;
import
java.security.Security;
import
java.security.spec.InvalidKeySpecException;
import
javax.crypto.BadPaddingException;
import
javax.crypto.Cipher;
import
javax.crypto.IllegalBlockSizeException;
import
javax.crypto.KeyGenerator;
import
javax.crypto.NoSuchPaddingException;
import
javax.crypto.SecretKey;
import
javax.crypto.SecretKeyFactory;
import
javax.crypto.spec.DESedeKeySpec;
import
org.bouncycastle.jce.provider.BouncyCastleProvider;
import
org.bouncycastle.util.encoders.Hex;
public
class
TripleDESDemo {
private
static
String src =
"TestTripleDES"
;
public
static
void
jdkTripleDES () {
try
{
//生成密钥Key
KeyGenerator keyGenerator = KeyGenerator.getInstance(
"DESede"
);
keyGenerator.init(
168
);
SecretKey secretKey = keyGenerator.generateKey();
byte
[] bytesKey = secretKey.getEncoded();
//KEY转换
DESedeKeySpec deSedeKeySpec =
new
DESedeKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance(
"DESede"
);
Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
//加密
Cipher cipher = Cipher.getInstance(
"DESede/ECB/PKCS5Padding"
);
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte
[] encodeResult = cipher.doFinal(TripleDESDemo.src.getBytes());
System.out.println(
"TripleDESEncode :"
+ Hex.toHexString(encodeResult));
//解密
cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);
byte
[] DecodeResult = cipher.doFinal(encodeResult);
System.out.println(
"TripleDESDncode :"
+
new
String (DecodeResult));
}
catch
(NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch
(InvalidKeyException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(InvalidKeySpecException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(NoSuchPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(IllegalBlockSizeException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(BadPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public
static
void
bcTripleDES () {
try
{
Security.addProvider(
new
BouncyCastleProvider());
//生成密钥Key
KeyGenerator keyGenerator = KeyGenerator.getInstance(
"DESede"
,
"BC"
);
keyGenerator.getProvider();
keyGenerator.init(
168
);
SecretKey secretKey = keyGenerator.generateKey();
byte
[] bytesKey = secretKey.getEncoded();
//KEY转换
DESedeKeySpec deSedeKeySpec =
new
DESedeKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance(
"DESede"
);
Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
//加密
Cipher cipher = Cipher.getInstance(
"DESede/ECB/PKCS5Padding"
);
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte
[] encodeResult = cipher.doFinal(TripleDESDemo.src.getBytes());
System.out.println(
"TripleDESEncode :"
+ Hex.toHexString(encodeResult));
//解密
cipher.init(Cipher.DECRYPT_MODE,convertSecretKey);
byte
[] DecodeResult = cipher.doFinal(encodeResult);
System.out.println(
"TripleDESDncode :"
+
new
String (DecodeResult));
}
catch
(NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch
(InvalidKeyException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(InvalidKeySpecException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(NoSuchPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(IllegalBlockSizeException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(BadPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(NoSuchProviderException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public
static
void
main(String[] args) {
jdkTripleDES ();
bcTripleDES ();
}
}
|
4.AES 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
package
com.tancky.security;
import
java.security.InvalidKeyException;
import
java.security.Key;
import
java.security.NoSuchAlgorithmException;
import
java.security.NoSuchProviderException;
import
java.security.SecureRandom;
import
java.security.Security;
import
javax.crypto.BadPaddingException;
import
javax.crypto.Cipher;
import
javax.crypto.IllegalBlockSizeException;
import
javax.crypto.KeyGenerator;
import
javax.crypto.NoSuchPaddingException;
import
javax.crypto.SecretKey;
import
javax.crypto.spec.SecretKeySpec;
import
org.bouncycastle.jce.provider.BouncyCastleProvider;
import
org.bouncycastle.util.encoders.Hex;
public
class
AESDemo {
private
static
String src =
"TestAES"
;
public
static
void
jdkAES (){
try
{
//生成Key
KeyGenerator keyGenerator = KeyGenerator.getInstance(
"AES"
);
keyGenerator.init(
128
);
//keyGenerator.init(128, new SecureRandom("seedseedseed".getBytes()));
//使用上面这种初始化方法可以特定种子来生成密钥,这样加密后的密文是唯一固定的。
SecretKey secretKey = keyGenerator.generateKey();
byte
[] keyBytes = secretKey.getEncoded();
//Key转换
Key key =
new
SecretKeySpec(keyBytes,
"AES"
);
//加密
Cipher cipher = Cipher.getInstance(
"AES/ECB/PKCS5Padding"
);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte
[] encodeResult = cipher.doFinal(AESDemo.src.getBytes());
System.out.println(
"AESencode : "
+ Hex.toHexString(encodeResult) );
//解密
cipher.init(Cipher.DECRYPT_MODE, key);
byte
[] decodeResult = cipher.doFinal(encodeResult);
System.out.println(
"AESdecode : "
+
new
String (decodeResult));
}
catch
(NoSuchAlgorithmException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(NoSuchPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(InvalidKeyException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(IllegalBlockSizeException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(BadPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public
static
void
bcAES (){
try
{
//使用BouncyCastle 的DES加密
Security.addProvider(
new
BouncyCastleProvider());
//生成Key
KeyGenerator keyGenerator = KeyGenerator.getInstance(
"AES"
,
"BC"
);
keyGenerator.getProvider();
keyGenerator.init(
128
);
SecretKey secretKey = keyGenerator.generateKey();
byte
[] keyBytes = secretKey.getEncoded();
//Key转换
Key key =
new
SecretKeySpec(keyBytes,
"AES"
);
//加密
Cipher cipher = Cipher.getInstance(
"AES/ECB/PKCS5Padding"
);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte
[] encodeResult = cipher.doFinal(AESDemo.src.getBytes());
System.out.println(
"AESencode : "
+ Hex.toHexString(encodeResult) );
//解密
cipher.init(Cipher.DECRYPT_MODE, key);
byte
[] decodeResult = cipher.doFinal(encodeResult);
System.out.println(
"AESdecode : "
+
new
String (decodeResult));
}
catch
(NoSuchAlgorithmException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(NoSuchPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(InvalidKeyException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(IllegalBlockSizeException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(BadPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
catch
(NoSuchProviderException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public
static
void
main(String[] args) {
jdkAES();
bcAES();
}
}
|
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持我! 。
原文链接:http://www.cnblogs.com/tancky/p/6409823.html 。
最后此篇关于Java中常用加密/解密方法详解的文章就讲到这里了,如果你想了解更多关于Java中常用加密/解密方法详解的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!