How to save and restore a Key?

Hi,
I Am working through some example code based on the Sun documentation here:
http://java.sun.com/j2se/1.4/docs/guide/security/jce/JCERefGuide.html
Here is part of the program:
        KeyGenerator keygen = KeyGenerator.getInstance("DES");
        SecretKey desKey = keygen.generateKey();
            Cipher desCipher;
        // Create the cipher
        desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        // Initialize the cipher for encryption
        desCipher.init(Cipher.ENCRYPT_MODE, desKey);
        // Our cleartext
        byte[] clearBytes =  "Joe".getBytes("UTF8");
        // Encrypt the cleartext
        byte[] cipherBytes = desCipher.doFinal(clearBytes);
        // Initialize the same cipher for decryption
        desCipher.init(Cipher.DECRYPT_MODE, desKey);
        // Decrypt the ciphertext
        byte[]  decryptedBytes = desCipher.doFinal(cipherBytes);This all works fine. Now, suppose I want to save desKey for future use. (Maybe I want to save my encrypted bytes to a file and decrypt them in the future. Maybe I want to give someone else the key for decrypting cipherBytes. )
In the Key interface there is a method getEncoded() => byte[] which appears to be a way to save the key as a byte array for future use. However, I don't see how to restore the key from the byte array. That is, I expected a Key interface constructor something like: Key(bytes[], encodedKey). What am I misunderstanding?
Regards,
Joe

>
This works fine: SecretKey myKey = new SecretKeySpec(rawKey, "DES");
And indeed it does. Thanks, mut - I learned something new today. (Of course, I feel stupid for not having ever noticed that SKSpec isA SecretKey - but that happens to me allll the time...)
I couldn't get Grant's code to compile. Seems like
the SecretKeyFactory class doesn't have a
method named generateKey (in my 1.4 SDK).
I replaced generateKey with
generateSecret which compiled without error,Yup, sorry, I hadn't run the code thru a compiler or I'd've caught that.
but then when I ran the code the third line threw an
exception:
java.security.spec.InvalidKeySpecException:
Inappropriate key specification
Any idea what's going on?Interesting. I don't offhand see any way for one to work and the other to fail with the same raw data. Then again, having been wrong twice in one post, I'm feeling abruptly unqualified to comment! ;)
Will experiment some, and let you know what I find out.

Similar Messages

  • How to save and restore Music from iTunes without using library ?

    Could yous help me please ?
    I use an 60 giga iPod I started to fill it with my CDs without using library, just directly in it, I delete the Music in the library after record to save place on may computer. I have nearly 7 megas of Music stored. To fill 60 Giga it wille takes at least 6 months on my rest time. At the end if i loose my iPod or if stolen or broken or if i want to buy a new iPod of 80 Giga for example to store more Music ? How can i save Music in my iPod on a Hard Disk or DVD and restore on it or on a new one ? Is it possible or not allowed by iTunes to avoid Music copie ?
    Thank you for your help....

    Frank31,
    Syncing music to the iPod is a one-way street, as discussed in this article:
    http://docs.info.apple.com/article.html?artnum=302155
    If you are concerned about using up your computer's hard drive for music, and are using iTunes 7, you might consider creating your library on an external drive if one is available.
    This article discusses creating an alternate iTunes library in another location, such as another drive:
    http://docs.info.apple.com/article.html?artnum=304447
    Thank you,
    Nathan C.

  • How to save and restore serialized map?

    Hi all,
    I have here a Serializable object with a map and some other variables in it.
    For distribution reasons I would like to get this object serialised and store it in a file (or just a string to test) and afterwards read it out and unserialize the string to the original object again.
    I didn't find it yet, but is anything like this possible?
    Many thanks in advance,
    Steven

    tombatore wrote:
    ooookey I just bumped into the ObjectOutputStream class, giving that one a try first ;)How about you give this a try: [http://java.sun.com/docs/books/tutorial/reallybigindex.html]?
    And seaching the (web|fora)?
    :)

  • How Do I save and restore keywords, categories and tags in PE6?

    I have my photos backed up on an external hard drive, but if and when my hard drive crashes, how do I get back the tags, keywords, categories, etc. that I have used in the PE 6 Organizer?  Can I save them and, if so, when I need to restore this, how do I do that?  How do I reconnect tags, categories, keywords, etc. with photos that I re-import after a hard drive crash?

    John: Many thanks for this help.  I use CS3, but my mom has PE6 and I'm trying to help her.  I'm not as familiar with PE6.
    Again, thanks,
    Mark
    Date: Sat, 12 Sep 2009 13:24:12 -0600
    From: [email protected]
    To: [email protected]
    Subject: How Do I save and restore keywords, categories and tags in PE6?
    PSE keeps your keywords, categories, albums, stacks, version sets, captions, notes, dates, and star ratings in a file in a "catalog folder".  (Do Help > System Info to find the location of your catalog folder).  When you make a backup with the PSE File > Backup command, it copies all your photos and the catalog file into the backup.   When you restore from that backup, it copies that catalog file and all your photos to the location you specify.
    >

  • How to view and restore sharepoint 2013 designer workflows and how to redeploy with newer version to environments

    how to view and restore sharepoint 2013 designer workflows and how to redeploy with newer version to environments
    MCTS Sharepoint 2010, MCAD dotnet, MCPDEA, SharePoint Lead

    Hi,
    In SharePoint Designer 2010, we could not save the workflow as a template directly except the reusable workflow.
    However, in SharePoint Designer 2013, we can just save all the types workflow as a template, then you can import the workflow into the new environment.
    http://blogs.msdn.com/b/workflows_for_product_catalogs/archive/2012/11/02/deploying-a-workflow-on-a-different-server.aspx
    In SharePoint Designer 2013, every time we publish the workflow, we would get a newer version workflow, and the old workflow version would be overwritten.
    So, when you deploy the workflow in the environment, the workflow would the newer version.
    Thanks,
    Jason
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected]
    Jason Guo
    TechNet Community Support

  • Built-in SSH Terminal question on how to save and exit

    Howdy, Heres my question and problem.
    I haven't been able to figure out how to save and exit the editor for the SSH terminal that the mac has. I've read that to save you press control -O hit return and to exit hit control -X but that doesn't seem to be working.
    I'm trying to install imagemagick onto my web server and I get stuck at that point of saving and then exiting.
    can anyone tell me how to save and exit the editor?
    I hold down control press -O i let go of the buttons then i press control -X and it stays at the same editor like nothing happened. I try quitting the terminal and reloading it all but that just causes me to restart and nothing saved. and I actually hit the "-" key should i do that or no?
    heres the instructions I'm following for the imagemagick install.
    (For this next part, if you don't have "pico", you can use "vi" instead)
    $ cd libtiff-lzw-compression-kit
    $ pico Makefile
    change
    TIFFSRCDIR = /tmp/libtiff
    to
    TIFFSRCDIR = ../tiff-v3.5.5
    Then save and exit the Editor $ make install
    $ cd ..
    $ cd tiff-v3.5.5
    $ ./configure
    thanks for any help
    -Ahufs

    ctrl+x (thats both keys at the same time) will invoke the exit of the program. at wich point if youve made changes to the file it will ask you if you want to save the data or not. If yes you need to type yes or y and hit enter... if no then no or n. Then it will ask you what you want the file name to be with the default being whatever the file was that you opend to buffer to work on so in most cases you hit enter again.
    hitting ctrl+o (thats both at the same time again) should write the file out and close the application without prompting i think (i dunno for sure... i always use ctrl+x myself).
    At least thats on the OS X version of pico... your server is probably linux so it may differ although i wouldnt expect it to. my pico bindings have always been the same from Debian to CentOS to Darwin.
    Also i msut ask... why cant you pass the TIFFSRCDIR as an option to ./configure like normal instead of manually editing the make file?

  • HT1766 Now, I understand how the backup and restore the backup functions. However, if I don't want to transfer the music that I have on iTunes to my phone, how do I avoid it? How do I not transfer the music or take it out of my iPhone if it is already the

    Now, I understand how the backup and restore the backup features function. However, if I don't want to transfer the music that I have on iTunes to my phone, how do I avoid it? How do I not transfer the music or take it out of my iPhone if it is already there? I really do not want any music on my iPhone because I have an iPod for that. Thanks. Hope someone can help.

    When you connect your phone and start iTunes on the left side you will see your phone listed under devices.Click on te icon next to it that looks like a battery. Click on the music button at the top and uncheck the sync music box.

  • How to backup and restore in sap ECC 6.0  EHP 6 in HANA database

    how to backup and restore in sap ECC 6.0  EHP 6 in HANA database

    Stop loading your interview questions here - otherwise your user ID will be deleted.
    Thread locked!

  • How to save and  move an application

    Hi,
    I have made an application on Oracle Express. How to save and move that to another machine ?
    Many thanks.

    I'm in the process of porting my XE app to a 10.1 instance.
    I created an app export and export file of the schema. Then I installed a client on the same machine so I could connect and I get errors detailed in the end of this thread when attempting to import. Most of the tables are successful however, none of the procedural code goes in. The app export imported fine.
    Now, I can manually copy the objects that remain but only if I can connect to XE and this ability seems to have been lost with the install of the 10.1 client.
    Does anyone know how I can connect to XE using PLSQL Developer or is it indeed possible to connect to XE on a machine that is configured to also connect to a 10.1 remote server? I have installed the 10.1 client into a new Oracle home.
    . importing CODESPEED's objects into PRIMPATH
    IMP-00008: unrecognized statement in the export file:
    IMP-00017: following statement failed with ORACLE error 1950:
    "CREATE TABLE "FOLDER" ("FOLDER_ID" NUMBER NOT NULL ENABLE, "STAFF_ID_CREATE"
    "" NUMBER NOT NULL ENABLE, "STAFF_ID_MODIFY" NUMBER, "FOLDER_NAME" VARCHAR2("
    "100) NOT NULL ENABLE, "FOLDER_CREATED" DATE NOT NULL ENABLE, "FOLDER_MODIFI"
    "ED" DATE NOT NULL ENABLE, "FOLDER_NOTES" CLOB, "FOLDER_OWNER" NUMBER NOT NU"
    "LL ENABLE, "DRA_VERSION_ID" NUMBER, "ASSIGNED" NUMBER NOT NULL ENABLE, ""
    "UNASSIGNED" NUMBER NOT NULL ENABLE, "APPROVED" NUMBER NOT NULL ENABLE, "FOL"
    "DER_STATUS" VARCHAR2(1) NOT NULL ENABLE, "CODING_RULE_SET" NUMBER) PCTFREE"
    " 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FR"
    "EELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "USERS" LOGGING NOCOMPRESS "
    "LOB ("FOLDER_NOTES") STORE AS (TABLESPACE "USERS" ENABLE STORAGE IN ROW CH"
    "UNK 8192 PCTVERSION 10 NOCACHE LOGGING STORAGE(INITIAL 65536 FREELISTS 1 F"
    "REELIST GROUPS 1 BUFFER_POOL DEFAULT))"
    IMP-00003: ORACLE error 1950 encountered
    ORA-01950: no privileges on tablespace 'USERS'
    IMP-00017: following statement failed with ORACLE error 2248:
    "ALTER SESSION SET PLSQL_OPTIMIZE_LEVEL = 2 NLS_LENGTH_SEMANTICS = 'BYTE' PL"
    "SQL_CODE_TYPE = 'INTERPRETED' PLSQL_DEBUG = TRUE PLSQL_WARNINGS = 'DISABLE:"
    "ALL'PLSQL_CCFLAGS = ''"
    IMP-00003: ORACLE error 2248 encountered
    ORA-02248: invalid option for ALTER SESSION
    IMP-00000: Import terminated unsuccessfully
    C:\Documents and Settings\PWin>

  • How to save and edit a Word template and excel??

    How to save and edit a Word template and excel (extension. doc and. xls) in an XE database, which can be opened to allow the document and record all changes made by the user of the palicacion.
      or where it a clear tutorial on this topic.?
    I appreciate your attention and collaboration.
    good day ...

    You should not expect to do this in Oracle. But by .net, it can be fine
    http://www.codeproject.com/Articles/36432/Edit-Microsoft-Word-Document-From-Your-Application

  • How to backup and restore iPhone 4

    how to backup and restore iphone 4

    How to back up: http://support.apple.com/kb/HT1766.
    How to restore: http://support.apple.com/kb/ht1414.

  • Please help! I am currently using iPhone 5 and changing to iphone 6 in sometime. I have taken backup of my photos, videos, contacts, notes and whatsapp. The only thing that remains is my iMessage. Can someone tell me how to backup and restore my iMes

    I am currently using iPhone 5 and changing to iphone 6 in sometime. I have taken backup of my photos, videos, contacts, notes and whatsapp. The only thing that remains is my iMessage. Can someone tell me how to backup and restore my iMessages?

    iMessages are backed up in the normal backup of your phone so it will be restored when you restore the backup.

  • Can anyone tell me how to find and restore previously purchased OS updates to my iMac?

    Can anyone tell me how to find and restore previously purchased OS updates to my iMac?

    Please go to the Apple (upper left corner) and click on About this Mac. In the resulting window, click on the numbers underneath until they display the OS version.
    Snow Leopard, Lion, and Mountain Lion are all OS X - they are 10.6, 10.7, and 10.8 respectively. After you get the OS version, click until you get the "Build" number and post that as well.
    If your Mac is from 2010, it most likely came with Snow Leopard. If you purchased Lion, then you should be able to see it under your Purchases in the App Store (not iTunes). But, first, we need to figure out what was installed on your machine after the hard drive replacement.
    Not sure about iPhoto at this point - may have to do with whaever OS version is installed. Check in Applications folder in column view and post back which version of iPhoto you have installed.

  • Just set-up my first IPad (Air) using ICloud.  This resulted in none of my desktop Outlook icons working.  Can anyone tell me how ICloud works and restore functional Outlook icons on my desktop.

    Just set-up my first IPad (Air) using ICloud.  This resulted in none of my desktop Outlook icons working.  Can anyone tell me how ICloud works and restore functional Outlook icons on my desktop?

    Just set-up my first IPad (Air) using ICloud.  This resulted in none of my desktop Outlook icons working.  Can anyone tell me how ICloud works and restore functional Outlook icons on my desktop?

  • How to Create, Save and Restore a Custom Catalog in PE 6?

    I am having great difficulty in trying to create a custom catalog using the organizer function of PE 6. Upon opening the organizer I have many photos already depicted in it. So, I highlight them all and delete them obtaining a blank screen with no photos. I then import the file containing only the custom photos I want to catalog. I then backup this catalog using the following procedure http://www.backuphowto.info/how-to-backup-adobe-photoshop-elements-6-catalogue Everything looks fine until I try to restore it at a future time. It doesn't restore! In fact I can browse to the saved file but am unable to get the locate backup file location to change to it??
    What am I doing wrong and can anyone help me acheive my objective of creating a custom catalog that I can save and retrieve intact for later use?
    Thanks, in advance, for any help provided. Oh, and I need to add I am relatively new to PE 6 having only previously used PE 4 and never used the organizer in it which is to say -  lead me very simply please.
    Sanray19

    sanray19 wrote:
    I am having great difficulty in trying to create a custom catalog using the organizer function of PE 6. Upon opening the organizer I have many photos already depicted in it. So, I highlight them all and delete them obtaining a blank screen with no photos. I then import the file containing only the custom photos I want to catalog.
    I can't imagine the purpose of a "custom catalog". Seems to me you have done a lot of work when I am 100% sure there is an easier way to do whatever it is you are trying to accomplish. Further, it seems like you are trying to create a backup of only some of your photos, and I can't imagine the purpose of that either ... I would presume you would want to make backups of your entire collection of photos.
    Just as a general rule, there usually isn't a good reason to have more than one catalog (unless you have separate categories of photos, like personal and work).
    Everything looks fine until I try to restore it at a future time. It doesn't restore!
    Can you give us a detail or two about what happens when "it doesn't restore"? Can you give us more details about what you actually did? And most importantly, can you describe what you see on the screeN? Screen captures would be even better.

Maybe you are looking for

  • How to develop BRF

    Hi All, I'm currently in ISU-FICA module. We now have to implement SAP Collections management in FICA. As per our client requirement I have to use BRF (not BRF+) to implement business rules. Our approach goes slightly different. Normally BRF has to b

  • Problems while using ORDER BY in declaring a PL/SQL cursor

    Hi, everybody I hope you can help me. We are programming a very simple PL/SQL Procedure where we are declaring the following Cursor:Declare Cursor CUnidadP is Select UNOR_CO_UNID_ORGID,UNOR_CO_PADREID,UNOR_NU_NIVEL from Unidad_Organizativa where Unor

  • How to create new themes in adobe flex projects

    How to create new themes in adobe flex projects 

  • In Service Tickets-Adding Buttons-transaction-launcher

    Hi i need two information's.. 1.how to add buttons in the service tickets after Escalate and save button. 2.I have to retrieve the value of some fields in the categorization schema based on the service ticket organization..    its urgent for me.   i

  • Unbalanced transaction - A/P Invoice

    Dear all, I am facing the error "UNBALANCED TRANSACTION" while posting the A/P invoice document. I have tried all the solutions provided in this forum, like checking the taz amounts, unchecking the unbalanced FC for journal entries in document settin