예제 #1
0
        public string DecryptData(byte[] data)
        {
            var asnObj = Asn1Der.Parse(data);

            return(Regex.Replace(TripleDesHelper.DescbcDecryptor(_privateKey,
                                                                 asnObj.Objects[0].Objects[1].Objects[1].Data, asnObj.Objects[0].Objects[2].Data),
                                 @"[^\u0020-\u007F]",
                                 ""));
        }
예제 #2
0
        public void EncryptTripleDesTest()
        {
            var key    = CryptoHelper.GenerateKey("*****@*****.**", 16); //16 24
            var strKey = Convert.ToBase64String(key);

            var cipher = TripleDesHelper.Encrypt("cszfp.com", strKey);
            var clear  = TripleDesHelper.Decrypt(cipher, strKey);

            Assert.AreEqual("cszfp.com", clear);
        }
예제 #3
0
        public void EncrytpTripleDesTest2()
        {
            var key = CryptoHelper.GenerateKey("*****@*****.**", 24);
            var iv  = CryptoHelper.GenerateIv(8);

            var cipher = TripleDesHelper.Encrypt("cszfp.com", key, iv);

            var clear = TripleDesHelper.Decrypt(cipher, key, iv);

            Assert.AreEqual("cszfp.com", clear);
        }
예제 #4
0
        public void TripleDesTest()
        {
            var str      = "123123";
            var password = "******";
            var iv       = "12345678";

            password.Length.ShouldBe(24);
            _testOutputHelper.WriteLine(password);

            var encoded = TripleDesHelper.Encrypt(str, password, iv: iv);

            _testOutputHelper.WriteLine(encoded);
            var decoded = TripleDesHelper.Decrypt(encoded, password, iv: iv);

            decoded.ShouldBe(str);
        }
예제 #5
0
        public void test()
        {
            // var api = "http://api.test.yusuinet.com/openapi/test";
            // appId
            var appId = "12ae01c0d6fc4d8aa34dab379a3f5dcb";
            // 密钥
            var secret = "ed37a865b0484a01a91f653b";
            // 计算当前时间戳
            var timestamp = "1600504614";
            // 使用guid生成32位随机字符
            var nonce = "742b0467aa364ff491d163d400a17209";
            // 使用guid生成32位请求id
            // var requestId = "e28ccc72720c4b599ef84ac7100d96fd";
            // 创建接口调用对象
            var obj = new
            {
                appId,
                timestamp,
                nonce,
                data = new
                {
                    year  = 2020,
                    momth = 1
                }
            };

            var json = JsonSerializer.Serialize(obj);

            _testOutputHelper.WriteLine(json);
            _testOutputHelper.WriteLine("");

            // 使用3des加密,ECB+PKCS7
            var encrypted = TripleDesHelper.Encrypt(json, secret, CipherMode.ECB, PaddingMode.PKCS7);

            _testOutputHelper.WriteLine(encrypted);
            _testOutputHelper.WriteLine("");

            // HmacSha256 hash
            var hash = HashHelper.HMACSHA256(json, secret);

            _testOutputHelper.WriteLine(hash);
        }
예제 #6
0
 /// <summary>
 /// Encrypt Data by tripleDes
 /// </summary>
 /// <param name="plainText"></param>
 /// <param name="key"></param>
 /// <param name="IV"></param>
 /// <returns></returns>
 public static string EncryptString3Des(string plainText, string keyStr, string IVStr)
 {
     return(TripleDesHelper.EncryptString3Des(plainText, keyStr, IVStr));
 }
예제 #7
0
 public static string DecryptString3Des(string encryptedStr, string keyStr, string IVStr)
 {
     return(TripleDesHelper.DecryptString3Des(encryptedStr, keyStr, IVStr));
 }
예제 #8
0
        public bool Initialize(string profilePath)
        {
            BerkeleyDb    db       = new BerkeleyDb(Path.Combine(profilePath, "key3.db"));
            PasswordCheck pwdCheck = new PasswordCheck(db.Keys.Where(p => p.Key.Equals("password-check"))
                                                       .Select(p => p.Value).First().Replace("-", ""));

            string globalSalt = db.Keys.Where(p => p.Key.Equals("global-salt"))
                                .Select(p => p.Value)
                                .First()
                                .Replace("-", "");

            var globalSaltBytes   = StringExtensions.HexToBytes(globalSalt);
            var pwdCheckSaltBytes = StringExtensions.HexToBytes(pwdCheck.EntrySalt);

            var masterPassword = "";

            bool CheckPassword(string password)
            {
                MozillaPBE checkPwd = new MozillaPBE(globalSaltBytes, Encoding.ASCII.GetBytes(password),
                                                     pwdCheckSaltBytes);

                checkPwd.Compute();
                string decryptedPwdChk = TripleDesHelper.DescbcDecryptor(checkPwd.Key, checkPwd.IV,
                                                                         StringExtensions.HexToBytes(pwdCheck.Passwordcheck));

                return(decryptedPwdChk.StartsWith("password-check"));
            }

            if (!CheckPassword(masterPassword))
            {
                var foundPassword = false;
                if (PasswordList?.Length > 0)
                {
                    foreach (var password in PasswordList)
                    {
                        masterPassword = password;
                        foundPassword  = CheckPassword(password);
                        if (foundPassword)
                        {
                            break;
                        }
                    }
                }

                if (!foundPassword)
                {
                    return(false);
                }
            }

            //private key
            string f81 = db.Keys.Where(p => !p.Key.Equals("global-salt") &&
                                       !p.Key.Equals("Version") &&
                                       !p.Key.Equals("password-check"))
                         .Select(p => p.Value)
                         .First()
                         .Replace("-", "");

            Asn1DerObject f800001 = Asn1Der.Parse(StringExtensions.HexToBytes(f81));

            MozillaPBE checkPrivateKey = new MozillaPBE(StringExtensions.HexToBytes(globalSalt),
                                                        Encoding.ASCII.GetBytes(masterPassword), f800001.Objects[0].Objects[0].Objects[1].Objects[0].Data);

            checkPrivateKey.Compute();

            byte[] decryptF800001 = TripleDesHelper.DescbcDecryptorByte(checkPrivateKey.Key, checkPrivateKey.IV,
                                                                        f800001.Objects[0].Objects[1].Data);

            Asn1DerObject f800001deriv1 = Asn1Der.Parse(decryptF800001);
            Asn1DerObject f800001deriv2 = Asn1Der.Parse(f800001deriv1.Objects[0].Objects[2].Data);

            if (f800001deriv2.Objects[0].Objects[3].Data.Length > 24)
            {
                _privateKey = new byte[24];
                Array.Copy(f800001deriv2.Objects[0].Objects[3].Data,
                           f800001deriv2.Objects[0].Objects[3].Data.Length - 24, _privateKey, 0, 24);
            }
            else
            {
                _privateKey = f800001deriv2.Objects[0].Objects[3].Data;
            }

            return(true);
        }