public async Task RestChannelSamples()
        {
            AblyRest rest    = new AblyRest("{{API_KEY}}");
            var      channel = rest.Channels.Get("{{RANDOM_CHANNEL_NAME}}");
            await channel.PublishAsync("example", "message data");

            PaginatedResult <Message> resultPage = await channel.HistoryAsync();

            Console.WriteLine("Last published message ID: " + resultPage.Items[0].Id);

            byte[]         key              = null;
            CipherParams   cipherParams     = Crypto.GetDefaultParams(key);
            ChannelOptions options          = new ChannelOptions(cipherParams);
            var            encryptedChannel = rest.Channels.Get("channelName", options);
        }
示例#2
0
        public async Task PublishShouldNotAlterChannelOptions()
        {
            var key          = Convert.FromBase64String("dDGE8dYl8M9+uyUTIv0+ncs1hEa++HiNDu75Dyj4kmw=");
            var cipherParams = new CipherParams(key);
            var options      = new ChannelOptions(cipherParams); // enable encryption
            var client       = await GetConnectedClient();

            var channel = client.Channels.Get("test", options);

            var channel2 = client.Channels.Get("test");

            channel.Publish(new Message(null, "This is a test", Guid.NewGuid().ToString()));

            await client.ProcessCommands();

            Assert.Equal(options.ToJson(), channel2.Options.ToJson());
            options.CipherParams.Equals(cipherParams).Should().BeTrue();
        }
        internal static KeyStoreV3 <Pbkdf2Params> EncryptKey(PrivateKey key, string password, Pbkdf2Params kdfParams)
        {
            if (ReferenceEquals(key, null))
            {
                throw new KdfException(ErrorCode.BAD_ARGUMENT, "empty key");
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new KdfException(ErrorCode.BAD_ARGUMENT, "empty password");
            }

            // unsupported prf
            if (kdfParams.prf != Pbkdf2Params.HMACSHA256)
            {
                throw new KdfException(ErrorCode.UNSUPPORTED, $"unsupported kdfparams.prf:{kdfParams.prf}");
            }

            // random values ( salt, iv )
            var salt         = kdfParams.salt;
            var cipherParams = new CipherParams();

            // derivedKey -> cipherKey -> cipherText -> mac
            var derivedKey = PbkdfCrypt.GeneratePbkdf2Sha256DerivedKey(password, salt.HexToBytes(), kdfParams.c, kdfParams.dklen);
            var cipherKey  = PbkdfCrypt.GenerateCipherKey(derivedKey);
            var cipherText = PbkdfCrypt.GenerateAesCtrCipher(cipherParams.iv.HexToBytes(), cipherKey, key.Bytes);
            var mac        = PbkdfCrypt.GenerateMac(derivedKey, cipherText);

            return(new KeyStoreV3 <Pbkdf2Params>()
            {
                version = Version,
                id = Guid.NewGuid().ToString(),
                address = key.Address.HexAddress.ToLower(),
                crypto =
                {
                    ciphertext   = cipherText.ToHex(),
                    cipherparams = cipherParams,
                    cipher       = CIPHER,
                    kdf          = KdfType.pbkdf2.ToString(),
                    kdfparams    = kdfParams,
                    mac          = mac.ToHex()
                }
            });
        }
示例#4
0
        private static KeyStoreV3 <Pbkdf2Params> EncryptKey(byte[] key, string address, string password, Pbkdf2Params kdfParams)
        {
            if (key.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (password.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(password));
            }
            // unsupported prf
            if (kdfParams.prf != Pbkdf2Params.HMACSHA256)
            {
                throw new ArgumentException("unsupported kdfparams.prf");
            }

            // random values ( salt, iv )
            var salt         = kdfParams.salt;
            var cipherParams = new CipherParams();

            // derivedKey -> cipherKey -> cipherText -> mac
            var derivedKey = PbkdfCrypt.GeneratePbkdf2Sha256DerivedKey(password, salt.ToByteArray(), kdfParams.c, kdfParams.dklen);
            var cipherKey  = PbkdfCrypt.GenerateCipherKey(derivedKey);
            var cipherText = PbkdfCrypt.GenerateAesCtrCipher(cipherParams.iv.ToByteArray(), cipherKey, key);
            var mac        = PbkdfCrypt.GenerateMac(derivedKey, cipherText);

            return(new KeyStoreV3 <Pbkdf2Params>()
            {
                version = Version,
                id = Guid.NewGuid().ToString(),
                address = address,
                crypto =
                {
                    ciphertext   = cipherText.ToHexString(),
                    cipherparams = cipherParams,
                    cipher       = CIPHER,
                    kdf          = KdfType.pbkdf2.ToString(),
                    kdfparams    = kdfParams,
                    mac          = mac.ToHexString()
                }
            });
        }
示例#5
0
        public string EncryptPrivateKey(string privateKey, string passphrase, KDFType type)
        {
            string address = KeyTools.GetAddressFromPrivateKey(privateKey);

            byte[] iv   = KeyTools.GenerateRandomBytes(16);
            byte[] salt = KeyTools.GenerateRandomBytes(32);
            byte[] derivedKey;
            if (type == KDFType.PBKDF2)
            {
                PBKDF2Params pbkdf2Params = new PBKDF2Params();

                pbkdf2Params.Salt  = ByteUtil.ByteArrayToHexString(salt);
                pbkdf2Params.DkLen = 32;
                pbkdf2Params.Count = 262144;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), pbkdf2Params);
            }
            else
            {
                ScryptParams scryptParams = new ScryptParams();

                scryptParams.Salt = ByteUtil.ByteArrayToHexString(salt);

                scryptParams.DkLen = 32;
                scryptParams.P     = 1;
                scryptParams.R     = 8;
                scryptParams.N     = 8192;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), scryptParams);
            }

            byte[] encryptKey = new byte[16];
            Array.Copy(derivedKey, encryptKey, 16);

            KeyStoreCrypto cry = new KeyStoreCrypto();

            byte[] ciphertext = cry.GenerateAesCtrCipher(iv, encryptKey, ByteUtil.HexStringToByteArray(privateKey));
            byte[] mac        = HashUtil.GenerateMac(derivedKey, ciphertext);

            //build struct
            CipherParams cipherParams = new CipherParams();

            cipherParams.Iv = ByteUtil.ByteArrayToHexString(iv);

            Kdfparams kp     = new Kdfparams(ByteUtil.ToSbyte(salt));
            Crypto    crypto = new Crypto();

            crypto.Cipher       = "aes-128-ctr";
            crypto.Cipherparams = cipherParams;
            crypto.Ciphertext   = ByteUtil.ByteArrayToHexString(ciphertext);
            crypto.Kdf          = (type == KDFType.PBKDF2 ? "pbkdf2" : "scrypt");
            crypto.Kdfparams    = kp;
            crypto.Mac          = ByteUtil.ByteArrayToHexString(mac);

            KeystoreV3 key = new KeystoreV3();

            key.Address = address;
            key.Crypto  = crypto;
            key.Id      = Guid.NewGuid().ToString();
            key.Version = 3;

            return(JsonConvert.SerializeObject(key));
        }
示例#6
0
 /// <summary>Create a new instance of AesCipther.</summary>
 /// <param name="params">Cipher params used to configure the RinjaelManaged algorithm</param>
 public AesCipher(CipherParams @params)
 {
     _params = @params;
 }
示例#7
0
        public string EncryptPrivateKey(string privateKey, string passphrase, KDFType type)
        {
            string address = KeyTools.GetAddressFromPrivateKey(privateKey);

            byte[] iv   = KeyTools.GenerateRandomBytes(16);
            byte[] salt = KeyTools.GenerateRandomBytes(32);
            byte[] derivedKey;
            if (type == KDFType.PBKDF2)
            {
                PBKDF2Params pbkdf2Params = new PBKDF2Params();

                pbkdf2Params.Salt  = ByteUtil.ByteArrayToHexString(salt);
                pbkdf2Params.DkLen = 32;
                pbkdf2Params.Count = 262144;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), pbkdf2Params);
            }
            else
            {
                ScryptParams scryptParams = new ScryptParams();

                scryptParams.Salt = ByteUtil.ByteArrayToHexString(salt);

                scryptParams.DkLen = 32;
                scryptParams.P     = 1;
                scryptParams.R     = 8;
                scryptParams.N     = 8192;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), scryptParams);
            }

            byte[] encryptKey = new byte[16];
            Array.Copy(derivedKey, encryptKey, 16);


            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key     = encryptKey,
                Mode    = CipherMode.CBC,
                Padding = System.Security.Cryptography.PaddingMode.None
            };
            //TODO 加密方法待完善

            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
            byte[] ciphertext = cTransform.TransformFinalBlock(ByteUtil.HexStringToByteArray(privateKey), 0, ByteUtil.HexStringToByteArray(privateKey).Length);

            byte[] mac = HashUtil.GenerateMac(derivedKey, ciphertext);

            //build struct
            CipherParams cipherParams = new CipherParams();

            cipherParams.Iv = ByteUtil.ByteArrayToHexString(iv);

            Kdfparams kp     = new Kdfparams(salt);
            Crypto    crypto = new Crypto();

            crypto.Cipher       = "aes-128-ctr";
            crypto.Cipherparams = cipherParams;
            crypto.Ciphertext   = ByteUtil.ByteArrayToHexString(ciphertext);
            crypto.Kdf          = (type == KDFType.PBKDF2 ? "pbkdf2" : "scrypt");
            crypto.Kdfparams    = kp;
            crypto.Mac          = ByteUtil.ByteArrayToHexString(mac);

            KeystoreV3 key = new KeystoreV3();

            key.Address = address;
            key.Crypto  = crypto;
            key.Id      = Guid.NewGuid().ToString();
            key.Version = 3;

            return(JsonConvert.SerializeObject(key));
        }
示例#8
0
 public ChannelOptions(CipherParams @params)
 {
     Encrypted    = true;
     CipherParams = @params;
 }
示例#9
0
        public string EncryptPrivateKey(string privateKey, string passphrase, KDFType type)
        {
            string address = CryptoUtil.GetAddressFromPrivateKey(privateKey);

            byte[] iv   = CryptoUtil.GenerateRandomBytes(16);
            byte[] salt = CryptoUtil.GenerateRandomBytes(32);
            byte[] derivedKey;
            if (type == KDFType.PBKDF2)
            {
                PBKDF2Params pbkdf2Params = new PBKDF2Params();

                pbkdf2Params.Salt  = ByteUtil.ByteArrayToHexString(salt);
                pbkdf2Params.DkLen = 32;
                pbkdf2Params.Count = 262144;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), pbkdf2Params);
            }
            else
            {
                var scryptParams = new KDFParams();

                scryptParams.Salt = ByteUtil.ByteArrayToHexString(salt);

                scryptParams.dklen = 32;
                scryptParams.p     = 1;
                scryptParams.r     = 8;
                scryptParams.n     = 8192;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), scryptParams);
            }

            byte[] encryptKey = new byte[16];
            Array.Copy(derivedKey, encryptKey, 16);

            KeyStore cry = new KeyStore();

            byte[] ciphertext = cry.GenerateAesCtrCipher(iv, encryptKey, ByteUtil.HexStringToByteArray(privateKey));
            //build struct
            CipherParams cipherParams = new CipherParams();

            cipherParams.Iv = ByteUtil.ByteArrayToHexString(iv);

            var kp = new KDFParams()
            {
                Salt = Encoding.UTF8.GetString(salt)
            };
            var crypto = new MusCipher();

            crypto.Cipher       = "aes-128-ctr";
            crypto.Cipherparams = cipherParams;
            crypto.Ciphertext   = ByteUtil.ByteArrayToHexString(ciphertext);
            crypto.Kdf          = (type == KDFType.PBKDF2 ? "pbkdf2" : "scrypt");
            crypto.KdfParams    = kp;
            crypto.Mac          = Encoding.UTF8.GetString(HashUtil.GenerateMac(derivedKey, ciphertext));

            KeyStore key = new KeyStore();

            key.Address = "0x" + address;
            key.Crypto  = crypto;
            key.Id      = Guid.NewGuid().ToString();
            key.Version = 3;

            return(JsonConvert.SerializeObject(key));
        }