Ejemplo n.º 1
0
        /// <summary>
        /// Method for encrypting plain text
        /// </summary>
        /// <param name="selectedAlgorithim">the algorithim to use, will be cast to enum</param>
        /// <param name="keySize">the key size to use</param>
        /// <param name="secretKey">the secret key for the algorithim</param>
        /// <param name="plainText">the plain text to encrypt</param>
        /// <returns>the encrypted bytes</returns>
        public byte[] EncryptText(int selectedAlgorithim, int keySize, byte[] secretKey, byte[] iv, string plainText)
        {
            var algorithim = ((SymmetricBouncyCastleCipher)selectedAlgorithim).ToString().Replace("_", "-");;

            bufferedCipher = CipherUtilities.GetCipher(algorithim);
            var plain = ByteConvert.StringToUTF8Bytes(plainText);

            if (GetIvSize(selectedAlgorithim) > 0)
            {
                var kp  = new KeyParameter(secretKey);
                var ivp = new ParametersWithIV(kp, iv);
                bufferedCipher.Init(true, ivp);
            }
            else
            {
                bufferedCipher.Init(true, new KeyParameter(secretKey));
            }
            return(bufferedCipher.DoFinal(plain));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Encrypts a plain text using a public key
        /// </summary>
        /// <param name="publicKey">the key used for encryption</param>
        /// <param name="plainText">the plain text to encrypt</param>
        /// <returns>encrypted bytes</returns>
        public byte[] EncryptText(byte[] publicKey, string plainText)
        {
            var plain = ByteConvert.StringToUTF8Bytes(plainText);

            return(EncryptBytes(publicKey, plain));
        }