Ejemplo n.º 1
0
        private static byte[] DecryptKey(KeyStoreV3 <Pbkdf2Params> keystore, string password)
        {
            if (ReferenceEquals(keystore, null))
            {
                throw new ArgumentNullException(nameof(keystore));
            }
            if (password.IsNullOrEmpty())
            {
                throw new ArgumentNullException(nameof(password));
            }

            var crypto    = keystore.crypto;
            var kdfparams = crypto.kdfparams;

            // unsupported cipher
            if (crypto.cipher != CIPHER)
            {
                throw new ArgumentException("unsupported cipher");
            }

            // unsupported prf
            if (kdfparams.prf != Pbkdf2Params.HMACSHA256)
            {
                throw new ArgumentException("unsupported kdfparams.prf");
            }

            return(PbkdfCrypt.DecryptPbkdf2Sha256(password, Hex.ToByteArray(crypto.mac),
                                                  HexToByteArray(crypto.cipherparams.iv),
                                                  HexToByteArray(crypto.ciphertext),
                                                  kdfparams.c, kdfparams.salt.ToByteArray(), kdfparams.dklen));
        }
        internal static PrivateKey DecryptKey(KeyStoreV3 <Pbkdf2Params> keyStore, string password)
        {
            if (string.IsNullOrEmpty(password))
            {
                throw new KdfException(ErrorCode.BAD_ARGUMENT, "empty password");
            }
            if (ReferenceEquals(keyStore, null))
            {
                throw new KdfException(ErrorCode.BAD_ARGUMENT, "keyStore is null");
            }

            var crypto    = keyStore.crypto;
            var kdfparams = keyStore.crypto.kdfparams;

            // unsupported cipher
            if (crypto.cipher != CIPHER)
            {
                throw new KdfException(ErrorCode.UNSUPPORTED, $"unsupported cipher:{crypto.cipher}");
            }

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

            var key = PbkdfCrypt.DecryptPbkdf2Sha256(password, crypto.mac.HexToBytes(),
                                                     crypto.cipherparams.iv.HexToBytes(),
                                                     crypto.ciphertext.HexToBytes(),
                                                     kdfparams.c, kdfparams.salt.HexToBytes(), kdfparams.dklen);

            return(new PrivateKey(key));
        }
        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()
                }
            });
        }
Ejemplo n.º 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()
                }
            });
        }