Unable to Decrypt the data properly using javax.crypto class and SunJCE

Hello all,
I am not new to Java but new to this forums
but and JCE and i wanted to write a program that Encrypts a file and also another program that decrypts it. As far Encryption is concerned i have been successful but When it comes to Decryption things aren't looking bright i have some or the other Problem with it. plz help me out .
Here is the Code for my Programs
Encryption
Code:
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
import javax.swing.*;
class MyJCE
public static void main(String args[])throws Exception
Provider sunjce = new com.sun.crypto.provider.SunJCE();
Security.addProvider(sunjce);
JFileChooser jfc = new JFileChooser();
int selection= jfc.showOpenDialog(null);
if(selection==JFileChooser.APPROVE_OPTION)
FileInputStream fis = new FileInputStream(jfc.getSelectedFile());
System.out.println("Selected file " + jfc.getSelectedFile());
try{
KeyGenerator kg = KeyGenerator.getInstance("DESede");
SecretKey key= kg.generateKey();
byte[] mkey=key.getEncoded();
System.out.println(key);
SecretKeySpec skey = new SecretKeySpec(mkey, "DESede");
Cipher cipher=Cipher.getInstance("DESede/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE,skey);
byte[] data= new byte[fis.available()];
//reading the file into data byte array
byte[] result= cipher.update(data);
byte[] enc= new byte [fis.read(result)];
System.out.println("Encrypted =" + result);
File fi= new File("/home/srikar/Encrypted");
FileOutputStream fos= new FileOutputStream(fi);
fos.write(enc);
fos.close();
byte[] encodedSpeckey = skey.getEncoded();
FileOutputStream ks= new FileOutputStream("./key.txt");
ks.write(encodedSpeckey);
System.out.println("Key written to a file");
}//try
catch(Exception ex)
ex.printStackTrace();
}//catch
}This Creates a Encrypted File. and a Encrypted key.txt
Code:
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;
import javax.swing.*;
class Decrypt
public static void main(String[] args)
try
JFileChooser jfc = new JFileChooser();
int selection= jfc.showOpenDialog(null);
if(selection==JFileChooser.APPROVE_OPTION)
FileInputStream fis = new FileInputStream(jfc.getSelectedFile());
System.out.println("Selected file " + jfc.getSelectedFile());
//Read from the Encrypted Data
int ll= (int)jfc.getSelectedFile().length();
byte[] buffer = new byte[ll];
int bytesRead=fis.read(buffer);
byte[] data= new byte[bytesRead];
System.arraycopy(buffer,0,data,0,bytesRead);
//Read the Cipher Settings
FileInputStream rkey= new FileInputStream("./key.txt");
bytesRead = rkey.read(buffer);
byte[] encodedKeySpec=new byte[bytesRead];
System.arraycopy(buffer,0,encodedKeySpec,0,bytesRead);
//Recreate the Secret Symmetric Key
SecretKeySpec skeySpec= new SecretKeySpec(encodedKeySpec,"DESede");
//create the cipher for Decrypting
Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE,skeySpec);
byte[] decrypted= cipher.update(data);
FileOutputStream fos= new FileOutputStream("/home/srikar/Decrypted");
fos.write(decrypted);
}//if
}//try
catch(Exception e)
e.printStackTrace();
}//catch
}//main
}//classthis Decrypt.java is expected to decrypt the above encrypted file but this simply creates a plaintext file of the same size as the Encrypted file but its contents are unreadable.
Or I endup with Exceptions like BadPadding or IllegalBlockSize Exception if i use any other Algorithm .
Please help out
thanx in advance

Srikar2871 wrote:
Well thanx for ur reply but
As i said there are No issues with ENCRYPTION and am getting an Encrypted file exactly of the same size as that of the original file and NOT as null bytes and Even am able to get a Decrypted file of again the same size of the Encrypted File but this time that data inside is in unreadable format.I ran your code EXACTLY* as posted and the contents of the file when viewed in a Hex editor was
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00So unless you are running different code to what you have posted, your file will look the same.
Cheers,
Shane

Similar Messages

  • Customizing FD01 and FB70 using SAP PS Class and Characteristics

    Hello SAP Experts
    I have the following issue:
    My client has a requirement where we need to customize the Customer Master  (FD01) screen and the Invoice Posting Screen (FB70). A few additional fields have to be added by creating a separate tab. I was intending to take Abaper's help and do this using user exits but I have been suggested by the cleint to use SAP PS Class and Characteristics feature to do this. Can someone please throw some light on this feature and how can i create custom fields on FD01 and FB70 screens. Is there a way we could customize these screens using PS characteristics. Your opinions would be much appreciated.
    Please kindly chip in your ideas
    Regards,
    Nik

    Joao Paulo,
    Thank you for the response. I have tried to obtain some info from OSS but no luck. Tried all means but there is limited information available.
    Nik

  • Unable to load the data into Cube Using DTP in the quality system

    Hi,
    I am unable to load the data from PSA to Cube using DTP in the quality system for the first time
    I am getting the error like" Data package processing terminated" and "Source TRCS 2LIS_17_NOTIF is not allowed".
    Please suggest .
    Thanks,
    Satyaprasad

    Hi,
    Some Infoobjects are missing while collecting the transport.
    I collected those objects and transported ,now its working fine.
    Many Thanks to all
    Regards,
    Satyaprasad

  • Unable to change the data in PSA

    Hello All,
    I have a delta failure because of invalid characters in one of the field and now i wanted to change the PSA. I have deleted the request from all data targets and started changing the error record, it is not showing all columns from PSA hence i am unable to change the data in required field.
    Could you please let me know how will i see all columns from PSA. Thanks
    Regards,Ashok

    Hi Ashok,
    To increase/decrease the number of columns that are displayed on the menu from the Settings option select Change display variants
    Also if you want to see say all the failed records you can sort the order of the data using the Status column

  • Unable to access the data from Data Management Gateway: Query timeout expired

    Hi,
    Since 2-3 days the data refresh is failing on our PowerBI site. I checked below:
    1. The gateway is in running status.
    2. Data source is also in ready status and test connection worked fine too.
    3. Below is the error in System Health -
    Failed to refresh the data source. An internal service error has occurred. Retry the operation at a later time. If the problem persists, contact Microsoft support for further assistance.        
    Error code: 4025
    4. Below is the error in Event Viewer.
    Unable to access the data from Data Management Gateway: Query timeout expired. Please check 1) whether the data source is available 2) whether the gateway on-premises service is running using Windows Event Logs.
    5. This is the correlational id for latest refresh failure
    is
    f9030dd8-af4c-4225-8674-50ce85a770d0
    6.
    Refresh History error is –
    Errors in the high-level relational engine. The following exception occurred while the managed IDataReader interface was being used: The operation has timed out. Errors in the high-level relational engine. The following exception occurred while the
    managed IDataReader interface was being used: Query timeout expired. 
    Any idea what could have went wrong suddenly, everything was working fine from last 1 month.
    Thanks,
    Richa

    Never mind, figured out there was a lock on SQL table which caused all the problems. Once I released the lock it PowerPivot refresh started working fine.
    Thanks.

  • The ADO NET Source was unable to process the data. ORA-64203: Destination buffer too small to hold CLOB data after character set conversion.

     We developed a SSIS Package to pull the data From Oracle source to Sql Server 2012. Here we used ADO.Net source to pull the records from Source but getting the below error after pulling some 40K records.
      [ADO NET Source [2]] Error: The ADO NET Source was unable to process the data. ORA-64203: Destination buffer too small to hold CLOB data after character set conversion.
    [SSIS.Pipeline] Error: SSIS Error Code DTS_E_PRIMEOUTPUTFAILED. 
     The PrimeOutput method on ADO NET Source returned error code 0xC02090F5. 
     The component returned a failure code when the pipeline engine called PrimeOutput(). 
    The meaning of the failure code is defined by the component, 
    but the error is fatal and the pipeline stopped executing. 
     There may be error messages posted before this with more 
    information about the failure.
    Anything that we can do to fix this?

    Hi,
      Tried both....
      * Having schema type as Nvarchar(max). - Getting the same error.
      * Instead of ADO.Net Source used OLEDB Source with driver as " Oracle Provide for OLE DB" Getting error as below.
           [OLE DB Source [478]] Error: SSIS Error Code DTS_E_OLEDBERROR.  An OLE DB error has occurred. Error code: 0x80004005.
           [SSIS.Pipeline] Error: SSIS Error Code DTS_E_PRIMEOUTPUTFAILED.  The PrimeOutput method on OLE DB Source returned error code 0xC0202009.  The component returned a failure
    code when the pipeline engine called PrimeOutput(). The meaning of the failure code is defined by the component, but the error is fatal and the pipeline stopped executing.  There may be error messages posted before this with more information about the
    failure.
    Additional Info:
       * Here the Source task is getting failed not the conversion or destination task.
    Thanks,
    Loganathan A.

  • Unable to get the data when capture the BOL object infomation.

    Hello All.
    I created a Z component.
    In that component I created one View.
    In that view i have the context node with BOL object  -  BTAdminH
    In configuration also i am selecting the fields.
    But I am unable to capture the data.
    In that i write like
    data: lr_ent type ref to CL_CRM_BOL_ENTITY.
    lr_ent ?= me->typed_context->btadminh->collection_wrapper->get_current( ).
    I write this code in the EDIT button event.
    But lr_ent is not capture any value.
    BTADMINH is there in the CTXT class.
    In debugging GET_CURRENT( ) method
    me->collection_ref->collection
    is not having any value.
    I do not know why it is not having any value.
    When I am using value node it is working fine.
    But when i am using BOL object at that time only it is not getting any value.
    Can anybody give me suggision to solve this problem.
    In stadard it is working fine.
    How it is working in Stadard i did not understand.
    Why it is not working in Z component.
    How we can use BOL objets in Z components.
    Please give me some solution.
    Regards,
    B. Krishna.

    Hi krishna
    Is this a Z component to be used within a  standard component?
    ->then you need to create a usage definition to this component from standard
    ->expose btadminh as public context through component controller
    ->create binding to the standard btadminh through the usage initialize method
    then it should be okay
    If the Z component is standalone , then create Root object BTorder and BTAdminH node should having a hierarchial relationship to this. Initialize BTOrder through cl_crm_bol_core methods.

  • BOFC Unable to start the data source

    Hi there,
    i've done a standalone install using SQL 2008 R2 express. But i'm unable to start the data source and get this error:
    Failed to start data source
    Failed to start server instances on machine DELL1.
    Failed to initialize server configured on machine DELL1.
    Class not registered
    Thanks,
    Anees

    Ok, problem solved.
    I had to create a seperate DCOM user and assign it to the following roles in windows:
    log on as a batch job
    log on as a service
    then assign this DCOM user to the Ctbroker and CTserver. As i'm not running this on a domain, there was no need to use 'packet level authentication'. So i set the authentication level to none.
    There was no need to use the web based admin interface. Ctadmin.msc worked perfectly.
    Thanks,
    Anees

  • Unable to change the data source of SAP system

    Dear all,
    We have install SAP netweaver EHP1 standalone java on oracle 10.2.0.5 on windows 2008 R2.
    while installation i have given the central SLD system as solman,
    By mistake i have deleted the system from SMSY.. now when i createed the system manually i am unable to change the data resource as SLD due to this i can start my SMD configuration
    Request please let me know how to resolve this issue.
    Thanks
    Paresh

    Hi Paresh,
    You can proceed with deleting your SAP system from SMSY (along with connected technical content such as AS Java definition) and the run the LANDSCAPE FETCH through transaction SMSY_SETUP to have the correct definition of your SAP system in SMSY.
    Following this, you could use the addon Landscape Verification (a new thing I learnt today!!!)  to check the correctness of transaction SMSY. The following link details the Landscape verification 1.0 for SolMan:
    http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/e093b3ce-c034-2d10-01bc-b51f1691d3da?quicklink=index&overridelayout=true
    Hope this helps sort out your issue.

  • Unable to find the data source in r/3  ECC 6.0 -  ( 0FI_GL_10 ) in RSA5

    Hi guys,
    I am unable to find the data source(0FI_GL_10) in RSA5.
    Presently I am using BI7.0 version with R/3 ECC 6.0.
    BI Patch level - 16.
    BI content Patch level - 8
    Thanks,
    Gupta

    Hi,
    Try Installing Application Component Hierarchies 
    http://help.sap.com/saphelp_nw2004s/helpdata/en/b3/c30a0e9c44204eb596c6353c827d2e/frameset.htm
    Use RSA9 for this.
    Hope this helps.
    Thanks,
    JituK

  • Excel 2013 PowerPivot Error - "PowerPivot is unable to load the Data Model"

    I'm attempting to use the PowerPivot add-on in Excel.  When clicking 'Manage Data Model' or attempting to 'Load to data model,' I receive the error 'We couldn't load the Data Model.  This may be because the Data Model in this workbook is damaged.',
    followed by 'PowerPivot is unable to load the Data Model.'  This happens irrespective of data source type.  I have Excel 2013 32-bit, PowerPivot 32-bit, running on Windows 7 OS 64-bit.  I am not running an SSAS instance on my machine. 
    Any suggestions?

    Hi
    Have you ever tried to repair your office?
    Also we may refer to the following blog :
    http://blogs.technet.com/b/the_microsoft_excel_support_team_blog/archive/2013/11/12/powerpivot-for-excel-2013-errors-after-october-update-kb-2825655.aspx
    This issue seems to be caused by the October 2013 update (KB 2825655) for Excel 2013. Try to remove it or restore the system to an earlier point and check the result.
    Regards
    Tylor Wang
    TechNet Community Support

  • Unable to open external list : Error : Unable to render the data. If the problem persists, contact your web server administrator.

    Hi,
    Please note we are using SQL Server 2008 for sharepoint. Does it matter for this issue?
    Please help.
    Thanks.

    Hi,
    According to your post, my understanding is that you failed to open external list.
    Please change the External Content’s connection properties’ Authentication Mode to BDC identity.
    Then launch the SharePoint 2013 Management Shell and run the PowerShell commands to remove the error.
    Here is a similar thread for your reference:
    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/69d937e6-f4a3-40e0-b57f-67cddb4ed12e/sharepoint-2013-unable-to-render-the-data-if-the-problem-persists-contact-your-web-server?forum=sharepointcustomization
    Best Regards,
    Linda Li
    Linda Li
    TechNet Community Support

  • Sharepoint ONLINE - Unable to render the data. If the problem persists, contact your web server administrator

    Hi,
    I have followed the steps in the following link:
    Make an External List from a SQL Azure table with Business Connectivity Services and Secure Store, but I keep receiving the following message when I try to access an external list:
    "Unable to render the data. If the problem persists, contact your web server administrator"
    Note: I'm using SharePoint 2013 ONLINE, and (as far as I know) I have no access to the logs & powershell.
    Any body has had the same issue before?
    Regards.
    Note: I have tried changing my ECT's connection property (through designer 2013) to BDC Identity, but I keep receiving the following error message:
    The metadata object that has Name "xxxx" has a Property with name "AuthenticationMode" and value "RevertToSelf". This value indicates that the runtime should revert to the identity of the application pool, but reverting to the
    application pool is not permitted for partitioned service applications.
    Saeed Fattahi .NET Specialist

    Try changing the External Content’s connection
    properties’ Authentication Mode to BDC identity. You will most like get an error (which I have shown below)
    Below is an error you will received. Read through
    the error carefully.
    Launch the SharePoint 2013 Management Shell
    and run the below commands
    $bcsServiceApp = Get-SPServiceApplication
    | where {$_ -match "Business Data Connectivity Service"}
    $bcsServiceApp.RevertToSelfAllowed
    = $true;
    $bcsServiceApp.Update();
    This Solved the problem for me. you can try
    Sharepoint | TechCenter franklin

  • I have an external hard drive from my time capsule that stopped working on me. I am attempting to access the data with a hard drive reader on my MAC. I am able to see the drive in disk utility and under system info USB. But I am unable to access the data.

    I have an external hard drive from my time capsule that stopped working on me. I am attempting to access the data with a hard drive reader on my MAC. I am able to see the drive in disk utility and under system info USB. But I am unable to access the data and it does not show on the desktop when connected.

    Ok if disk utility was able to verify the drive I doubt there is any problem.. are you trying to open a TM backup??
    You need to mount the sparsebundle then check the actual info inside the bundle.
    Don't use disk warrior.. if the disk has verified then unless you deliberately deleted files there is nothing that is going to do.
    Pondini has a lot of stuff about getting access to the sparsebundle.
    http://pondini.org/TM/17.html
    But if you have copied info to the TC that is now gone.. and the disk is ok.. I am not sure.. the TC will not have deleted the files itself.

  • SP online 365 - Unable to render the data. If the problem persists, contact your web server administrator.

    Help needed urgently, i'm pulling my hair out on this one!
    Using SharePoint online (365) I am trying to create an external list to a SQL table. I go through the steps in SP designer and finally create the external list but when I go to the list I get the error:
    Unable to render the data. If the problem persists, contact your web server administrator.
    I have read all of the posts on here to fix this and they are mainly for on premise; the powershell doesn't work with spOnline and I have added permissions into the BCS service application but nothing works. I read that it is not possible to connect
    sharepoint online to an on-premise SQL database so I moved the SQL database to an azure SQL service server but I still get the same error.
    I have a "test" on premise sharepoint which I have tried this on and it works fine so it must be an issue with SPonline! does anyone know how to get BCS connections to external data working in sharepoint online as external lists?

    Hi,
    In SharePoint online, we support:
    Business Data Connectivity (BDC)
    Data Source types, including Windows Communication Foundation (WCF) 
    SQL Server Azure connectors
    I'd suggest you refer to the article below:
    https://support.office.com/en-us/article/Make-an-External-List-from-a-SQL-Azure-table-with-Business-Connectivity-Services-and-Secure-Store-466f3809-fde7-41f2-87f7-77d9fdadfc95?ui=en-US&rs=en-US&ad=US
    It describes about how to use SharePoint technologies to access data from a SQL Azure database without having to write code, including create an External List by using Business Connectivity Services (BCS) and Secure Store. BCS connects SharePoint solutions
    to external data, and Secure Store enables user authentication for the data. By using an External List, you can display the contents of a table from SQL Azure in SharePoint Online. Users can read, edit, and update the data, all in SharePoint Online.
    Regards,
    Rebecca Tu
    TechNet Community Support

Maybe you are looking for