Пример #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>
        /// 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));
        }
Пример #3
0
        /// <summary>
        /// Signs the passed in data with a private key
        /// </summary>
        /// <param name="privateKey">the private key used to create the signature</param>
        /// <param name="data">The data to sign</param>
        /// <returns>the signature as a byte array</returns>
        public byte[] Sign(byte[] privateKey, byte[] data)
        {
            var signer = new Ed448Signer(ByteConvert.StringToAsciiBytes("context"));
            Ed448PrivateKeyParameters privKey = null;

            try
            {
                privKey = (Ed448PrivateKeyParameters)CreateAsymmetricKeyParameterFromPrivateKeyInfo(privateKey);
            }
            catch (InvalidCastException exception)
            {
                string message = "Private Key Import Failed!\n" +
                                 $"{exception.Message}.\n" +
                                 "The contents of the source do not represent a valid Ed448 key parameter\n" +
                                 "Verify that the key is not corrupted.\n" +
                                 "- or - Verify that the correct key is selected.";
                throw new CryptoException(message, exception);
            }
            signer.Init(true, privKey);
            signer.BlockUpdate(data, 0, data.Length);
            var signature = signer.GenerateSignature();

            return(signature);
        }
Пример #4
0
        /// <summary>
        /// Verifies a signature to be authentic
        /// </summary>
        /// <param name="originalSignature">The signature which is be verified</param>
        /// <param name="publicKey">the public key used for the verification</param>
        /// <param name="data">the data which is signed</param>
        /// <returns>true if signature is authentic, false if not</returns>
        public bool Verify(byte[] originalSignature, byte[] publicKey, byte[] data)
        {
            Ed448PublicKeyParameters pubKey = null;

            try
            {
                pubKey = (Ed448PublicKeyParameters)CreateAsymmetricKeyParameterFromPublicKeyInfo(publicKey);
            }
            catch (InvalidCastException exception)
            {
                string message = "Public Key Import Failed!\n" +
                                 $"{exception.Message}.\n" +
                                 "The contents of the source do not represent a valid Ed448 key parameter\n" +
                                 "Verify that the key is not corrupted.\n" +
                                 "- or - Verify that the correct key is selected.";
                throw new CryptoException(message, exception);
            }

            var signer = new Ed448Signer(ByteConvert.StringToAsciiBytes("context"));

            signer.Init(false, pubKey);
            signer.BlockUpdate(data, 0, data.Length);
            return(signer.VerifySignature(originalSignature));
        }
Пример #5
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.StringToAsciiBytes(plainText);

            return(EncryptBytes(publicKey, plain));
        }
Пример #6
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.BytesToAsciiString(decrypted));
        }