Beispiel #1
0
        /// <summary>
        /// 文件加密
        /// </summary>
        /// <param name="pubKey">椭圆曲线公钥</param>
        /// <param name="filePath">文件路径</param>
        /// <returns>文件加密后的文件名</returns>
        public string EncryptFile(ECPublicKeyParameters pubKey, string filePath)
        {
            byte[] cleartext = File.ReadAllBytes(filePath);

            AsymmetricCipherKeyPair Rand = KeyUtils.GenerateKeyPair();

            byte[] ciphertext = engine.Encrypt(
                (ECPublicKeyParameters)pubKey,
                (ECPrivateKeyParameters)Rand.Private,
                cleartext
                );

            using (FileStream fs = File.OpenWrite(filePath))
            {
                byte[] hexPubKey = KeyUtils.EncodeECPublicKey((ECPublicKeyParameters)Rand.Public);
                Debug.Assert(
                    hexPubKey.Length == 33,
                    "Encoded EC public key must be in hex compressed format"
                    );

                fs.Write(hexPubKey, 0, hexPubKey.Length);
                fs.Write(ciphertext, 0, ciphertext.Length);
            }

            string lockedPath = filePath + lockSuffix;

            File.Move(filePath, lockedPath);

            return(lockedPath);
        }