How i can use Cryptography library to encryption files in python - python-2.7

I'm trying to encrypt and decrypt PDF file using crypto and everything works well but I need to use another library like Cryptography https://github.com/pyca/cryptography .
How I can use Cryptography library to encrypt and decrypt files like PDF file because I search a lot and I can't find one example of how I can use Cryptography with files.
Any example may help.

To answer your question Cryptography's documentation could potentially help you with this. I used the library to encrypt text files, python files, i not sure about pdf's though. But the doc's were helpful. https://cryptography.io/en/latest/
Plus this website also helped me out as well.
https://www.thepythoncode.com/article/encrypt-decrypt-files-symmetric-python
It implements the Fernet key type encryption which is symmetric encryption.
There is asymmetric encryption (I believe this type uses public and private keys) you can as far i know encrypt strings with this type of encryption. As far as encrypting files with this type of encryption i am not sure about that as i have not done any research on that my self lol!
I was messing around with some of the functions in this library and found some useful things in it. Hope this helps.
p.s. Stay away from the hazmat section of this library. ONLY use this if you really know what your doing in terms of cryptography!

Related

Encrypting files with crypto++ using RSA

I wanted to encrypt a large binary file using crypto++ library with asymmetric encryption(RSA) algorithm. I have searched for working example and couldn't find it.
Is it possible? What kind of things should i consider when i implement it using this library

Simple text-file encryption in C++

So I was looking at ways to lock file folders with a password in windows, and this type of security is not really supported.
Given I know C++ I was wondering if I could simply do this myself.
It would be simple enough, in the case of a text file, to copy the entire contents of the file into a C-string. I could then use basic logic to prompt for a password, if it matches, use an fstream overload and insert the whole string into a text file.
Then, simply wipe the file when I'm done using it.
I basically know how to do this, and the result would be a string containing the document compiled into a .exe which I assume would be unreadable. The thing is, I've never really studied encryption or computer security so I'm wondering how secure this would be, or if there is a better way to do this?
Could it be done on photo or video files as well, if so, how?
How hard would it be to reverse (decompile) the process?
What types of things could I do to make reversal more difficult, ie. using multiple strings, or mixing in random characters?
I'm not looking to hide super-sensitive files, I'm just curious about encryption basics.
Never implement crypto yourself - it is destined to fail. Use well reviewed libraries such as OpenSSL. A good example of using AES for file encryption: Encrypting and decrypting a small file using openssl
Using such simple approach will let you encrypt any file. And it will be secure. Why settle for weak encryption if you can have strong encryption?
If you don't want to write a program, just get, for example, OpenSSL and use the terminal: openssl des3 -salt -in file.txt -out file.des3

How to Encrypt a Folder Using C++?

I'm creating a program uysing c++ that relies off sensitive information contained within a folder located on my Ubuntu 14.04 desktop. I need some way to protect this information.
Essentially I have two buttons setup on my application. One to encrypt the folder and one to decrypt the folder. However, I have no experience with encryption and don't even know if you can encrypt a folder itself. Most tutorials I have found only talk about encrypting text. A friend recommended using AES encrytpion, but again, I can only find tutorials that show how to encrypt text.
Does anyone know of any way to protect these folders? They contain a large amount of images (.bmp and .png file types) concerning patient information along with a few text files. Obviously the quickest method would be best, as long as they aren't easily accessible without pressing the buttons.
Encryption is not some magic wand one can waive over some data, and encrypt it. If your application has a button that automatically "decrypts" the data, it means that anyone else can do it as well. For this button to work as you described, your application must logically know everything that's needed to decrypt the data. If so, a determined attacker can simply obtain a copy of your application, debug it, figure out how it decrypts the data, and game over.
At the very minimum, a passphrase will be required in order to decrypt the data; so that the application alone is not sufficient to effect encryption and decryption.
As far as the actual technology goes, the two primary software libraries on Linux that provide generic encryption facilities are OpenSSL and GnuTLS. Both provide comparable implementations of all standard symmetric and asymetric cipher-suites.
I believe that GnuTLS is a better API, and that's what I recommend. The design of GnuTLS's C API naturally lends itself to a light C++ OO wrapper facade. The GnuTLS library provides extensive documentation, so your first step is to read through the documentation; at which point you should have all sufficient information to implement encryption in your application.
Just a simple point.
You are going to have to make a blob, which you someway mount as a filesystem. You are also going to have to decide how to control access to that filesystem while people are using it. Also how people are going to synchronize access. Do it wrong and two people will write to the same area at the same time and create something that no one will ever decrypt!
Look at the source code for dm-crypt and TrueCrypt, but if you want to limit access beyond the permission system that your OS supports you may find yourself way in over your head.
you need build private filesystem,so every file operator must pass you application. you can encrypt the file contain to user.

Easy way to encrypt a text file

I'm working on a text based adventure game in C++ and I would like to store quests in a text file,but I don't want the player to read it.
Is there an easy way to encrypt it?
Another way to "hide" content of your file to player is to encrypt the file.
You can use openssl for instance.
In this thread you can have an idea on the usage.
Velthune's OpenSSL suggestion is fine but it is arguably overkill. I would try something simple like XOR encryption instead.
Of course XOR encryption is not secure, but neither is the OpenSSL approach, since your program must store the encryption key somewhere in the executable file in order to be able to do the decryption.
There is no way to truly secure the file's contents against a determined user and still have it be accessible to a program that runs on the user's machine.
So, I'd suggest XOR encryption as a simple form of obfuscation that will deter someone from changing the file casually, yet won't make your program dependent on an external library.

How to sign XML document or verify XML document signature with C++?

Subj. I need to sign/verify under Windows in native C++ (no .NET), using private key for signing, public key for verification.
I saw few examples on MSDN (http://msdn.microsoft.com/en-us/library/ms761363(VS.85).aspx) that demonstrate how to sign the document with CSP (I don't know what this means).
For my case I need to use a "key" from the binary data array (using DSA encryption algorithm)... can somebody help me with that?
Thank you in advance.
I recommand libxml2 and xmlsec which are perfect for this purpose.
The API can seem hard to read at first, but it works well. xmlsec uses OpenSSL to achieve the cryptographic part.
Providing a "short" example here is probably hard because the three libraries require some initialization and a lot of C calls.
Google's keyczar lib is also very easy to use
There's also Crypto++