Example #1
0
        public string DecryptPrivateKey(string encryptJson, string passphrase)
        {
            KeystoreV3 keystoreV3 = JsonConvert.DeserializeObject <KeystoreV3>(encryptJson);

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

            byte[] derivedKey;
            if (kdf == "pbkdf2")
            {
                PBKDF2Params pbkdf2Params = new PBKDF2Params();
                pbkdf2Params.Salt  = ByteUtil.ByteArrayToHexString(kp.Salt);
                pbkdf2Params.DkLen = 32;
                pbkdf2Params.Count = 262144;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), pbkdf2Params);
            }
            else
            {
                ScryptParams scryptParams = new ScryptParams();
                scryptParams.Salt  = ByteUtil.ByteArrayToHexString(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() != keystoreV3.Crypto.Mac)
            {
                throw new Exception("Failed to decrypt.");
            }

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

            //TODO 加密方法待完善
            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key     = encryptKey,
                Mode    = CipherMode.CBC,
                Padding = System.Security.Cryptography.PaddingMode.None
            };
            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
            byte[] ciphertextByte = cTransform.TransformFinalBlock(ciphertext, 0, ciphertext.Length);

            return(ByteUtil.ByteArrayToHexString(ciphertextByte));
        }
Example #2
0
        public string DecryptPrivateKey(string encryptJson, string passphrase)
        {
            KeystoreV3 keystoreV3 = JsonConvert.DeserializeObject <KeystoreV3>(encryptJson);

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

            byte[] derivedKey;
            if (kdf == "pbkdf2")
            {
                PBKDF2Params pbkdf2Params = new PBKDF2Params();
                pbkdf2Params.Salt  = ByteUtil.ByteArrayToHexString(kp.Salt);
                pbkdf2Params.DkLen = 32;
                pbkdf2Params.Count = 262144;
                derivedKey         = GetDerivedKey(Encoding.Default.GetBytes(passphrase), pbkdf2Params);
            }
            else
            {
                ScryptParams scryptParams = new ScryptParams();
                scryptParams.Salt  = ByteUtil.ByteArrayToHexString(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() != keystoreV3.Crypto.Mac)
            {
                throw new Exception("Failed to decrypt.");
            }

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

            KeyStoreCrypto cry = new KeyStoreCrypto();

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

            return(ByteUtil.ByteArrayToHexString(ciphertextByte));
        }
Example #3
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));
        }
Example #4
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));
        }