lovebet爱博体育习做UWP时涉嫌到之几栽加密签名相关。复习做UWP时提到到之几种植加密签名相关。

本身菜鸟一样枚,大学里任兴趣学了好几WP的肤浅,后来而幸运(或者不幸)的迈入了扳平家顾于Windows生态的公司举行了一段时间的UWP。在博客园刻画点自己撞的物,作为享受,也作为团结之备忘,如果起左的地方,或者可以提升B格的地方,希望园子里之大神们不吝赐教。

自家菜鸟一样朵,大学里无兴趣学了某些WP的浅,后来同时有幸(或者不幸)的前进了相同下顾于Windows生态的店召开了一段时间的UWP。在博客园勾勒点自己遇的事物,作为享受,也当作协调的备忘,如果产生荒唐的地方,或者可以升官B格的地方,希望园子里之大神们不吝赐教。

初上铺经常,公司只要做开发有关的事情,需要跟支付宝、优易付、爱贝等开支渠道对接,对新手的自家吧,加密抑或是签简直难顶异常,学校里啊用过此,OMG,只能对着头皮找材料。

初上铺经常,公司如果举行开发有关的工作,需要与支付宝、优易付、爱贝等开支渠道对接,对新手的本身来说,加密要是签简直难及深,学校里啦用了这,OMG,只能冲着头皮找材料。

这边我眷恋请教大家一个问题,就是安学习。例如当你遇上一个陌生的东西,你是什么样寻找资料解决问题的?当自家要开加密的当儿自己的做法是开辟百度搜索“WP
3des加密”,这样找出来的结果基本都非可知用。。。后来终当博客园观望了有限首有关的博客,我才理解原来要实现相关力量要哪几只类似呀几独办法,这个进程大约用了扳平宏观吧,效率超低。

此处我思请教大家一个问题,就是什么学习。例如当您遇到一个生疏的物,你是哪寻找资料解决问题之?当自家待举行加密的当儿自己的做法是打开百度搜索“WP
3des加密”,这样找出来的结果基本还非克用。。。后来竟当博客园见到了个别首相关的博客,我才知晓原来要实现相关职能要哪几只类似呀几独办法,这个过程大概用了平等健全吧,效率超低。

解决自己迫不及待的少篇博客分别是:

化解我急切的个别篇博客分别是:

王磊: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

扣押了当下半首博客就理解该以什么类了,再连接下去就相对简便易行了。非常感谢两各先生的享受和点。

关押了马上有限篇博客就亮该以什么类了,再搭下便相对简便易行了。非常感谢两各教师的享用同指导。

当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签名

因爱贝的渴求,需要先拿数据开展sha1 hash,再将hash后底数量进行sha1签署

依据爱贝的求,需要先拿数据进行sha1 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哈希,再sha1署,可是我用

本着连片了支付宝的大神们应该比熟悉,支付宝和爱贝的区别就在支付宝是先md5哈希,再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.世代sha1署名的私钥的格式 CryptographicKey KeyPair =
asymmetricAlgorithm.ImportKeyPair(CryptographicBuffer.DecodeFromBase64String(key),
CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey)中CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey和CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo的别

1.千古sha1签字的私钥的格式 CryptographicKey KeyPair =
asymmetricAlgorithm.ImportKeyPair(CryptographicBuffer.DecodeFromBase64String(key),
CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey)中CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey和CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo的分别

2.pem格式密钥和xml格式密钥怎么转移

2.pem格式密钥和xml格式密钥怎么变换

3.为何先md5哈希再rsa签名始终同支付宝对不达到?

3.胡先md5哈希再rsa签名始终和支付宝对匪上?

设来明深入的大神,希望可以指导一下。

苟发生了解深入的大神,希望得以指导一下。

第一蹩脚写博客,可能思路不清晰,望见谅!

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

 

 

相关文章