Get string output for PGP Encrypt and Sign in one pass using Bouncy Castle - bouncycastle

I have the code which can PGP encrypt and sign a file in one pass and save the encrypted data in another file.
The problem is that I have to pass this encrypted data to another API in JSON format. For that I need to convert the encrypted data to a string which can be identified by the API and then later be decrypted.
Is there any way to PGP encrypt and sign a particular string and then get the encrypted response as a string which can later be decrypted.
Apparently reading the file contents to a string and then trying to decrypt the string is not helping.

The solution would be to convert the file contents to a byte array and then base-64 encoding the byte array.

Related

Convert BASE64 String to PDF file in the IFS on AS400

We receive a BASE64 encoded representation of a courier label PDF in an xml file, which we store in the IFS of our AS400.
We would like to decode this BASE64 string and save it as a .PDF in the IFS so we can then either email it as an attachment or send it to a printer.
I have looked at the capability of the CPYSPLF command using the *PDF WSCST parameter, but this only seems relevant where we would have a Spooled File representation of the label we want to produce.
Does anyone know if this is possible via native iSeries commands/RPG?
One way is to
IFS_READ_UTF8 to load the XML file
XMLPARSE to make it a XML object
XML_TABLE to extract BASE64 data
BASE64_DECODE to decode B64 data to PDF binary stream
IFS_WRITE_BINARY to write that stream as a .pdf file
You could make it a pure sql prodecure, or a SQLRPGLE program.
You also could extract BASE64 date using RPGLE XML-INTO. The use Scott Klement's BASE64 SRVPGM to decode it, then write it to the IFS

Read file metadata along with data in Python

I am using Python 3.7 along with a library for AES CBC on Windows 10 to encrypt files and it works perfectly. Except, after decrypting them, they lose their metadata like the date they were created. Because I want the user to feel like they never 'deleted' or 'lost' the original file, I need to preserve that data.
This is what I'm doing to read the data:
f = open(file_name, "rb")
data = f.read()
f.close()
After I encrypt the data, I write the encrypted bytes into a new file. When I decrypt this new file, I would like the metadata to be preserved so that the file (like an image) is exactly like it was before encryption. (P.S. I don't know if this will help but overwriting the new data on the original file might help but I want to try and avoid this if possible)
How do I include metadata of the file in a variable WITH the data that I am encrypting, such that when I decrypt, I get the exact same file with the same date created etc.?
EDIT:
I found a way to get the file creation time but I STILL NEED to get all the metadata as the file can be in any format: for example an image or a video, or a doc file's author. I also want to store this in the decrypted file which I don't know how to.
os.path.getctime(file_name)

I have encoded string but i don't know what type is it

I have two encoded strings that used same encoding method but i don't know what type it is.
I have tried using base64 decode but it didn't work.
This is the first encoded string I have 3qpY0Vw86MZykGfqc7jnVg==
This is the second encoded string I have nB6dtl3iA5IE1Z+g9SpBrw==
They are using same encoding method.
I want to know what type of encoding that used in that strings. Also I want to know how to decode it.
Those base64 payload may be containing something else than a string, like a raw binary, an image, a ciphered payload, etc. that can't be displayed as text.
base64 is not exclusively used to encode text.
For example to save to a file:
$ printf %s 'iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAYAAADimHc4AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAZdEVYdFNvZnR3YXJlAEFkb2JlIEltYWdlUmVhZHlxyWU8AAAMfUlEQVR4Xu1bC3BU1Rn+7mN3E8IjEiAY3iQxIA8bKiJSRUofjLZKx0cf6mgfap3O6NjptNNqW6vjYDt2+pip09ZXqXa01amIoqJTpVYIijYIpFEgBAMkhEB4JCG7e/fe2+8/ezdsHhYq7t7d5H6ZO3tz7tm7e//vnO9/nLOaSyCAb9C91wA+ISDAZwQE+IyAAJ8REOAzAgJ8RkCAzwgI8BkBAT4jIMBnBAT4jIAAnzHoCbCbt8La+CicQ41eS25hUBOQ+M9LiK++A7G1K5DY9Be4iZh3JXcwKMvRbvQYrA0PI1H7NziH9/IpNWjDShD+/A8Rmne11ys3MOgIcKMdiL34M9hbVnPERwEjDE3a7Tj0sZWIXH4fjCnzk51zAINOgrRwEQ28AG7HgaTxOfplBsi507aDM+OPcI7s83r7j8HnA3Qd5uxLEVpyG3C83WsUDkiCbiJR/wqsd56gg8gNfzAonbAWHobQwm9Ar1oKN9bptbJdN9RrYsNDdNAvq3O/MWijIH30FIQ//V1ow8dS/y2vleAscOPHEXtlBZyWOq/RPwzqMNSctlBFPoh2IhVrKCmiP3AP7Ub8jQfhdB9W7X4hbwlwY13ovKuCDrXZaxkY5uzLEVr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JGDheyyZkZMrabGS4qulk9AgXqaXKXIL/BAxx5AABlCcpG0iWKiWIDB7qcySTziH4T4Ds2xzByGTUhIwfWvEkJXW5BP/DUNnJ3N7EgcnR6TnAjIGfoY0qU1sZ0xEkYj5jaEdBQxwBAT4jqwToH/KjCr9hmCd+oJdtZNUHRCIRFBcXqwWbXIF8v2g0is7OTvW9su0DskKAIPUxGfi400bqOwqyTUDWNCH1YClSculIIf08W8iqKPd98Fw7/EBGCBCREamRQ3Q1L48sSWXGfMDGmhrfRtXHAdkpMb28HOU8MomMEBDg1JGbgfkQQkCAzwgI8BkBAT4jIMBnBAT4jIAAnxEQ4DMCAnxGQIDPCAjwGQEBPiMgwGcEBPiMgACfERDgMwICfEZAgM8ICPAZAQG+Avgv4WYH0htgNTQAAAAASUVORK5CYII=' | base64 -d > stackoverflow.png

PyDES with Bloomberg data

I am trying to decrypt DES encoded Bloomberg financial data with PyDES. The file ends in gz.enc, I am able to read the file to a binary, reload the binary back in to a variable (the variable has data) and then use the following decryption on it.
k = pyDes.des(password, pyDes.ECB, padmode=pyDes.PAD_PKCS5)
released = k.decrypt(encrypted_data)
The problem is I get an empty string for 'released'. I unfortunately cannot share the file nor the password due to privacy laws. I cannot use the bloomberg decryption as they do not support Ubuntu nor Mac.

How to Encode a text using base64_encode() , So that it can't be decoded

I just want to know how to secure a text using base64_encode() , so that it can't be decoded properly from base64_decode().
If you have a text and encode it this way you simply change the byte sequence but it always can be transformed back into its original form; so you cannot stop people from doing it.
You could introduce errors into the base64 encoded form to stop the decode function to work but this is not at all secure.
To stop people from reading the text you could encrypt it and then base64 encode the encrypted form.
Here's one way to make sure your data can never be decoded.
import random
def secureEncode(s):
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890/+'
length = int(len(s)*4./3)
return ''.join(random.choice(chars) for _ in xrange(length))
In all seriousness, you seem to be completely missing the point. Base64 is a data format. The whole point is that you can get the data back. If you want to destroy data, just destroy it.
If you want to hide your data in a way that it can only be read if you know a secret, you need to look into encryption.

Resources