spring jdbc配置文件进行加密解密.docx

上传人:b****9 文档编号:26003237 上传时间:2023-06-17 格式:DOCX 页数:24 大小:17.92KB
下载 相关 举报
spring jdbc配置文件进行加密解密.docx_第1页
第1页 / 共24页
spring jdbc配置文件进行加密解密.docx_第2页
第2页 / 共24页
spring jdbc配置文件进行加密解密.docx_第3页
第3页 / 共24页
spring jdbc配置文件进行加密解密.docx_第4页
第4页 / 共24页
spring jdbc配置文件进行加密解密.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

spring jdbc配置文件进行加密解密.docx

《spring jdbc配置文件进行加密解密.docx》由会员分享,可在线阅读,更多相关《spring jdbc配置文件进行加密解密.docx(24页珍藏版)》请在冰豆网上搜索。

spring jdbc配置文件进行加密解密.docx

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

*

    021

    *

  • Title:

    [DESEncryptUtil]

  • 022

    *

  • Description:

    [加密码解密类]

  • 023

    *

  • Copyright2009RoadWayCo.,Ltd.
  • 024

    *

  • Allrightreserved.
  • 025

    *

  • Createdby[Huyvanpull][Jul19,2010]
  • 026

    *

  • Midifiedby[修改人][修改时间]
  • 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

*

    052

    *

  • Description:

    [创建一个密钥]

  • 053

    *

  • Createdby[Huyvanpull][Jul19,2010]
  • 054

    *

  • Midifiedby[修改人][修改时间]
  • 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

*

    071

    *

  • Description:

    [根据流得到密钥]

  • 072

    *

  • Createdby[Huyvanpull][Jul19,2010]
  • 073

    *

  • Midifiedby[修改人][修改时间]
  • 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

*

    095

    *

  • Description:

    [对数据进行加密]

  • 096

    *

  • Createdby[Huyvanpull][Jul19,2010]
  • 097

    *

  • Midifiedby[修改人][修改时间]
  • 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

*

    122

    *

  • Description:

    [对数据进行解密]

  • 123

    *

  • Createdby[Huyvanpull][Jul19,2010]
  • 124

    *

  • Midifiedby[修改人][修改时间]
  • 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

classpath*:

spring_config/jdbc_official.databaseinfo

6

7

8

spring_config/key.key"/>

9

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 其它模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1