Exemplo n.º 1
0
 public byte[] GetDerivedKey(byte[] password, KDFParams parameters)
 {
     if (parameters is Pbkdf2)
     {
         var pbkdf2Params = (Pbkdf2)parameters;
         return(pbkdf2Params.GetDerivedKey(password));
     }
     else if (parameters is KDFParams)
     {
         return(GenerateDerivedScryptKey(Encoding.UTF8.GetString(password)));
     }
     else
     {
         throw new Exception("unsupport kdf params");
     }
 }
Exemplo n.º 2
0
 public byte[] GetDerivedKey(byte[] password, KDFParams paras)
 {
     if (paras is PBKDF2Params)
     {
         PBKDF2Params pbkdf2Params = (PBKDF2Params)paras;
         return(pbkdf2Wrapper.GetDerivedKey(password, ByteUtil.HexStringToByteArray(pbkdf2Params.Salt), pbkdf2Params.Count, pbkdf2Params.DkLen));
     }
     else if (paras is ScryptParams)
     {
         ScryptParams scryptParams = (ScryptParams)paras;
         return(scryptWrapper.GetDerivedKey(password, ByteUtil.HexStringToByteArray(scryptParams.Salt), scryptParams.N, scryptParams.R, scryptParams.P, scryptParams.DkLen));
     }
     else
     {
         throw new Exception("unsupport kdf params");
     }
 }
Exemplo n.º 3
0
        public string DecryptPrivateKey(string encryptJson, string passphrase)
        {
            KeyStore keystore = JsonConvert.DeserializeObject <KeyStore>(encryptJson);

            byte[]    ciphertext = ByteUtil.HexStringToByteArray(keystore.Crypto.Ciphertext);
            byte[]    iv         = ByteUtil.HexStringToByteArray(keystore.Crypto.Cipherparams.Iv);
            KDFParams kp         = keystore.Crypto.KdfParams;
            string    kdf        = keystore.Crypto.Kdf;

            byte[] derivedKey;
            if (kdf == "pbkdf2")
            {
                PBKDF2Params pbkdf2Params = new PBKDF2Params();
                pbkdf2Params.Salt  = ByteUtil.ByteArrayToHexString(Encoding.UTF8.GetBytes(kp.Salt));
                pbkdf2Params.DkLen = 32;
                pbkdf2Params.Count = 262144;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), pbkdf2Params);
            }
            else
            {
                KDFParams scryptParams = new KDFParams();
                scryptParams.Salt  = ByteUtil.ByteArrayToHexString(Encoding.UTF8.GetBytes(kp.Salt));
                scryptParams.dklen = 32;
                scryptParams.p     = 1;
                scryptParams.r     = 8;
                scryptParams.n     = 8192;

                derivedKey = GetDerivedKey(Encoding.Default.GetBytes(passphrase), scryptParams);
            }
            string mac = ByteUtil.ByteArrayToHexString(HashUtil.GenerateMac(derivedKey, ciphertext));

            if (mac.ToUpper() != keystore.Crypto.Mac)
            {
                throw new Exception("Failed to decrypt.");
            }

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

            byte[] ciphertextByte = GenerateAesCtrCipher(iv, encryptKey, ciphertext);

            return(ByteUtil.ByteArrayToHexString(ciphertextByte));
        }
Exemplo n.º 4
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));
        }