locked
how to decrypt pdf by using a key RRS feed

  • Question

  • User1436192082 posted

    Hello,

    I have a different requirement, pdf is encrypted by using a key and that encrypted pdf will be sent to client, when anybody opens

    that pdf, it asks for key, if right key applied pdf should be decrypted and displayed.

    I have used RijndaelManaged Algorithm for encrypting pdf file, pdf is getting encrypted, but when i double click on that

    encrypted pdf its not asking for key. It should ask for key and pdf will be decrypted and will be opened.

    I have two programs , one for encryption , one for decryption. I am successfully encrypting and decrypting by using those

    prg's. but for decrypting i cannot use program, when we double click on file then only decryption program runs and check

    for right key, should be decrypted. 

    I have been really searching for this specific thing for the past 48 hours. I did not find any solution. 

    Please help me out from this problem...

    This is my code to encrypt pdf by using a key

    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] key = UE.GetBytes(password);

    string cryptFile = outputFile;

    FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

    RijndaelManaged RMCrypto = new RijndaelManaged();

    CryptoStream cs = new CryptoStream(fsCrypt,
    RMCrypto.CreateEncryptor(key, key),
    CryptoStreamMode.Write);

    FileStream fsIn = new FileStream(inputFile, FileMode.Open);

    int data;
    while ((data = fsIn.ReadByte()) != -1)
    cs.WriteByte((byte)data);


    fsIn.Close();
    cs.Close();
    fsCrypt.Close();

    This is my decryption program.

    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] key = UE.GetBytes(password);

    FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);

    RijndaelManaged RMCrypto = new RijndaelManaged();

    CryptoStream cs = new CryptoStream(fsCrypt,
    RMCrypto.CreateDecryptor(key, key),
    CryptoStreamMode.Read);

    FileStream fsOut = new FileStream(outputFile, FileMode.Create);

    int data;
    while ((data = cs.ReadByte()) != -1)
    fsOut.WriteByte((byte)data);

    fsOut.Close();
    cs.Close();
    fsCrypt.Close();

    Thanks in Advance...

    Friday, April 6, 2012 6:06 AM

Answers

  • User1436192082 posted

    Hi,

    I have earlier used ITextSharp.dll , but i didnot get the solution. I finally findout solution in someother way.

    string outputFile = @"E:\javascript6secure.pdf";
    string inputFile = @"E:\javascript6.pdf";

    using (Stream input = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
    using (Stream output = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
    {
    PdfReader reader = new PdfReader(input);
    PdfEncryptor.Encrypt(reader, output, true, "secret", "secret", PdfWriter.ENCRYPTION_AES_128);

    }
    }

    As you said ITextSharp.DLL is used here. Pdf is converted and even search engines cannot access these pdf's meta data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 13, 2012 1:14 AM

All replies

  • User1436192082 posted

    hi dev-guru,

    Thanx for advise. 

    your above lines of code will only do password protection for pdf, but it will not encrypt text in pdf.

    I want something which encrypts data in pdf file using a password or key, and any body can view that pdf provided same key.

    any advises are greatly welcome

    Dileep kumar.

    Tuesday, April 10, 2012 2:35 AM
  • User1436192082 posted

    Hi,

    I have earlier used ITextSharp.dll , but i didnot get the solution. I finally findout solution in someother way.

    string outputFile = @"E:\javascript6secure.pdf";
    string inputFile = @"E:\javascript6.pdf";

    using (Stream input = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
    using (Stream output = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
    {
    PdfReader reader = new PdfReader(input);
    PdfEncryptor.Encrypt(reader, output, true, "secret", "secret", PdfWriter.ENCRYPTION_AES_128);

    }
    }

    As you said ITextSharp.DLL is used here. Pdf is converted and even search engines cannot access these pdf's meta data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 13, 2012 1:14 AM