예제 #1
0
        /// <summary>
        /// Method for decrypting to 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="encrypted">the encrypted bytes</param>
        /// <returns>decrypted text</returns>
        public string DecryptToText(int selectedAlgorithim, int keySize, byte[] secretKey, byte[] iv, byte[] encrypted)
        {
            var algorithim = ((SymmetricBouncyCastleCipher)selectedAlgorithim).ToString().Replace("_", "-");;

            bufferedCipher = CipherUtilities.GetCipher(algorithim);
            if (GetIvSize(selectedAlgorithim) > 0)
            {
                var kp  = new KeyParameter(secretKey);
                var ivp = new ParametersWithIV(kp, iv);
                bufferedCipher.Init(false, ivp);
            }
            else
            {
                bufferedCipher.Init(false, new KeyParameter(secretKey));
            }

            byte[] decrypted = null;
            try
            {
                decrypted = bufferedCipher.DoFinal(encrypted);
            }
            catch (CryptoException exception)
            {
                if (exception.Message == "pad block corrupted")
                {
                    string message = "Decryption failed!\n" +
                                     "The secret key is corrupted.\n" +
                                     "Verify that the same key is used for encrypting and decrypting.";
                    throw new CryptoException(message, exception);
                }
                else if (exception.Message == "last block incomplete in decryption")
                {
                    string message = "Decryption failed!\n" +
                                     "The encryption block length is not complete\n" +
                                     "Verify that the encryption bit length is a multiple of the expected blocksize\n" +
                                     $"Blocksize bit length: {bufferedCipher.GetBlockSize() * 8}\n" +
                                     $"Encryption bit length: {encrypted.Length * 8}";
                    throw new CryptoException(message, exception);
                }
                else
                {
                    throw new CryptoException("Contact developer for help.", exception);
                }
            }
            return(ByteConvert.BytesToUTF8String(decrypted));
        }
예제 #2
0
        /// <summary>
        /// Decrypts the encrypted byte array to a plain text
        /// </summary>
        /// <param name="privateKey">the private key used for decryption</param>
        /// <param name="encrypted">the encrypted bytes</param>
        /// <returns>the decrypted plain text</returns>
        public string DecryptToText(byte[] privateKey, byte[] encrypted)
        {
            var decrypted = DecryptToBytes(privateKey, encrypted);

            return(ByteConvert.BytesToUTF8String(decrypted));
        }