复习做UWP时涉及到的两种加密具名相关lovebet爱博体育

自个儿新手一枚,大学里凭兴趣学了几许WP的肤浅,后来又有幸(大概不幸)的进了一家专注于Windows生态的铺面做了1段时间的UWP。在博客园写点自个儿碰着的事物,作为享受,也视作家组织调的备忘,固然有错误的地方,也许能够升官B格的地点,希望园子里的大神们不吝赐教。

自身菜鸟一枚,高校里凭兴趣学了少数WP的肤浅,后来又有幸(或许不幸)的进了一家专注于Windows生态的店四做了一段时间的UWP。在网易写点自个儿遇到的事物,作为享受,也作为协调的备忘,假若有荒唐的地点,或然能够升官B格的地点,希望园子里的大神们不吝赐教。

初进市4时,企业要做开拓有关的事体,要求和支付宝、优易付、爱贝等支出路子对接,对菜鸟的自个儿来说,加密或许是签订契约简直难到死,高校里哪用过那个,OMG,只可以迎着头皮找资料。

初进商场时,公司要做开拓有关的事务,必要和支付宝、优易付、爱贝等花费路子对接,对新手的自小编来讲,加密要么是签订契约大致难到死,高校里哪用过这么些,OMG,只好迎着头皮找材质。

此处作者想请教咱们2个难点,正是如何学习。例如当您遇见一个目生的事物,你是何许找寻资料化解难点的?当自家急需做加密的时候自个儿的做法是开拓百度寻觅“WP
3des加密”,那样找寻来的结果基本都无法用。。。后来终究在新浪看来了两篇有关的博客,作者才精通原来要落到实处相关职能要求哪多少个类哪多少个方法,这些历程差不多用了一周吧,作用超低。

此处自个儿想请教我们三个标题,就是什么学习。例如当您超过3个素不相识的事物,你是怎么样搜索资料化解难题的?当自家索要做加密的时候自个儿的做法是张开百度找出“WP
三des加密”,那样寻找来的结果基本都不可能用。。。后来算是在天涯论坛看来了两篇有关的博客,小编才掌握原来要落到实处相关作用必要哪多少个类哪多少个方法,那一个进度差不离用了15日吧,功能超低。

不留余地自个儿热切的两篇博客分别是:

消除自身急迫的两篇博客分别是:

王磊:http://www.cnblogs.com/webabcd/archive/2013/06/03/3114657.html

王磊:http://www.cnblogs.com/webabcd/archive/2013/06/03/3114657.html

老周:http://www.cnblogs.com/tcjiaan/p/4303918.html

老周:http://www.cnblogs.com/tcjiaan/p/4303918.html

看了那两篇博客就明白该利用什么类了,再接下去就相对轻便了。格外多谢两位名师的分享和引导。

看了那两篇博客就知晓该采Nash么类了,再接下去就相对简单了。非凡谢谢两位老师的分享和辅导。

在RT应用中,涉及到加/解密的API都在偏下多少个命名空间里:

在RT应用中,涉及到加/解密的API都在以下多少个命名空间里:

1、Windows.Security.Cryptography

1、Windows.Security.Cryptography

2、Windows.Security.Cryptography.Core

2、Windows.Security.Cryptography.Core

3、Windows.Security.Cryptography.DataProtection

3、Windows.Security.Cryptography.DataProtection

接下去分享一下自小编在档次中做过的多少个加密/签字,进行了弹指间简练的包装,还望大家教导

接下去分享一下小编在类型中做过的多少个加密/签字,举行了须臾间归纳的包裹,还望大家引导

1.3des加密

1.3des加密

lovebet爱博体育 1lovebet爱博体育 2

lovebet爱博体育 3lovebet爱博体育 4

    internal sealed class TripleDesEncryptHelper
    {

        //加/解密 第一步 
        //通过SymmetricKeyAlgorithmProvider的静态方法OpenAlgorithm()得到一个SymmetricKeyAlgorithmProvider实例
        //该方法参数是要使用的加/解密算法的名字
        internal static SymmetricKeyAlgorithmProvider syprd = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.TripleDesEcb);


        /// <summary>
        /// 加密函数
        /// </summary>
        /// <param name="data">需要加密的字符串</param>
        /// <returns>返回加密后的结果</returns>
        public static string Encrypt(string data, string key)
        {
            string encryptedData = null;
            try
            {
                byte[] dataBytes = Encoding.UTF8.GetBytes(data);
                //这里我自己写了一个pkcs5对齐,不过后来有看到过C#pkcs5和pkcs7是一样的说法,有待验证
                byte[] pkcs5databytes = pkcs5(dataBytes);
                IBuffer databuffer = pkcs5databytes.AsBuffer();

                byte[] keyBytes = Convert.FromBase64String(key);
                IBuffer keybuffer = keyBytes.AsBuffer();
                //构造CryptographicKey
                CryptographicKey cryptographicKey = syprd.CreateSymmetricKey(keybuffer);
                //加密
                //Encrypt需要三个参数分别为加密使用的Key,需要加密的Data,以及向量IV
                //Des加密中Ecb模式和Cbc模式的区别就在于是否必须使用向量IV
                IBuffer cryptBuffer = CryptographicEngine.Encrypt(cryptographicKey, databuffer, null);      **************/
                byte[] enctyptedBytes = cryptBuffer.ToArray();
                //进行base64编码
                encryptedData = Convert.ToBase64String(enctyptedBytes); 
            }
            catch (Exception ex)
            {
                DebugHelper.Log("TripleDesEncryptHelper.Encrypt", ex.Message);
            }
            return encryptedData.Trim();
        }

        /// <summary>
        /// 解密函数
        /// </summary>
        /// <param name="data">待解密的字符串</param>
        /// <returns>解密后的数据</returns>
        public static string Decrypt(string data, string key)
        {
            string decryptedData = null;
            try
            {
                byte[] dataBytes = Convert.FromBase64String(data);
                IBuffer dataBuffer = dataBytes.AsBuffer();
                byte[] keyBytes = Convert.FromBase64String(key);
                IBuffer keybuffer = keyBytes.AsBuffer();
                CryptographicKey cryptographicKey = syprd.CreateSymmetricKey(keybuffer);
                IBuffer decryptedBuffer = CryptographicEngine.Decrypt(cryptographicKey, dataBuffer, null);
                decryptedData = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, decryptedBuffer);
                //防止乱码
                var result = System.Text.RegularExpressions.Regex.Match(decryptedData, "[a-zA-Z0-9]*");
                decryptedData = result.ToString();
            }
            catch (Exception ex)
            {
                DebugHelper.Log("TripleDesEncryptHelper.Decrypt", ex.Message);
            }
            return decryptedData;
        }




        /// <summary>
        /// 把数据进行pkcs5对齐
        /// </summary>
        /// <param name="databytes">待处理的数据</param>
        /// <returns></returns>
        private static byte[] pkcs5(byte[] databytes)
        {
            byte[] newBytes = null;

            int datalength = databytes.Length;
            int blocksize = (int)syprd.BlockLength;
            if (!(datalength % blocksize == 0))
            {
                int need = blocksize - (datalength % 8);
                newBytes = new byte[datalength + need];
                for (int i = 0; i < datalength; i++)
                {
                    newBytes[i] = databytes[i];
                }
                byte xx = 0x0;
                switch (need)
                {
                    case 1:
                        xx = 0x1;
                        break;
                    case 2:
                        xx = 0x2;
                        break;
                    case 3:
                        xx = 0x3;
                        break;
                    case 4:
                        xx = 0x4;
                        break;
                    case 5:
                        xx = 0x5;
                        break;
                    case 6:
                        xx = 0x6;
                        break;
                    case 7:
                        xx = 0x7;
                        break;
                }
                for (int i = 0; i < need; i++)
                {
                    newBytes[datalength + i] = xx;
                }
            }
            else
            {
                newBytes = new byte[datalength + 8];
                for (int i = 0; i < datalength; i++)
                {
                    newBytes[i] = databytes[i];
                }
                byte xx = 0x8;
                for (int i = 0; i < 8; i++)
                {
                    newBytes[datalength + i] = xx;
                }
            }
            return newBytes;
        }
}
    internal sealed class TripleDesEncryptHelper
    {

        //加/解密 第一步 
        //通过SymmetricKeyAlgorithmProvider的静态方法OpenAlgorithm()得到一个SymmetricKeyAlgorithmProvider实例
        //该方法参数是要使用的加/解密算法的名字
        internal static SymmetricKeyAlgorithmProvider syprd = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.TripleDesEcb);


        /// <summary>
        /// 加密函数
        /// </summary>
        /// <param name="data">需要加密的字符串</param>
        /// <returns>返回加密后的结果</returns>
        public static string Encrypt(string data, string key)
        {
            string encryptedData = null;
            try
            {
                byte[] dataBytes = Encoding.UTF8.GetBytes(data);
                //这里我自己写了一个pkcs5对齐,不过后来有看到过C#pkcs5和pkcs7是一样的说法,有待验证
                byte[] pkcs5databytes = pkcs5(dataBytes);
                IBuffer databuffer = pkcs5databytes.AsBuffer();

                byte[] keyBytes = Convert.FromBase64String(key);
                IBuffer keybuffer = keyBytes.AsBuffer();
                //构造CryptographicKey
                CryptographicKey cryptographicKey = syprd.CreateSymmetricKey(keybuffer);
                //加密
                //Encrypt需要三个参数分别为加密使用的Key,需要加密的Data,以及向量IV
                //Des加密中Ecb模式和Cbc模式的区别就在于是否必须使用向量IV
                IBuffer cryptBuffer = CryptographicEngine.Encrypt(cryptographicKey, databuffer, null);      **************/
                byte[] enctyptedBytes = cryptBuffer.ToArray();
                //进行base64编码
                encryptedData = Convert.ToBase64String(enctyptedBytes); 
            }
            catch (Exception ex)
            {
                DebugHelper.Log("TripleDesEncryptHelper.Encrypt", ex.Message);
            }
            return encryptedData.Trim();
        }

        /// <summary>
        /// 解密函数
        /// </summary>
        /// <param name="data">待解密的字符串</param>
        /// <returns>解密后的数据</returns>
        public static string Decrypt(string data, string key)
        {
            string decryptedData = null;
            try
            {
                byte[] dataBytes = Convert.FromBase64String(data);
                IBuffer dataBuffer = dataBytes.AsBuffer();
                byte[] keyBytes = Convert.FromBase64String(key);
                IBuffer keybuffer = keyBytes.AsBuffer();
                CryptographicKey cryptographicKey = syprd.CreateSymmetricKey(keybuffer);
                IBuffer decryptedBuffer = CryptographicEngine.Decrypt(cryptographicKey, dataBuffer, null);
                decryptedData = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, decryptedBuffer);
                //防止乱码
                var result = System.Text.RegularExpressions.Regex.Match(decryptedData, "[a-zA-Z0-9]*");
                decryptedData = result.ToString();
            }
            catch (Exception ex)
            {
                DebugHelper.Log("TripleDesEncryptHelper.Decrypt", ex.Message);
            }
            return decryptedData;
        }




        /// <summary>
        /// 把数据进行pkcs5对齐
        /// </summary>
        /// <param name="databytes">待处理的数据</param>
        /// <returns></returns>
        private static byte[] pkcs5(byte[] databytes)
        {
            byte[] newBytes = null;

            int datalength = databytes.Length;
            int blocksize = (int)syprd.BlockLength;
            if (!(datalength % blocksize == 0))
            {
                int need = blocksize - (datalength % 8);
                newBytes = new byte[datalength + need];
                for (int i = 0; i < datalength; i++)
                {
                    newBytes[i] = databytes[i];
                }
                byte xx = 0x0;
                switch (need)
                {
                    case 1:
                        xx = 0x1;
                        break;
                    case 2:
                        xx = 0x2;
                        break;
                    case 3:
                        xx = 0x3;
                        break;
                    case 4:
                        xx = 0x4;
                        break;
                    case 5:
                        xx = 0x5;
                        break;
                    case 6:
                        xx = 0x6;
                        break;
                    case 7:
                        xx = 0x7;
                        break;
                }
                for (int i = 0; i < need; i++)
                {
                    newBytes[datalength + i] = xx;
                }
            }
            else
            {
                newBytes = new byte[datalength + 8];
                for (int i = 0; i < datalength; i++)
                {
                    newBytes[i] = databytes[i];
                }
                byte xx = 0x8;
                for (int i = 0; i < 8; i++)
                {
                    newBytes[datalength + i] = xx;
                }
            }
            return newBytes;
        }
}

View Code

View Code

  1. Sha1签名
  1. Sha1签名

基于爱贝的供给,须要先将数据开始展览sha一 hash,再将hash后的数据开始展览sha一具名

基于爱贝的渴求,需求先将数据开始展览sha一 hash,再将hash后的多少实行sha1具名

lovebet爱博体育 5lovebet爱博体育 6

lovebet爱博体育 7lovebet爱博体育 8

internal sealed class Sha1SignHelper
    {
        /// <summary>
        /// 用sha1进行签名
        /// </summary>
        /// <param name="data">需要签名的数据</param>
        /// <param name="key">签名私钥</param>
        /// <returns></returns>
        internal static string Sha1Sign(string data, string key)
        {
            string signedData = null;
            IBuffer dataBuffer = Encoding.UTF8.GetBytes(data).AsBuffer();
            try
            {
                AsymmetricKeyAlgorithmProvider asymmetricAlgorithm = AsymmetricKeyAlgorithmProvider.OpenAlgorithm("RSASIGN_PKCS1_SHA1");
                //创建一个公钥私钥对
                CryptographicKey KeyPair = asymmetricAlgorithm.ImportKeyPair(CryptographicBuffer.DecodeFromBase64String(key), CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey);
                //哈希计算data
                HashAlgorithmProvider provider = HashAlgorithmProvider.OpenAlgorithm(Windows.Security.Cryptography.Core.HashAlgorithmNames.Sha1);
                IBuffer hashedData = provider.HashData(dataBuffer);
                //签名
                IBuffer signedHashedBuffer = CryptographicEngine.SignHashedData(KeyPair, hashedData);
                signedData = CryptographicBuffer.EncodeToBase64String(signedHashedBuffer);
            }
            catch (Exception ex)
            {
                DebugHelper.Log("Sha1SignHelper.Sha1Sign", ex.Message);
            }
            return signedData;
        }

        internal static bool Sha1VerifySignature(string oldText, string signature, string publicKey)
        {
            bool dataCorrect = false;
            try
            {
                AsymmetricKeyAlgorithmProvider asymmetricAlgorithm = AsymmetricKeyAlgorithmProvider.OpenAlgorithm("RSASIGN_PKCS1_SHA1");
                //创建一个公钥私钥对
                IBuffer pubKeyBuffer = Convert.FromBase64String(publicKey).AsBuffer();
                CryptographicKey KeyPair = asymmetricAlgorithm.ImportPublicKey(CryptographicBuffer.DecodeFromBase64String(publicKey));
                // 验证签名(通过公钥)
                IBuffer databuffer = CryptographicBuffer.ConvertStringToBinary(oldText, BinaryStringEncoding.Utf8); ;
                dataCorrect = CryptographicEngine.VerifySignature(KeyPair, databuffer, Convert.FromBase64String(signature).AsBuffer());
            }
            catch (Exception ex)
            {
                DebugHelper.Log("Sha1SignHelper.Sha1VerifySignature", ex.Message);
            }
            return dataCorrect;
        }
    }
internal sealed class Sha1SignHelper
    {
        /// <summary>
        /// 用sha1进行签名
        /// </summary>
        /// <param name="data">需要签名的数据</param>
        /// <param name="key">签名私钥</param>
        /// <returns></returns>
        internal static string Sha1Sign(string data, string key)
        {
            string signedData = null;
            IBuffer dataBuffer = Encoding.UTF8.GetBytes(data).AsBuffer();
            try
            {
                AsymmetricKeyAlgorithmProvider asymmetricAlgorithm = AsymmetricKeyAlgorithmProvider.OpenAlgorithm("RSASIGN_PKCS1_SHA1");
                //创建一个公钥私钥对
                CryptographicKey KeyPair = asymmetricAlgorithm.ImportKeyPair(CryptographicBuffer.DecodeFromBase64String(key), CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey);
                //哈希计算data
                HashAlgorithmProvider provider = HashAlgorithmProvider.OpenAlgorithm(Windows.Security.Cryptography.Core.HashAlgorithmNames.Sha1);
                IBuffer hashedData = provider.HashData(dataBuffer);
                //签名
                IBuffer signedHashedBuffer = CryptographicEngine.SignHashedData(KeyPair, hashedData);
                signedData = CryptographicBuffer.EncodeToBase64String(signedHashedBuffer);
            }
            catch (Exception ex)
            {
                DebugHelper.Log("Sha1SignHelper.Sha1Sign", ex.Message);
            }
            return signedData;
        }

        internal static bool Sha1VerifySignature(string oldText, string signature, string publicKey)
        {
            bool dataCorrect = false;
            try
            {
                AsymmetricKeyAlgorithmProvider asymmetricAlgorithm = AsymmetricKeyAlgorithmProvider.OpenAlgorithm("RSASIGN_PKCS1_SHA1");
                //创建一个公钥私钥对
                IBuffer pubKeyBuffer = Convert.FromBase64String(publicKey).AsBuffer();
                CryptographicKey KeyPair = asymmetricAlgorithm.ImportPublicKey(CryptographicBuffer.DecodeFromBase64String(publicKey));
                // 验证签名(通过公钥)
                IBuffer databuffer = CryptographicBuffer.ConvertStringToBinary(oldText, BinaryStringEncoding.Utf8); ;
                dataCorrect = CryptographicEngine.VerifySignature(KeyPair, databuffer, Convert.FromBase64String(signature).AsBuffer());
            }
            catch (Exception ex)
            {
                DebugHelper.Log("Sha1SignHelper.Sha1VerifySignature", ex.Message);
            }
            return dataCorrect;
        }
    }

View Code

View Code

对接过支付宝的大神们应该比较熟识,支付宝与爱贝的分歧就在于支付宝是先md5哈希,再sha壹签字,可是小编将

对接过支付宝的大神们应该相比较熟习,支付宝与爱贝的界别就在于支付宝是先md伍哈希,再sha1签署,可是笔者将

HashAlgorithmProvider provider =
HashAlgorithmProvider.OpenAlgorithm(Windows.Security.Cryptography.Core.HashAlgorithmNames.Sha1);

HashAlgorithmProvider provider =
HashAlgorithmProvider.OpenAlgorithm(Windows.Security.Cryptography.Core.HashAlgorithmNames.Sha1);

改为

改为

HashAlgorithmProvider provider =
HashAlgorithmProvider.OpenAlgorithm(Windows.Security.Cryptography.Core.HashAlgorithmNames.Md5);

HashAlgorithmProvider provider =
HashAlgorithmProvider.OpenAlgorithm(Windows.Security.Cryptography.Core.HashAlgorithmNames.Md5);

具名后获得的数据却始终与支出宝相称不上,无奈只得在服务器上到位这1环节,有知情难点所在的大神们得以指点一下。

签署后获取的数据却一向与支出宝相配不上,无奈只能在服务器上做到那1环节,有驾驭难点所在的大神们能够引导一下。

如上便是自己在做开拓时用到过的加密/具名算法,自身包裹成了类以便利用。

如上正是自个儿在做开荒时用到过的加密/具名算法,自身包装成了类以便利用。

做完这么些类型后实在作者对加密/具名算法自个儿依旧贰只雾水,并不晓得算法的实质原理,只是实现了成效而已,所以经过中也遇上了多少个难点:

做完这一个类型后实际自身对加密/具名算法本人照旧2头雾水,并不领悟算法的昆仑山真面目原理,只是完毕了职能而已,所以经过中也超越了多少个难点:

一.永恒sha1具名的私钥的格式 CryptographicKey KeyPair =
asymmetricAlgorithm.ImportKeyPair(CryptographicBuffer.DecodeFromBase64String(key),
CryptographicPrivateKeyBlobType.Pkcs壹奥迪Q7saPrivateKey)中CryptographicPrivateKeyBlobType.Pkcs一瑞鹰saPrivateKey和CryptographicPrivateKeyBlobType.Pkcs八RawPrivateKeyInfo的界别

一.永恒sha一签字的私钥的格式 CryptographicKey KeyPair =
asymmetricAlgorithm.ImportKeyPair(CryptographicBuffer.DecodeFromBase6四String(key),
CryptographicPrivateKeyBlobType.Pkcs一索罗德saPrivateKey)中CryptographicPrivateKeyBlobType.Pkcs一智跑saPrivateKey和CryptographicPrivateKeyBlobType.Pkcs捌RawPrivateKeyInfo的分别

贰.pem格式密钥和xml格式密钥怎么调换

二.pem格式密钥和xml格式密钥怎么转换

三.为何先md伍哈希再rsa签字始终和支付宝对不上?

3.怎么先md五哈希再rsa签名始终和支付宝对不上?

若是有通晓深远的大神,希望能够教导一下。

一旦有知道长远的大神,希望可以携带一下。

首先次写博客,只怕思路不明晰,望见谅!

先是次写博客,可能思路不清晰,望见谅!

 

 

相关文章