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

    资料:C# PKCS7加密解密

    //加密字符串
        public string Encryption(string toE)
        {
            //加密和解密必须采用相同的key,具体自己填写,但是必须为32位
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12345678123456781234567812345678");
            RijndaelManaged rijndaelManaged = new RijndaelManaged();
            rijndaelManaged.Key = keyArray;
            rijndaelManaged.Mode = CipherMode.ECB;
            rijndaelManaged.Padding = PaddingMode.PKCS7;
            ICryptoTransform icryptoTransform = rijndaelManaged.CreateEncryptor();
    
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toE);
            byte[] resultArray = icryptoTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    
            return Convert.ToBase64String( resultArray, 0, resultArray.Length);
        }
    
        //解密字符串
        public string Decryption(string toD)
        { 
            //加密和解密必须采用相同的key,具体值自己填,但是必须为32位
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes("12345678123456781234567812345678");
            RijndaelManaged rijndaelManaged = new RijndaelManaged();
            rijndaelManaged.Key = keyArray;
            rijndaelManaged.Mode = CipherMode.ECB;
            rijndaelManaged.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rijndaelManaged.CreateDecryptor();
    
            byte[] toEncryptArray = Convert.FromBase64String(toD);
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    
            return UTF8Encoding.UTF8.GetString(resultArray);
        }

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

    参考资料:C# And Java 3DES加解密 ECB模式/PKCS7

    还需要仔细看,不太符合想找的资料

     参考资料:如何在C#里面实现Java中的PKCS7加密的功能?

    资料地址:https://bbs.csdn.net/topics/340147880

    /// <summary>
            /// 创建签名
            /// </summary>
            /// <param name="data">标签数据</param>
            /// <returns></returns>
            private static byte [] CreateSign(byte[] data)
            {
                if (data == null)
                    throw new ArgumentNullException("数据不能为空");
    
                X509Certificate2 userCert = CreateCertificate(ConfigInfo.certFileName,ConfigInfo.Password);
                if (userCert == null)
                    throw new ArgumentNullException("证书不能为空");
    
                ContentInfo content = new ContentInfo(data);//设置要签名的数据
                SignedCms signedCms = new SignedCms(content);
                CmsSigner signer = new CmsSigner(userCert);
                try
                {
                    signedCms.ComputeSignature(signer);//创建签名
                    return signedCms.Encode();
                }
                catch (CryptographicException ex)
                {
                    throw ex;
                }
    
            }

    /// <summary>
            /// 创建证书
            /// </summary>
            /// <param name="fileName">文件名称</param>
            /// <param name="password">密码</param>
            /// <returns></returns>
            public static X509Certificate2 CreateCertificate(string fileName, string password)
            {
                if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(password))
                {
                    return null;
                }
                try
                {
                    return new X509Certificate2(fileName, password, X509KeyStorageFlags.Exportable);
                }
                catch (CryptographicException ex)
                {
                    throw ex;
                }
            }

    /// <summary>
            /// 校验签名
            /// </summary>
            /// <param name="signature">标签数据</param>
            /// <param name="certificate">证书</param>
            /// <returns></returns>
            public static bool Verify(byte[] signature, byte[] data)
            {
                ContentInfo contentInfo = new ContentInfo(data);
                SignedCms verifyCms = new SignedCms(contentInfo,true);
                try
                {
                    verifyCms.Decode(signature);
                    verifyCms.CheckSignature(true);
                    return true;
                }
                catch (CryptographicException ex)
                {
                    ServiceHub.AddLog(RuntimeLogType.Exception, null, "签名解码失败:" + ex.Message, ex.StackTrace);
                    throw ex;
                }
            }

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

    参考资料:求一位大神帮忙写一个JAVA对应的C#语言的签名方法

    资料地址:https://bbs.csdn.net/topics/392437375

    参考资料:RSA加解密,Java和C#互通

    资料地址:https://blog.csdn.net/thc1987/article/details/81383365

    需要重点看下。

  • 相关阅读:
    MySQL数据库之索引
    python面向对象编程
    linux端口占用
    lintcode
    java基础
    lintcode
    linux 常用命令
    Docker & Kubernates
    angular preparation
    java 命令行
  • 原文地址:https://www.cnblogs.com/Tpf386/p/11959653.html
Copyright © 2011-2022 走看看