Ejemplo n.º 1
0
        /// <summary>
        /// 给任意长度字符串解密(AES)
        /// </summary>
        /// <param name="toDecrypt">待解密数据base64字符串</param>
        /// <param name="key">密钥</param>
        /// <param name="strOut">解密后数据</param>
        /// <returns>成功与否</returns>
        public static bool Decrypt_AES_B64(string toDecrypt, string key, out string strOut)
        {
            strOut = "";
            if (key == "")
            {
                strOut = toDecrypt;
                return(true);
            }
            try
            {
                byte[] keyArray = new byte[16];
                byte[] bkey     = UTF8Encoding.UTF8.GetBytes(key);

                for (int i = 0; i < 16; i++)
                {
                    if (i < bkey.Length)
                    {
                        keyArray[i] = bkey[i];
                    }
                }

                if (toDecrypt.Length % 2 != 0)
                {
                    return(false);
                }


                int    iTotal         = toDecrypt.Length / 2;
                byte[] toDecryptArray = Convert.FromBase64String(toDecrypt);
                Service_NCSB.WriteLog("FromBase64String OK!");
                //byte[] toDecryptArray = new byte[toDecrypt.Length / 2];
                //for (int i = 0; i < toDecrypt.Length; i = i + 2)
                //{
                //    strTmp = toDecrypt[i].ToString() + toDecrypt[i + 1].ToString();
                //    bTmp = Convert.ToByte(strTmp, 16);
                //    toDecryptArray[i / 2] = bTmp;
                //}

                RijndaelManaged rDel = new RijndaelManaged();

                rDel.Key = keyArray;

                rDel.Mode = CipherMode.ECB;

                rDel.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = rDel.CreateDecryptor();
                Service_NCSB.WriteLog("toDecryptArray.Length [" + toDecryptArray.Length.ToString() + "]");
                byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);
                Service_NCSB.WriteLog("TransformFinalBlock OK!");
                strOut = UTF8Encoding.UTF8.GetString(resultArray);
                //strOut = Encoding.Default.GetString(resultArray);
            }
            catch (Exception err)
            {
                Service_NCSB.WriteLog(err.Message);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// RSA加密
        /// </summary>
        /// <param name="publickey"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RSAEncrypt(string publickey, string content)
        {
            string strResult = "eyJtZXRob2QiOiJzZWFyY2hiaWxsIiwicGFyYW1ldGVyIjp7InVuaXRpZCI6IjAwMDA3NDc5In19";

            publickey = @"<RSAKeyValue><Modulus>AJ7FqII2daSG8AeHjP96ZMEaXRNYpKCiW33GzQ2BYTaler3tMWVOum2InQtcX2AD/qYvwyvfHZ/eHi7oeDsilqk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            byte[] cipherbytes;
            rsa.FromXmlString(publickey);
            Service_NCSB.WriteLog(content);
            content     = "http://127.0.0.1:8080/wems/interface/3rdpart";
            cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
            Service_NCSB.WriteLog("Encrypt over!");
            byte[] arr = new byte[cipherbytes.Length - 1];

            if (cipherbytes[0] == 0)
            {
                Array.Copy(cipherbytes, 1, arr, 0, cipherbytes.Length - 1);
                strResult = Convert.ToBase64String(arr);
            }
            else
            {
                strResult = Convert.ToBase64String(cipherbytes);
            }

            Service_NCSB.WriteLog(strResult);
            return(strResult);
        }
Ejemplo n.º 3
0
        public static bool CompressFile(string SourceFile, string DestinationFile)
        {
            if (File.Exists(SourceFile) == false)//判断文件是否存在
            {
                Service_NCSB.WriteLog("文件[" + SourceFile + "]不存在!");
                return(false);
            }
            //创建文件流和字节数组
            byte[]     buffer            = null;
            FileStream sourceStream      = null;
            FileStream destinationStream = null;
            GZipStream compressStream    = null;

            try
            {
                sourceStream = new FileStream(SourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                buffer       = new byte[sourceStream.Length];
                //把文件流存放到字节数组中
                int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
                if (checkCounter != buffer.Length)
                {
                    Service_NCSB.WriteLog("文件[" + SourceFile + "]读错误!");
                    return(false);
                }
                destinationStream = new FileStream(DestinationFile, FileMode.OpenOrCreate, FileAccess.Write);
                //创建GzipStream实例,写入压缩的文件流
                compressStream = new GZipStream(destinationStream, CompressionMode.Compress, true);
                compressStream.Write(buffer, 0, buffer.Length);
            }
            catch (Exception err)
            {
                Service_NCSB.WriteLog("压缩文件时发生错误[" + err.Message + "]");
                return(false);
            }
            finally
            {
                //确保总是关闭所有的流
                if (compressStream != null)
                {
                    compressStream.Close();
                }
                if (sourceStream != null)
                {
                    sourceStream.Close();
                }
                if (destinationStream != null)
                {
                    destinationStream.Close();
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// AES解密文件
        /// </summary>
        /// <param name="strInFileName">待解密文件名</param>
        /// <param name="strKey">解密密钥</param>
        /// <param name="strOutFileName">解密后文件名</param>
        /// <returns></returns>
        public static bool Decrypt_AES_File(string strInFileName, string strKey, string strOutFileName)
        {
            Service_NCSB.WriteLog("strInFileName=[" + strInFileName + "]");
            Service_NCSB.WriteLog("strKey=[" + strKey + "]");
            Service_NCSB.WriteLog("strOutFileName=[" + strOutFileName + "]");
            if (strInFileName == strOutFileName)
            {//待解密文件名与解密后文件名不能重复
                return(false);
            }
            try
            {
                StreamReader sr = File.OpenText(Service_NCSB.MyPath + strInFileName);
                File.Delete(Service_NCSB.MyPath + strOutFileName);

                StreamWriter sw = new StreamWriter(Service_NCSB.MyPath + strOutFileName, false, Encoding.GetEncoding("GB2312"));
                string       strLine;
                strLine = sr.ReadLine();
                //strLine = "120140000001|银海住房公积金核心系统|20140101|2000.00|文件内容加解密测试|萧刘|yinhai";
                string strOutLine = "";
                while (strLine != null)
                {
                    Service_NCSB.WriteLog("strLine=[" + strLine + "]");
                    if (!Decrypt_AES_H(strLine, strKey, out strOutLine))
                    {
                        sr.Close();
                        sw.Close();
                        return(false);
                    }
                    //if (!U2G(strTmp, out strOutLine))
                    //{
                    //    Service_PZHSB.WriteLog("U2G 错误");
                    //    sr.Close();
                    //    sw.Close();
                    //    return false;
                    //}
                    Service_NCSB.WriteLog("strOutLine=[" + strOutLine + "]");
                    sw.WriteLine(strOutLine);
                    strLine = sr.ReadLine();
                }

                sr.Close();
                sw.Close();
            }
            catch (Exception err)
            {
                Service_NCSB.WriteLog("err=[" + err.Message + "]");
                return(false);
            }

            return(true);
        }
Ejemplo n.º 5
0
Archivo: FTP.cs Proyecto: yusislv/NCDC
        //从ftp服务器上载文件的功能
        public bool Put(string path, string filename, out string errinfo)
        {
            errinfo = "执行成功";
            FileInfo fileInf = new FileInfo(filename);
            string   uri     = "ftp://" + ftpServerIP + "/" + path + "/" + fileInf.Name;

            if (path == "")
            {
                uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
            }

            Service_NCSB.WriteLog("uri=[" + uri + "]");
            try
            {
                Connect(uri);//连接
                // 默认为true,连接不会被关闭
                // 在一个命令之后被执行
                reqFTP.KeepAlive = false;
                // 指定执行什么命令
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                // 上传文件时通知服务器文件的大小
                reqFTP.ContentLength = fileInf.Length;
                // 缓冲大小设置为kb
                int    buffLength = 2048;
                byte[] buff       = new byte[buffLength];
                int    iReadLen;
                // 打开一个文件流(System.IO.FileStream) 去读上传的文件
                FileStream fs = fileInf.OpenRead();
                // 把上传的文件写入流
                Stream stmFtpPut = reqFTP.GetRequestStream();
                // 每次读文件流的内容存到缓冲区buff
                iReadLen = fs.Read(buff, 0, buffLength);
                // 流内容没有结束
                while (iReadLen != 0)
                {
                    // 把缓冲区buff的内容写入stmFtpPut
                    stmFtpPut.Write(buff, 0, iReadLen);
                    iReadLen = fs.Read(buff, 0, buffLength);
                }
                // 关闭两个流
                stmFtpPut.Close();
                fs.Close();
            }
            catch (Exception err)
            {
                errinfo = err.Message;
                return(false);
            }
            return(true);
        }
Ejemplo n.º 6
0
        public static bool U2G(string strUTF8, out string strGB2312)
        {
            strGB2312 = "";
            try
            {
                byte[] bUTF8 = UTF8Encoding.UTF8.GetBytes(strUTF8);
                strGB2312 = Encoding.GetEncoding("gb2312").GetString(bUTF8);
            }
            catch (Exception err)
            {
                Service_NCSB.WriteLog("err=[" + err.Message + "]");
                return(false);
            }


            return(true);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// AES加密文件
        /// </summary>
        /// <param name="strInFileName">待加密文件名</param>
        /// <param name="strKey">加密密钥</param>
        /// <param name="strOutFileName">加密后文件名</param>
        /// <returns></returns>
        public static bool Encrypt_AES_File(string strInFileName, string strKey, string strOutFileName)
        {
            //Service_PZHSB.WriteLog("strInFileName=[" + strInFileName + "]");
            //Service_PZHSB.WriteLog("strKey=[" + strKey + "]");
            //Service_PZHSB.WriteLog("strOutFileName=[" + strOutFileName + "]");
            if (strInFileName == strOutFileName)
            {//待加密文件名与加密后文件名不能重复
                return(false);
            }
            try
            {
                StreamReader sr = new StreamReader(Service_NCSB.MyPath + strInFileName, Encoding.Default);
                File.Delete(Service_NCSB.MyPath + strOutFileName);
                //StreamWriter sw = File.CreateText(Service_PZHSB.MyPath + strOutFileName);
                System.Text.UTF8Encoding utf8 = new UTF8Encoding(false);//去除utf8文件开头的BOM
                StreamWriter             sw   = new StreamWriter(Service_NCSB.MyPath + strOutFileName, false, utf8);
                sw.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = "", strOutLine = "";

                while ((strLine = sr.ReadLine()) != null)
                {
                    //Service_PZHSB.WriteLog("strLine=[" + strLine + "]");
                    if (!Encrypt_AES_H(strLine, strKey, out strOutLine))
                    {
                        sr.Close();
                        sw.Close();
                        return(false);
                    }
                    //Service_PZHSB.WriteLog("strOutLine=[" + strOutLine + "]");
                    sw.WriteLine(strOutLine);
                }
                sw.Flush();
                sr.Close();
                sw.Close();
            }
            catch (Exception err)
            {
                Service_NCSB.WriteLog("err=[" + err.Message + "]");
                return(false);
            }

            return(true);
        }
Ejemplo n.º 8
0
Archivo: FTP.cs Proyecto: yusislv/NCDC
        //从ftp服务器下载文件的功能
        public bool Get(string filePath, string remotepath, string fileName, out string errorinfo)
        {
            errorinfo = "执行成功";
            try
            {
                String onlyFileName = Path.GetFileName(fileName);
                string newFileName  = filePath + "\\" + onlyFileName;
                //if (File.Exists(newFileName))
                //{
                //    errorinfo = string.Format("本地文件{0}已存在,无法下载", newFileName);
                //    return false;
                //}
                string url = "ftp://" + ftpServerIP + "/" + remotepath + "/" + fileName;
                Service_NCSB.WriteLog("url=[" + url + "]");
                Connect(url);//连接
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response   = (FtpWebResponse)reqFTP.GetResponse();
                Stream         ftpStream  = response.GetResponseStream();
                long           cl         = response.ContentLength;
                int            bufferSize = 2048;
                byte[]         buffer     = new byte[bufferSize];
                int            readCount  = ftpStream.Read(buffer, 0, bufferSize);
                //如果文件存在则被覆盖
                FileStream outputStream = new FileStream(newFileName, FileMode.Create);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
                ftpStream.Close();
                outputStream.Close();
                response.Close();
            }

            catch (Exception err)
            {
                errorinfo = err.Message;
                return(false);
            }

            return(true);
        }
Ejemplo n.º 9
0
        public static string GetMD5HashFromFile(string FileName)
        {
            try
            {
                FileStream fs = new FileStream(FileName, FileMode.Open);
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(fs);
                fs.Close();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }

                return(sb.ToString());
            }
            catch (Exception err)
            {
                Service_NCSB.WriteLog(err.Message);
                return("");
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// AES解密Base64文件
        /// </summary>
        /// <param name="strInFileName">待解密文件名</param>
        /// <param name="strKey">解密密钥</param>
        /// <param name="strOutFileName">解密后文件名</param>
        /// <returns></returns>
        public static bool Decrypt_AES_File(string strInFileName, string strKey, string strOutFileName, string strFileType)
        {
            Service_NCSB.WriteLog("strInFileName=[" + strInFileName + "]");
            Service_NCSB.WriteLog("strKey=[" + strKey + "]");
            Service_NCSB.WriteLog("strOutFileName=[" + strOutFileName + "]");
            Service_NCSB.WriteLog("strFileType=[" + strFileType + "]");
            if (strInFileName == strOutFileName)
            {//待解密文件名与解密后文件名不能重复
                return(false);
            }
            try
            {
                StreamReader sr = File.OpenText(Service_NCSB.MyPath + strInFileName);
                File.Delete(Service_NCSB.MyPath + strOutFileName);

                StreamWriter sw = new StreamWriter(Service_NCSB.MyPath + strOutFileName, false, Encoding.GetEncoding("GB2312"));
                string       strLine;
                strLine = sr.ReadLine();
                //strLine = "120140000001|银海住房公积金核心系统|20140101|2000.00|文件内容加解密测试|萧刘|yinhai";
                string strOutLine = "";
                while (strLine != null)
                {
                    Service_NCSB.WriteLog("strLine=[" + strLine + "]");
                    switch (strFileType)
                    {
                    case "B64":    //Base64文件
                        if (!Decrypt_AES_B64(strLine, strKey, out strOutLine))
                        {
                            sr.Close();
                            sw.Close();
                            return(false);
                        }
                        break;

                    case "H":    //16进制文件
                        if (!Decrypt_AES_H(strLine, strKey, out strOutLine))
                        {
                            sr.Close();
                            sw.Close();
                            return(false);
                        }
                        break;

                    default:
                        Service_NCSB.WriteLog("FileType错误![" + strFileType + "]");
                        return(false);
                    }
                    Service_NCSB.WriteLog("strOutLine=[" + strOutLine + "]");
                    sw.WriteLine(strOutLine);
                    strLine = sr.ReadLine();
                }

                sr.Close();
                sw.Close();
            }
            catch (Exception err)
            {
                Service_NCSB.WriteLog("err=[" + err.Message + "]");
                return(false);
            }

            return(true);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// AES解密文件 省社保养老金代发专用
        /// </summary>
        /// <param name="strInFileName">待解密文件名</param>
        /// <param name="strUserPassword">用户密码</param>
        /// <param name="strBankCode">银行编号</param>
        /// <param name="strOutFileName">解密后文件名</param>
        /// <returns></returns>
        public static bool DecFile_SCSBYLJ(string strInFileName, string strUserPassword, string strBankCode, string strOutFileName)
        {
            Service_NCSB.WriteLog("strInFileName=[" + strInFileName + "]");
            Service_NCSB.WriteLog("strUserPassword=[" + strUserPassword + "]");
            Service_NCSB.WriteLog("strBankCode=[" + strBankCode + "]");
            Service_NCSB.WriteLog("strOutFileName=[" + strOutFileName + "]");
            string strYHPass = "******";
            string strKey;      //解密密钥
            string strPassword; //密码字符串

            strPassword = strUserPassword + strYHPass + strBankCode;
            Service_NCSB.WriteLog("strPassword=[" + strPassword + "]");
            if (strInFileName == strOutFileName)
            {//待解密文件名与解密后文件名不能重复
                return(false);
            }
            try
            {
                //StreamReader sr = File.OpenText(Service_PZHSB.MyPath + strInFileName);
                StreamReader sr = File.OpenText("C:\\ftp\\" + strInFileName);
                File.Delete(Service_NCSB.MyPath + strOutFileName);

                StreamWriter sw = new StreamWriter("C:\\ftp\\" + strOutFileName, false, Encoding.GetEncoding("GB2312"));
                string       strLine;
                strLine = sr.ReadLine();
                //strLine = "120140000001|银海住房公积金核心系统|20140101|2000.00|文件内容加解密测试|萧刘|yinhai";
                string    strOutLine = "";
                int       iCount     = 0;
                ArrayList arrLine    = new ArrayList();
                MD5       MyMD5      = new MD5CryptoServiceProvider();
                while (strLine != null)
                {
                    Service_NCSB.WriteLog("strLine=[" + strLine + "]");
                    string strMD5 = strPassword + iCount.ToString();
                    //Service_PZHSB.WriteLog("MD5=[" + strMD5 + "]");
                    string strMD5Out = BitConverter.ToString((MyMD5.ComputeHash(Encoding.Default.GetBytes(strMD5)))).Replace("-", "");
                    //Service_PZHSB.WriteLog("strMD5Out=[" + strMD5Out + "]");
                    strKey = strMD5Out.Substring(8, 16);
                    strKey = strKey.ToLower();
                    //Service_PZHSB.WriteLog("strKey=[" + strKey + "]");
                    if (!Decrypt_AES_B64(strLine, strKey, out strOutLine))
                    {
                        Service_NCSB.WriteLog("Decrypt_AES_B64错误[" + iCount.ToString() + "]");
                        sr.Close();
                        sw.Close();
                        return(false);
                    }
                    //if (!U2G(strTmp, out strOutLine))
                    //{
                    //    Service_PZHSB.WriteLog("U2G 错误");
                    //    sr.Close();
                    //    sw.Close();
                    //    return false;
                    //}

                    Service_NCSB.WriteLog("strOutLine=[" + strOutLine + "]");
                    arrLine.Add(strOutLine);
                    //sw.WriteLine(strOutLine);
                    strLine = sr.ReadLine();
                    iCount++;
                }
                //数据校验
                for (int i = 0; i < arrLine.Count; i++)
                {
                    string strHash = ((string)arrLine[i]).Substring(0, 32);
                    string strTmp  = i.ToString() + ((string)arrLine[i]).Substring(32) + arrLine.Count.ToString();
                    string strMD5  = BitConverter.ToString((MyMD5.ComputeHash(Encoding.Default.GetBytes(strTmp)))).Replace("-", "");
                    Service_NCSB.WriteLog("strHash=[" + strHash + "]");
                    Service_NCSB.WriteLog("strTmp=[" + strTmp + "]");
                    Service_NCSB.WriteLog("strMD5=[" + strMD5 + "]");
                    if (strHash != strMD5.ToLower())
                    {
                        Service_NCSB.WriteLog("数据校验失败,行数[" + i.ToString() + "]");
                        //return false;
                    }
                    sw.WriteLine(((string)arrLine[i]).Substring(32));
                }

                sr.Close();
                sw.Close();
            }
            catch (Exception err)
            {
                Service_NCSB.WriteLog("err=[" + err.Message + "]");
                return(false);
            }

            return(true);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 字符串分隔符文件转xml文件
        /// </summary>
        /// <param name="XmlFileName"></param>
        /// <param name="StrFileName"></param>
        /// <param name="strRetMsg"></param>
        /// <returns></returns>
        public static bool StrFile2XmlFile(string StrFileName, string XmlFileName, out string strRetMsg)
        {
            strRetMsg = "交易成功";
            string XmlFileInfo = "";

            //Service_NCSB.WriteLog("XmlFileName:" + XmlFileName);
            //Service_NCSB.WriteLog("StrFileName:" + StrFileName);

            try
            {
                string[] strLines = File.ReadAllLines(StrFileName);
                int      iLine    = 0;
                while (iLine < strLines.Length)
                {
                    //Service_NCSB.WriteLog(strLines[iLine]);
                    string[] jy = strLines[iLine].Split('|');
                    iLine++;
                    if (iLine == 1)//第一行
                    {
                        if (jy.Length < 3)
                        {
                            strRetMsg = "第[" + iLine + "]行数据错误!";
                            Service_NCSB.WriteLog(strRetMsg);
                            return(false);
                        }
                        XmlFileInfo  = "<dz>\n\t<yhbh>" + jy[0] + "</yhbh>\n\t<dzsj>\n\t\t<jykssj>";
                        XmlFileInfo += jy[1] + "</jykssj>\n\t\t<jyjssj>";
                        XmlFileInfo += jy[2] + "</jyjssj>\n\t</dzsj>\n\t<dzjy>\n";
                        continue;
                    }
                    if (jy.Length < 14)
                    {
                        strRetMsg = "第[" + iLine + "]行数据错误!";
                        Service_NCSB.WriteLog(strRetMsg);
                        return(false);
                    }
                    XmlFileInfo += "\t\t<jy>\n\t\t\t<jylsh>" + jy[0] + "</jylsh>\n";
                    XmlFileInfo += "\t\t\t<grbm>" + jy[1] + "</grbm>\n";
                    XmlFileInfo += "\t\t\t<jfxz>" + jy[2] + "</jfxz>\n";
                    XmlFileInfo += "\t\t\t<ksqh>" + jy[3] + "</ksqh>\n";
                    XmlFileInfo += "\t\t\t<jzqh>" + jy[4] + "</jzqh>\n";
                    XmlFileInfo += "\t\t\t<jfdc>" + jy[5] + "</jfdc>\n";
                    XmlFileInfo += "\t\t\t<sbjbh>" + jy[6] + "</sbjbh>\n";
                    XmlFileInfo += "\t\t\t<yjbj>" + jy[7] + "</yjbj>\n";
                    XmlFileInfo += "\t\t\t<lx>" + jy[8] + "</lx>\n";
                    XmlFileInfo += "\t\t\t<znj>" + jy[9] + "</znj>\n";
                    XmlFileInfo += "\t\t\t<cwsxh>" + jy[10] + "</cwsxh>\n";
                    XmlFileInfo += "\t\t\t<jysj>" + jy[11] + "</jysj>\n";
                    XmlFileInfo += "\t\t\t<yhhh>" + jy[12] + "</yhhh>\n";
                    XmlFileInfo += "\t\t\t<jylx>" + jy[13] + "</jylx>\n\t\t</jy>\n";
                }

                XmlFileInfo += "\t</dzjy>\n</dz>";

                File.WriteAllText(XmlFileName, XmlFileInfo);
                return(true);
            }
            catch (Exception err)
            {
                strRetMsg = err.Message;
                return(false);
            }

            //return false;
        }