virtual public void SetCryptoMode(EncryptAlg revision, Int32 keyLength)
        {
            //encryptMetadata = DO_NOT_ENCRYPT_METADATA;
            //embeddedFilesOnly = EMBEDDED_FILES_ONLY;

            _encryptMetadata = true;
            _alg             = revision;
            switch (_alg)
            {
            case EncryptAlg.STANDARD_ENCRYPTION_40:
                _keyLength = 40;
                break;

            case EncryptAlg.STANDARD_ENCRYPTION_128:
                if (keyLength > 0)
                {
                    _keyLength = keyLength;
                }
                else
                {
                    _keyLength = 128;
                }
                break;

            case EncryptAlg.AES_128:
            case EncryptAlg.AES_256:
                throw new NotImplementedException("AES yet not impletmented");

            default:
                throw new ArgumentException("No valid encryption.mode");
            }
        }
예제 #2
0
        void CreateEncrypt(PdfElement elem)
        {
            var cf = elem.Get <PdfElement>("CF");

            if (cf != null)
            {
                var stdCf = cf.Get <PdfElement>("StdCF");
                if (stdCf != null)
                {
                }
            }
            var pValue = elem.Get <PdfInteger>("P").Value;
            var vValue = elem.Get <PdfInteger>("V").Value;
            var rValue = elem.Get <PdfInteger>("R").Value;
            var oValue = elem.Get <PdfString>("O").ToISOBytes();
            var uValue = elem.Get <PdfString>("U").ToISOBytes();
            var filter = elem.Get <PdfName>("Filter")?.Name;

            EncryptAlg cryptoMode = EncryptAlg.STANDARD_ENCRYPTION_128;

            switch (rValue)
            {
            case 4:
                if (vValue != 4)
                {
                    throw new PdfException($"Invalid encrypt. V={vValue}, P={vValue}");
                }
                var cfObj    = elem.Get <PdfDictionary>("CF");
                var stdCfObj = cfObj.Get <PdfDictionary>("StdCF");
                if (stdCfObj != null)
                {
                    if (stdCfObj.Get <PdfName>("CFM")?.Name == "V2")
                    {
                        cryptoMode = EncryptAlg.STANDARD_ENCRYPTION_128;
                    }
                }
                break;

            default:
                throw new NotImplementedException($"Decrypt value {rValue}");
            }

            _decryptor = new PdfEncryption();
            _decryptor.SetCryptoMode(cryptoMode, 0);

            if (filter == "Standard")
            {
                if (rValue != 5)
                {
                    _decryptor.SetupByOwnerPassword(_documentId, uValue, oValue, pValue);
                    Int32 checkLen = (rValue == 3 || rValue == 4) ? 16 : 32;
                    if (!EqualsArray(uValue, _decryptor.UserKey, checkLen))
                    {
                        _decryptor.SetupByUserPassword(_documentId, oValue, pValue);
                    }
                }
            }
        }
예제 #3
0
 public static string GetFileNameWithEncryptExtension(string fileName, EncryptAlg encryptAlg)
 {
     return(encryptAlg switch
     {
         EncryptAlg.None => fileName,
         EncryptAlg.AES => fileName + FileConstants.AesExtension,
         EncryptAlg.Rijndael => fileName + FileConstants.RijndaelExtension,
         _ => fileName
     });
        private async Task <bool> EncryptDataToFile(byte[] streamedFileContent, string absolutePath, EncryptAlg encryptAlg)
        {
            if (encryptAlg == EncryptAlg.AES)
            {
                var aesData = await _context.AesKeys.FirstOrDefaultAsync();

                return(SymmetricCrypto.EncryptDataAndSaveToFile(streamedFileContent, aesData.Key, aesData.IV, absolutePath, CryptoAlgorithm.Aes));
            }
            else
            {
                var rijndaeData = await _context.RijndaelKeys.FirstOrDefaultAsync();

                byte[] key = Convert.FromBase64String(rijndaeData.Key);
                byte[] IV  = Convert.FromBase64String(rijndaeData.IV);
                return(SymmetricCrypto.EncryptDataAndSaveToFile(streamedFileContent, key, IV, absolutePath));
            }
        }
 private async Task <bool> UploadFile(byte[] streamedFileContent, string absolutePath, EncryptAlg encryptAlg)
 {
     if (encryptAlg == EncryptAlg.None)
     {
         return(await FileHelpers.UploadFile(streamedFileContent, absolutePath));
     }
     else
     {
         return(await EncryptDataToFile(streamedFileContent, absolutePath, encryptAlg));
     }
 }
 public PdfEncryption()
 {
     _alg       = EncryptAlg.STANDARD_ENCRYPTION_40;
     _keyLength = 40;
     _keySize   = 0;
 }