日期:2014-05-16  浏览次数:20551 次

如何实现数据库连接的密码加密

   项目需求

所有认证数据,例如密码,不论是在储存、传输中都必须妥善保护,以防泄露或被未获授权修改。在安全认证中的Fortify 静态代码分析器的扫描中,如果密码明文放在文件中是肯定过不去的。

 

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning /> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL /> <w:BalanceSingleByteDoubleByteWidth /> <w:DoNotLeaveBackslashAlone /> <w:ULTrailSpace /> <w:DoNotExpandShiftReturn /> <w:AdjustLineHeightInTable /> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> <w:UseFELayout /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

<!-- [if !supportLists]-->需求解决方案

下面具体结合SSH的框架的代码实现。 <!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

<!-- [if !supportLists]-->1. <!-- [endif]-->认证数据加密

所有认证数据通过 3DES 加密;加解密方法如下:

byte src_byte[] = password.getBytes();

byte key_byte[] = "123456781234567812345678".getBytes();// 3DES 24 bytes key

 

  try {

    // 生成DES密钥

    javax.crypto.SecretKey deskey;

deskey = genDESKey(key_byte);

        System.out.println("Generator DES KEY OK");

 

          // DES加解密

         byte[] encrypt, decrypt;

        //加密

        encrypt = desEncrypt(deskey, src_byte);

        System.out.println("encrypt=" + new String(encrypt));

       //解密

       decrypt = desDecrypt(deskey, encrypt);

   System.out.println("decrypt=" + new String(decrypt));

}

     catch (Exception ex) {

      ex.printStackTrace();

  }