spring jdbc配置文件进行加密解密.docx
- 文档编号:13369798
- 上传时间:2023-06-13
- 格式:DOCX
- 页数:24
- 大小:17.92KB
spring jdbc配置文件进行加密解密.docx
《spring jdbc配置文件进行加密解密.docx》由会员分享,可在线阅读,更多相关《spring jdbc配置文件进行加密解密.docx(24页珍藏版)》请在冰点文库上搜索。
springjdbc配置文件进行加密解密
001
packagemons.util;
002
003
importjava.io.ByteArrayInputStream;
004
importjava.io.ByteArrayOutputStream;
005
importjava.io.File;
006
importjava.io.FileInputStream;
007
importjava.io.FileOutputStream;
008
importjava.io.InputStream;
009
importjava.io.ObjectInputStream;
010
importjava.io.ObjectOutputStream;
011
importjava.security.Key;
012
importjava.security.NoSuchAlgorithmException;
013
importjava.security.SecureRandom;
014
importjava.security.Security;
015
016
importjavax.crypto.Cipher;
017
importjavax.crypto.KeyGenerator;
018
019
/**
020
*
- Title:
[DESEncryptUtil]
- Description:
[加密码解密类]
- Copyright2009RoadWayCo.,Ltd.
- Allrightreserved.
- Createdby[Huyvanpull][Jul19,2010]
- Midifiedby[修改人][修改时间]
021
*
022
*
023
*
024
*
025
*
026
*
027
*
028
*
029
*@version1.0
030
*/
031
publicclassDESEncryptUtil
032
{
033
publicstaticvoidmain(String[]args)throwsException
034
{
035
/**生成KEY*/
036
StringoperatorType="key";
037
StringkeyFilePath="D:
/key.k";
038
DESEncryptUtil.test(keyFilePath,null,operatorType);
039
040
/**加密*/
041
operatorType="encrypt";
042
StringsourceFilePath="D:
/jdbc_official.properties";
043
DESEncryptUtil.test(keyFilePath,sourceFilePath,operatorType);
044
045
/**解密*/
046
operatorType="decrypt";
047
sourceFilePath="D:
/en_jdbc_official.properties";
048
DESEncryptUtil.test(keyFilePath,sourceFilePath,operatorType);
049
}
050
/**
051
*
- Description:
[创建一个密钥]
- Createdby[Huyvanpull][Jul19,2010]
- Midifiedby[修改人][修改时间]
052
*
053
*
054
*
055
*
056
*
057
*@return
058
*@throwsNoSuchAlgorithmException
059
*/
060
publicstaticKeycreateKey()throwsNoSuchAlgorithmException
061
{
062
Security.insertProviderAt(newcom.sun.crypto.provider.SunJCE(),1);
063
KeyGeneratorgenerator=KeyGenerator.getInstance("DES");
064
generator.init(newSecureRandom());
065
Keykey=generator.generateKey();
066
returnkey;
067
}
068
069
/**
070
*
- Description:
[根据流得到密钥]
- Createdby[Huyvanpull][Jul19,2010]
- Midifiedby[修改人][修改时间]
071
*
072
*
073
*
074
*
075
*
076
*@paramis
077
*@return
078
*/
079
publicstaticKeygetKey(InputStreamis)
080
{
081
try
082
{
083
ObjectInputStreamois=newObjectInputStream(is);
084
return(Key)ois.readObject();
085
}
086
catch(Exceptione)
087
{
088
e.printStackTrace();
089
thrownewRuntimeException(e);
090
}
091
}
092
093
/**
094
*
- Description:
[对数据进行加密]
- Createdby[Huyvanpull][Jul19,2010]
- Midifiedby[修改人][修改时间]
095
*
096
*
097
*
098
*
099
*
100
*@paramkey
101
*@paramdata
102
*@return
103
*/
104
privatestaticbyte[]doEncrypt(Keykey,byte[]data)
105
{
106
try
107
{
108
Ciphercipher=Cipher.getInstance("DES/ECB/PKCS5Padding");
109
cipher.init(Cipher.ENCRYPT_MODE,key);
110
byte[]raw=cipher.doFinal(data);
111
returnraw;
112
}
113
catch(Exceptione)
114
{
115
e.printStackTrace();
116
thrownewRuntimeException(e);
117
}
118
}
119
120
/**
121
*
- Description:
[对数据进行解密]
- Createdby[Huyvanpull][Jul19,2010]
- Midifiedby[修改人][修改时间]
122
*
123
*
124
*
125
*
126
*
127
*@paramkey
128
*@paramin
129
*@return
130
*/
131
publicstaticInputStreamdoDecrypt(Keykey,InputStreamin)
132
{
133
try
134
{
135
Ciphercipher=Cipher.getInstance("DES/ECB/PKCS5Padding");
136
cipher.init(Cipher.DECRYPT_MODE,key);
137
ByteArrayOutputStreambout=newByteArrayOutputStream();
138
byte[]tmpbuf=newbyte[1024];
139
intcount=0;
140
while((count=in.read(tmpbuf))!
=-1)
141
{
142
bout.write(tmpbuf,0,count);
143
tmpbuf=newbyte[1024];
144
}
145
in.close();
146
byte[]orgData=bout.toByteArray();
147
byte[]raw=cipher.doFinal(orgData);
148
ByteArrayInputStreambin=newByteArrayInputStream(raw);
149
returnbin;
150
}
151
catch(Exceptione)
152
{
153
e.printStackTrace();
154
thrownewRuntimeException(e);
155
}
156
}
157
158
privatestaticvoidtest(StringkeyFilePath,StringsourceFilePath,
159
StringoperatorType)throwsException
160
{
161
//提供了Java命令使用该工具的功能
162
if(operatorType.equalsIgnoreCase("key"))
163
{
164
//生成密钥文件
165
Keykey=DESEncryptUtil.createKey();
166
ObjectOutputStreamoos=newObjectOutputStream(newFileOutputStream(keyFilePath));
167
oos.writeObject(key);
168
oos.close();
169
System.out.println("成功生成密钥文件"+keyFilePath);
170
}
171
elseif(operatorType.equalsIgnoreCase("encrypt"))
172
{
173
//对文件进行加密
174
Filefile=newFile(sourceFilePath);
175
FileInputStreamin=newFileInputStream(file);
176
ByteArrayOutputStreambout=newByteArrayOutputStream();
177
byte[]tmpbuf=newbyte[1024];
178
intcount=0;
179
while((count=in.read(tmpbuf))!
=-1)
180
{
181
bout.write(tmpbuf,0,count);
182
tmpbuf=newbyte[1024];
183
}
184
in.close();
185
byte[]orgData=bout.toByteArray();
186
Keykey=getKey(newFileInputStream(keyFilePath));
187
byte[]raw=DESEncryptUtil.doEncrypt(key,orgData);
188
file=newFile(file.getParent()+"\\en_"+file.getName());
189
FileOutputStreamout=newFileOutputStream(file);
190
out.write(raw);
191
out.close();
192
System.out.println("成功加密,加密文件位于:
"+file.getAbsolutePath());
193
}
194
elseif(operatorType.equalsIgnoreCase("decrypt"))
195
{
196
//对文件进行解密
197
Filefile=newFile(sourceFilePath);
198
FileInputStreamfis=newFileInputStream(file);
199
200
Keykey=getKey(newFileInputStream(keyFilePath));
201
InputStreamraw=DESEncryptUtil.doDecrypt(key,fis);
202
ByteArrayOutputStreambout=newByteArrayOutputStream();
203
byte[]tmpbuf=newbyte[1024];
204
intcount=0;
205
while((count=raw.read(tmpbuf))!
=-1)
206
{
207
bout.write(tmpbuf,0,count);
208
tmpbuf=newbyte[1024];
209
}
210
raw.close();
211
byte[]orgData=bout.toByteArray();
212
file=newFile(file.getParent()+"\\rs_"+file.getName());
213
FileOutputStreamfos=newFileOutputStream(file);
214
fos.write(orgData);
215
System.out.println("成功解密,解密文件位于:
"+file.getAbsolutePath());
216
}
217
}
218
}
DecryptPropertyPlaceholderConfigurer.java
01
packagecom.framework.spring;
02
03
importjava.io.IOException;
04
importjava.io.InputStream;
05
importjava.io.InputStreamReader;
06
importjava.security.Key;
07
importjava.util.Properties;
08
09
importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
10
importorg.springframework.core.io.Resource;
11
importorg.springframework.util.DefaultPropertiesPersister;
12
importorg.springframework.util.PropertiesPersister;
13
14
importmons.util.DESEncryptUtil;
15
16
publicclassDecryptPropertyPlaceholderConfigurerextends
17
PropertyPlaceholderConfigurer
18
{
19
privateResource[]locations;
20
21
privateResourcekeyLocation;
22
23
privateStringfileEncoding;
24
25
publicvoidsetKeyLocation(ResourcekeyLocation)
26
{
27
this.keyLocation=keyLocation;
28
}
29
30
publicvoidsetLocations(Resource[]locations)
31
{
32
this.locations=locations;
33
}
34
35
publicvoidloadProperties(Propertiesprops)throwsIOException
36
{
37
if(this.locations!
=null)
38
{
39
PropertiesPersisterpropertiesPersister=newDefaultPropertiesPersister();
40
for(inti=0;i 41 { 42 Resourcelocation=this.locations[i]; 43 if(logger.isInfoEnabled()) 44 { 45 logger.info("Loadingpropertiesfilefrom"+location); 46 } 47 InputStreamis=null; 48 try 49 { 50 is=location.getInputStream(); 51 Keykey=DESEncryptUtil.getKey(keyLocation.getInputStream()); 52 is=DESEncryptUtil.doDecrypt(key,is); 53 if(fileEncoding! =null) 54 { 55 propertiesPersister.load(props,newInputStreamReader( 56 is,fileEncoding)); 57 } 58 else 59 { 60 propertiesPersister.load(props,is); 61 } 62 } 63 finally 64 { 65 if(is! =null) 66 { 67 is.close(); 68 } 69 } 70 } 71 } 72 } 73 } 配置文件: viewsourceprint? 1 --加密码属性文件--> 2 3 class="com.framework.spring.DecryptPropertyPlaceholderConfigurer"> 4 5 spring_config/jdbc_official.databaseinfo 6 7 8 spring_config/key.key"/> 9
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- spring jdbc配置文件进行加密解密 jdbc 配置文件 进行 加密 解密