zoukankan      html  css  js  c++  java
  • 支付相关-算法知识2

    参考资料:Enveloped and Signed CMS/PKCS #7 Message

    资料地址:https://docs.microsoft.com/en-us/previous-versions/bb885086(v=vs.90)?redirectedfrom=MSDN

    参考资料:

    资料地址:

    project:aspose-pdf/Aspose.Pdf-for-.NET

    File name: Examples/CSharp/AsposePDF/DigitallySign.cs

    using System.IO;
    using System;
    using Aspose.Pdf;
    using Aspose.Pdf.Facades;
    using System.Collections;
    using Aspose.Pdf.Forms;
    
    namespace Aspose.Pdf.Examples.CSharp.AsposePDF.SecuritySignatures
    {
        public class DigitallySign
        {
            public static void Run()
            {
                try
                {
                    // ExStart:DigitallySign
                    // The path to the documents directory.
                    string dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
                    string pbxFile = "";
                    string inFile = dataDir + @"DigitallySign.pdf";
                    string outFile = dataDir + @"DigitallySign_out.pdf";
                    using (Document document = new Document(inFile))
                    {
                        using (PdfFileSignature signature = new PdfFileSignature(document))
                        {
                            PKCS7 pkcs = new PKCS7(pbxFile, "WebSales"); // Use PKCS7/PKCS7Detached objects
                            DocMDPSignature docMdpSignature = new DocMDPSignature(pkcs, DocMDPAccessPermissions.FillingInForms);
                            System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
                            // Set signature appearance
                            signature.SignatureAppearance = dataDir + @"aspose-logo.jpg";
                            // Create any of the three signature types
                            signature.Certify(1, "Signature Reason", "Contact", "Location", true, rect, docMdpSignature);
                            // Save output PDF file
                            signature.Save(outFile);
                        }
                    }
    
                    using (Document document = new Document(outFile))
                    {
                        using (PdfFileSignature signature = new PdfFileSignature(document))
                        {
                            IList sigNames = signature.GetSignNames();
                            if (sigNames.Count > 0) // Any signatures?
                            {
                                if (signature.VerifySigned(sigNames[0] as string)) // Verify first one
                                {
                                    if (signature.IsCertified) // Certified?
                                    {
                                        if (signature.GetAccessPermissions() == DocMDPAccessPermissions.FillingInForms) // Get access permission
                                        {
                                            // Do something
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // ExEnd:DigitallySign
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }

    ----------------------------------------------------------------

    参考资料:Encrypt and Decrypt Data with C#

    资料地址:https://www.codeproject.com/articles/14151/encrypt-and-decrypt-data-with-csharp-2

    public static string Encrypt(string toEncrypt, bool useHashing)
    {
        byte[] keyArray;
        byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
    
        System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
        // Get the key from config file
    
        string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
        //System.Windows.Forms.MessageBox.Show(key);
        //If hashing use get hashcode regards to your key
        if (useHashing)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            //Always release the resources and flush data
            //of the Cryptographic service provide. Best Practice
    
            hashmd5.Clear();
        }
        else
            keyArray = UTF8Encoding.UTF8.GetBytes(key);
    
        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        //set the secret key for the tripleDES algorithm
        tdes.Key = keyArray;
        //mode of operation. there are other 4 modes. We choose ECB(Electronic code Book)
        tdes.Mode = CipherMode.ECB;
        //padding mode(if any extra byte added)
        tdes.Padding = PaddingMode.PKCS7;
    
        ICryptoTransform cTransform = tdes.CreateEncryptor();
        //transform the specified region of bytes array to resultArray
        byte[] resultArray = cTransform.TransformFinalBlock
                (toEncryptArray, 0, toEncryptArray.Length);
        //Release resources held by TripleDes Encryptor
        tdes.Clear();
        //Return the encrypted data into unreadable string format
        return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }
    public static string Decrypt(string cipherString, bool useHashing)
    {
        byte[] keyArray;
        //get the byte code of the string
    
        byte[] toEncryptArray = Convert.FromBase64String(cipherString);
    
        System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
        //Get your key from config file to open the lock!
        string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
    
        if (useHashing)
        {
            //if hashing was used get the hash code with regards to your key
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            //release any resource held by the MD5CryptoServiceProvider
    
            hashmd5.Clear();
        }
        else
        {
            //if hashing was not implemented get the byte code of the key
            keyArray = UTF8Encoding.UTF8.GetBytes(key);
         }
    
        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        //set the secret key for the tripleDES algorithm
        tdes.Key = keyArray;
        //mode of operation. there are other 4 modes.
        //We choose ECB(Electronic code Book)
    
        tdes.Mode = CipherMode.ECB;
        //padding mode(if any extra byte added)
        tdes.Padding = PaddingMode.PKCS7;
    
        ICryptoTransform cTransform = tdes.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock
                (toEncryptArray, 0, toEncryptArray.Length);
        //Release resources held by TripleDes Encryptor
        tdes.Clear();
        //return the Clear decrypted TEXT
        return UTF8Encoding.UTF8.GetString(resultArray);
    }

    -------------------------------------------------------------------------------

    参考资料:https://www.c-sharpcorner.com/UploadFile/f8fa6c/data-encryption-and-decryption-in-C-Sharp/

    资料地址:Data Encryption And Decryption in C#

     
    using System;  
    using System.Security.Cryptography;  
    using System.Text;  
       
    namespace DataEncrypterDecrypter  
    {  
        public class CryptoEngine  
        {  
            public static string Encrypt(string input, string key)  
            {  
                byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input);  
                TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();  
                tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);  
                tripleDES.Mode = CipherMode.ECB;  
                tripleDES.Padding = PaddingMode.PKCS7;  
                ICryptoTransform cTransform = tripleDES.CreateEncryptor();  
                byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);  
                tripleDES.Clear();  
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);  
            }  
            public static string Decrypt(string input, string key)  
            {  
                byte[] inputArray = Convert.FromBase64String(input);  
                TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();  
                tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);  
                tripleDES.Mode = CipherMode.ECB;  
                tripleDES.Padding = PaddingMode.PKCS7;  
                ICryptoTransform cTransform = tripleDES.CreateDecryptor();  
                byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);  
                tripleDES.Clear();   
                return UTF8Encoding.UTF8.GetString(resultArray);  
            }  
        }  
    }  

    关键词:Decrypting PKCS#7 encrypted data in C#

    地址:https://stackoverflow.com/questions/1503974/decrypting-pkcs7-encrypted-data-in-c-sharp

    地址:https://csharp.hotexamples.com/examples/iTextSharp.text.pdf/PdfPKCS7/-/php-pdfpkcs7-class-examples.html

  • 相关阅读:
    图片验证码, 登录, 注销, 修改密码
    注册页面及注册功能实现
    高级配置文件, csrf, django settings源码, django auth模块, 文件配置的插拔式设计
    cookie操作, session操作, django中间件
    半自动创建多对多关系表, forms组件
    sweetalert, bulk_create, 分页器
    orm查询优化, MVC与MTV, choices参数, ajax
    聚合查询, 分组查询, F与Q查询, 常见字段及参数, 自定义Char字段, 事务操作
    Contest2058
    ACM版《孔乙己》
  • 原文地址:https://www.cnblogs.com/Tpf386/p/11959764.html
Copyright © 2011-2022 走看看