看好你的数据库连接字符串!

一般情况下,大多数人习惯于将数据库连接写在web.config上里面,理论上讲,将明文存放在该文件里面是安全的,因为web.config文件是不允许被客户端下载,但一旦该文件泄漏出去,哪怕是很短的时间,数据库都将承受巨大的危害,可能花上N年才充实起来的信息在很短时间里毁于一旦。这是任何程序绝对不应该出现的问题。有人用简单的对称加密来将数据库连接字符串的密文存放,但密钥一旦丢失,加密与否,形同虚设,那么如何保证连接字符串的安全性呢。下面这个类就完成这个功能,该类调用系统API,在不同的系统中对相同的连接串会生成不同的密文,即使非法获得该串,不能获得在服务器上的管理员权限,仍然没有能力知道数据库的真正所在。有人说,那服务器管理员权限也被盗用了呢?那盗用者还需要经过一系列复杂的跟踪和总结,来获得系统标识变量。这无疑又是一个难度,等到他真正破解了解该系统的时候,也许你早就在此之前,改正了服务器的配置和密码,还害得人家白忙活了一趟。够阴的!
呵呵
代码如下:
  1 None.gif using  System;
  2 None.gif using  System.Text;
  3 None.gif using  System.Runtime.InteropServices;
  4 None.gif
  5 None.gif namespace  JillZhang.Security
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7InBlock.gif    public enum  Store
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  9InBlock.gif        USE_NACHINE_STORE=1,USE_USER_STORE
 10ExpandedSubBlockEnd.gif    }
;
 11InBlock.gif    public class DataProtector
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 13InBlock.gif        
 14InBlock.gif        [DllImport("Crypt32.dll",SetLastError=true,CharSet=System.Runtime.InteropServices.CharSet.Auto)]
 15InBlock.gif        private static extern bool CryptProtectData
 16InBlock.gif            (
 17InBlock.gif            ref DATA_BLOB pDataIn,
 18InBlock.gif            String szDataDecr,
 19InBlock.gif            ref DATA_BLOB pOptionEntropy,
 20InBlock.gif            IntPtr pvReserved,
 21InBlock.gif            ref CRYPTPROTECT_PROMPTSTRUCT pPromptStruct,
 22InBlock.gif            int dwFlags,
 23InBlock.gif            ref DATA_BLOB pDataOut
 24InBlock.gif            );
 25InBlock.gif
 26InBlock.gif        [DllImport("Crypt32.dll",SetLastError=true,CharSet=System.Runtime.InteropServices.CharSet.Auto)]
 27InBlock.gif        private static extern bool CryptUnprotectData
 28InBlock.gif            (
 29InBlock.gif            ref DATA_BLOB pDataIn,
 30InBlock.gif            String szDataDecr,
 31InBlock.gif            ref DATA_BLOB pOptionEntropy,
 32InBlock.gif            IntPtr pvReserved,
 33InBlock.gif            ref CRYPTPROTECT_PROMPTSTRUCT pPromptStruct,
 34InBlock.gif            int dwFlags,
 35InBlock.gif            ref DATA_BLOB pDataOut
 36InBlock.gif            );
 37InBlock.gif
 38InBlock.gif        [DllImport("kernel32.dll",CharSet=System.Runtime.InteropServices.CharSet.Auto)]
 39InBlock.gif        private unsafe static extern int FormatMessage
 40InBlock.gif            (
 41InBlock.gif            int dwFlags,
 42InBlock.gif            ref IntPtr lpSource,
 43InBlock.gif            int dwMessageId,
 44InBlock.gif            int dwLanguageId,
 45InBlock.gif            ref String lpBuffer,
 46InBlock.gif            int nSize,
 47InBlock.gif            IntPtr *Arguments
 48InBlock.gif            );
 49InBlock.gif        [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
 50InBlock.gif            internal struct DATA_BLOB
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 52InBlock.gif            public int cbData;
 53InBlock.gif            public IntPtr pbData;
 54ExpandedSubBlockEnd.gif        }

 55InBlock.gif        [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
 56InBlock.gif            internal struct CRYPTPROTECT_PROMPTSTRUCT
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 58InBlock.gif            public  int cbSize;
 59InBlock.gif            public int dwPromptFlags;
 60InBlock.gif            public IntPtr hwndApp;
 61InBlock.gif            public String szPrompt;
 62ExpandedSubBlockEnd.gif        }

 63InBlock.gif        static  private  IntPtr NullPtr=((IntPtr)((int)(0)));
 64InBlock.gif        private const int CRYPTPROTECT_UI_FORBIDDEN=0x1;
 65InBlock.gif        private const int CRYPTPROTECT_LOCAL_MACHINE=0x4;
 66InBlock.gif    
 67InBlock.gif        private Store store;
 68InBlock.gif        public DataProtector(Store tempStore)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 70InBlock.gif            store=tempStore;            
 71ExpandedSubBlockEnd.gif        }

 72InBlock.gif        public byte[] Encrypt(byte[] plainText,byte[] optionalEntropy)
 73ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 74InBlock.gif            bool reVal=false;
 75InBlock.gif            DATA_BLOB plainTextBlob = new DATA_BLOB();
 76InBlock.gif            DATA_BLOB cipherTextBlob=new DATA_BLOB();
 77InBlock.gif            DATA_BLOB entropyBlob = new DATA_BLOB();
 78InBlock.gif            CRYPTPROTECT_PROMPTSTRUCT prompt=new CRYPTPROTECT_PROMPTSTRUCT();
 79InBlock.gif            InitPromptstruct(ref prompt);
 80InBlock.gif            int dwFlags;
 81InBlock.gif            try
 82ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 83InBlock.gif                try
 84ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 85InBlock.gif                    int byteSize=plainText.Length;
 86InBlock.gif                    plainTextBlob.pbData=Marshal.AllocHGlobal(byteSize);
 87InBlock.gif                    if(IntPtr.Zero==plainTextBlob.pbData)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 89InBlock.gif                        throw new Exception("Unable to allocate plaintext buffer:");
 90ExpandedSubBlockEnd.gif                    }

 91InBlock.gif                    plainTextBlob.cbData=byteSize;
 92InBlock.gif                    Marshal.Copy(plainText,0,plainTextBlob.pbData,byteSize);  
 93ExpandedSubBlockEnd.gif                }

 94InBlock.gif                catch(Exception ex)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 96InBlock.gif                    throw new Exception("Exception marshalling data.:"+ex.Message);
 97ExpandedSubBlockEnd.gif                }

 98InBlock.gif                if(Store.USE_NACHINE_STORE==store)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
100InBlock.gif                    //计算机存储区
101InBlock.gif                    dwFlags=CRYPTPROTECT_LOCAL_MACHINE|CRYPTPROTECT_UI_FORBIDDEN;
102InBlock.gif                    if(null==optionalEntropy)
103ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
104InBlock.gif                        optionalEntropy=new byte[0];
105ExpandedSubBlockEnd.gif                    }

106InBlock.gif                    try
107ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
108InBlock.gif                        int byteSize=optionalEntropy.Length;
109InBlock.gif                        entropyBlob.pbData=Marshal.AllocHGlobal(optionalEntropy.Length);
110InBlock.gif                        if(IntPtr.Zero==entropyBlob.pbData)
111ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
112InBlock.gif                            throw new Exception("Unable to allocate entropy data buffer.");
113ExpandedSubBlockEnd.gif                        }

114InBlock.gif                        Marshal.Copy(optionalEntropy,0,entropyBlob.pbData,byteSize);
115InBlock.gif                        entropyBlob.cbData=byteSize;
116ExpandedSubBlockEnd.gif                    }

117InBlock.gif                    catch(Exception ex)
118ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
119InBlock.gif                        throw new Exception("Exception entropy marshalling data."+ex.Message);
120ExpandedSubBlockEnd.gif                    }
    
121ExpandedSubBlockEnd.gif                }

122InBlock.gif                else
123ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
124InBlock.gif                    dwFlags=CRYPTPROTECT_UI_FORBIDDEN;
125ExpandedSubBlockEnd.gif                }

126InBlock.gif                reVal=CryptProtectData(ref plainTextBlob,"",ref entropyBlob,IntPtr.Zero,ref prompt,dwFlags,ref cipherTextBlob);
127InBlock.gif                if(false == reVal)
128ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
129InBlock.gif                    throw new Exception("Encryption failed."+GetErrorMessage(Marshal.GetLastWin32Error()));
130ExpandedSubBlockEnd.gif                }

131ExpandedSubBlockEnd.gif            }

132InBlock.gif            catch(Exception ex)
133ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
134InBlock.gif                throw new Exception("Exception encrypting:"+ex.Message);
135ExpandedSubBlockEnd.gif            }

136InBlock.gif            byte[] cipherText = new byte[cipherTextBlob.cbData];
137InBlock.gif            Marshal.Copy(cipherTextBlob.pbData,cipherText,0,cipherTextBlob.cbData);
138InBlock.gif            return cipherText;
139ExpandedSubBlockEnd.gif        }

140InBlock.gif        public byte[] Decrypt(byte[] ciperText,byte[] optionalEntropy)
141ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
142InBlock.gif            bool reVal=false;
143InBlock.gif            DATA_BLOB plainTextBlob=new DATA_BLOB();
144InBlock.gif            DATA_BLOB cipherBlob=new DATA_BLOB();
145InBlock.gif            CRYPTPROTECT_PROMPTSTRUCT prompt=new CRYPTPROTECT_PROMPTSTRUCT();
146InBlock.gif            InitPromptstruct(ref prompt);
147InBlock.gif            try
148ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
149InBlock.gif                try
150ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
151InBlock.gif                    int cipherTextSize=ciperText.Length;
152InBlock.gif                    cipherBlob.pbData=Marshal.AllocHGlobal(cipherTextSize);
153InBlock.gif                    if(IntPtr.Zero==cipherBlob.pbData)
154ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
155InBlock.gif                        throw new Exception("unable to allocate cipherText buffer.");
156ExpandedSubBlockEnd.gif                    }

157InBlock.gif                    cipherBlob.cbData=cipherTextSize;
158InBlock.gif                    Marshal.Copy(ciperText,0,cipherBlob.pbData,cipherBlob.cbData);
159ExpandedSubBlockEnd.gif                }

160InBlock.gif                catch(Exception ex)
161ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
162InBlock.gif                    throw new Exception("Exception marshalling data."+ex.Message);
163ExpandedSubBlockEnd.gif                }

164InBlock.gif                DATA_BLOB entropyBlob=new DATA_BLOB();
165InBlock.gif                int dwFlags;
166InBlock.gif                if(Store.USE_NACHINE_STORE==store)
167ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
168InBlock.gif                    dwFlags=CRYPTPROTECT_LOCAL_MACHINE|CRYPTPROTECT_UI_FORBIDDEN;
169InBlock.gif                    if(null==optionalEntropy)
170ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
171InBlock.gif                        optionalEntropy=new byte[0];
172ExpandedSubBlockEnd.gif                    }

173InBlock.gif                    try
174ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
175InBlock.gif                        int byteSize=optionalEntropy.Length;
176InBlock.gif                        entropyBlob.pbData=Marshal.AllocHGlobal(byteSize);
177InBlock.gif                        if(IntPtr.Zero==entropyBlob.pbData)
178ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
179InBlock.gif                            throw new Exception("Unable to allocate entropy buffer.");
180ExpandedSubBlockEnd.gif                        }

181InBlock.gif                        entropyBlob.cbData=byteSize;
182InBlock.gif                        Marshal.Copy(optionalEntropy,0,entropyBlob.pbData,byteSize);
183ExpandedSubBlockEnd.gif                    }

184InBlock.gif                    catch(Exception ex)
185ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
186InBlock.gif                        throw new Exception("Exception entropy marshalling data."+ex.Message);
187ExpandedSubBlockEnd.gif                    }

188ExpandedSubBlockEnd.gif                }

189InBlock.gif                else
190ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
191InBlock.gif                    dwFlags=CRYPTPROTECT_UI_FORBIDDEN;
192ExpandedSubBlockEnd.gif                }

193InBlock.gif                reVal=CryptUnprotectData(ref cipherBlob,null,ref entropyBlob,IntPtr.Zero,ref prompt,dwFlags,ref plainTextBlob);
194InBlock.gif                if(false==reVal)
195ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
196InBlock.gif                    throw new Exception("Decryption failed."+GetErrorMessage(Marshal.GetLastWin32Error()));
197ExpandedSubBlockEnd.gif                }

198InBlock.gif                if(IntPtr.Zero!=cipherBlob.pbData)
199ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
200InBlock.gif                    Marshal.FreeHGlobal(cipherBlob.pbData);
201ExpandedSubBlockEnd.gif                }

202InBlock.gif                if(IntPtr.Zero!=entropyBlob.pbData)
203ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
204InBlock.gif                    Marshal.FreeHGlobal(entropyBlob.pbData);
205ExpandedSubBlockEnd.gif                }

206InBlock.gif                
207ExpandedSubBlockEnd.gif            }

208InBlock.gif            catch(Exception ex)
209ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
210InBlock.gif                throw new Exception("Exception decrypting."+ex.Message);
211ExpandedSubBlockEnd.gif            }

212InBlock.gif            byte[] plainText=new byte[plainTextBlob.cbData];
213InBlock.gif            Marshal.Copy(plainTextBlob.pbData,plainText,0,plainTextBlob.cbData);
214InBlock.gif            return plainText;
215ExpandedSubBlockEnd.gif        }

216InBlock.gif
217InBlock.gif        private void InitPromptstruct(ref CRYPTPROTECT_PROMPTSTRUCT ps)
218ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
219InBlock.gif            ps.cbSize=Marshal.SizeOf(typeof(CRYPTPROTECT_PROMPTSTRUCT));
220InBlock.gif            ps.dwPromptFlags=0;
221InBlock.gif            ps.hwndApp=NullPtr;
222InBlock.gif            ps.szPrompt=null;
223ExpandedSubBlockEnd.gif        }

224InBlock.gif        private unsafe static String GetErrorMessage(int errorCode)
225ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
226InBlock.gif            int FORMAT_MESSAGE_ALLOCATE_BUFFER=0x00000100;
227InBlock.gif            int FORMAT_MESSAGE_IGNORE_INSERTS=0x00000200;
228InBlock.gif            int FORMAT_MESSAGE_FROM_SYSTEM=0x00001000;
229InBlock.gif            int messageSize=255;
230InBlock.gif            String lpMsgBuf="";
231InBlock.gif            int dwFlags=FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS;
232InBlock.gif            IntPtr ptrlpSource=new IntPtr();
233InBlock.gif            IntPtr ptrArgument=new IntPtr();
234InBlock.gif            int retVal=FormatMessage(dwFlags,ref ptrlpSource,errorCode,0,ref lpMsgBuf,messageSize,&ptrArgument);
235InBlock.gif            if(0==retVal)
236ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
237InBlock.gif                throw new Exception("Failed to format message for error code"+errorCode+".");
238ExpandedSubBlockEnd.gif            }

239InBlock.gif            return lpMsgBuf;
240ExpandedSubBlockEnd.gif        }

241InBlock.gif
242ExpandedSubBlockEnd.gif    }

243ExpandedBlockEnd.gif}

244 None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值