Generate key

I hv generated a key by using KeyGenerator under javax.crypto.*.
after generating the key, i transform it into a string variable and store it in database..
how should i decrypt by using the same key??
i mean.. if i want to use back the key which i hv transform into string..
how should i do??
pls help me.
thanks.

Hi,
u may try this:
on encryption side:
KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
     SecretKey skey = kgen.generateKey();
     byte[] raw = skey.getEncoded();
store raw somewhere(db, or a file);
on decryption side:
retrieve raw from your datastore;
SecretKeySpec skeySpec = new SecretKeySpec(raw, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] decrypted =
cipher.doFinal(ur encrypted data);
hope this can help

Similar Messages

  • Getting generated keys inside Java

    Is there any way to get generated keys in Java running on CF
    server?
    I have some java code accessed from CF, I get a connection to
    the data source by name via
    "coldfusion.server.DataSourceService.getDatasource(datasourceName).getConnection()"
    call within java, which works. However if I try to do a
    connection.prepareStatement(sql,
    PreparedStatement.RETURN_GENERATED_KEYS) call or
    preparedStatement.getGeneratedKeyts() call I get a
    "java.lang.AbstractMethodError" exception like:
    java.lang.AbstractMethodError:
    coldfusion.server.j2ee.sql.JRunConnectionHandle.prepareStatement(Ljava/lang/String;I)Ljav a/sql/PreparedStatement;
    Which seems to indicate to me that CF does not implement
    getting generated keys via JDBC - is there another way or do I have
    to just give up and do a "select" after "insert"?
    Hope this made sense.
    Thanks,
    -HH
    P.S. In case this is usefull, CF MX7 with MSSQL2005 , JDK
    1.4

    Hichhiker wrote:
    > Is there any way to get generated keys in Java running
    on CF server?
    CF6/7 uses JDBC 2 so I don't think you can get it to work
    there. In CF 8
    it should work (if your database supports it).
    Jochem
    Jochem van Dieten
    Adobe Community Expert for ColdFusion

  • Generating key pair on PKCS#11token and save it there

    Hello,
    again i'm completely lost in this PKCS11 jungle.
    What i want to do:
    Generating key pair on crypto pkcs11 token and store it there.
    In the moment i've tried eg:
    sun.security.pkcs11.SunPKCS11 p = new sun.security.pkcs11.SunPKCS11(configName);
    Security.addProvider(p);
    Builder builder = KeyStore.Builder.newInstance("PKCS11", p, new KeyStore.CallbackHandlerProtection(new UserInputDialog(new JDialog(),"test","test")));
    KeyStore ks = builder.getKeyStore();
    ks.load(null,null);
    KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", p);
    gen.initialize(1024);
    KeyPair kp = gen.generateKeyPair();
               Here access to token works. The callback PIN dialog comes up and i can login.
    But i'm not sure whether the key are generated on this PKCS11. And they are not stored there.
    How i can generate keys are stored there.
    (like with keytool -genkeys ). In keytool case a certificate is stored.
    ... every little hint, also to some documentation i've not seen, is very welcome ...
    Thank You !
    Regards
    Thomas
    .

    First, you need to get a KeyStore representation of the PKCS#11 token with code similar to this, I'm using NSS as the PKCS#11 token in this example:
    Provider nss = new sun.security.pkcs11.SunPKCS11(configFile);
    Security.insertProviderAt(nss, 1);  //you may not want it at highest priority
    KeyStore ks = KeyStore.getInstance("PKCS11", nss);
    ks.load(null, password);From the testing I've done in the past with various tokens, when you generate an asymmetric keypair (e.g. RSA like you are) specifying the PKCS11 provider, it creates it right on the token automatically and code like below is not needed.
    To store the key in the keystore, use code similar to this, I'm using NSS again and storing a symmetric key:
    KeyGenerator kg = KeyGenerator.getInstance("DESede",nss);
    SecretKey tripleDesKey = kg.generateKey();
    KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(tripleDesKey);
    ks.setEntry(randAlias, skEntry, new KeyStore.PasswordProtection(password));

  • Keychain not generating keys for email certificates

    In trying to set up email signing for two different machines I ran into a problem when adding email authentication certificates from Comodo.  After downloading the .p7s files each of the users double clicked the files, adding them to their key chains.  However, when they opened Mail there were no options for adding the lock(encrypt) and star(digitally sign) icons to their 'compose new message' windows.
    After a lot of screwing around, I discovered that the new certificates had been added, but just as regular certificates and never made it to the 'My certificates' section.  After some more comparisons I discovered that the private keys had not been generated automatically when the keys were added.  The solution was to send the origional files to a machine that was generating keys, add them to that machine's keychain and then export the certificates (this time with a .p12 extention) and re-import the keys back to the owners machines.
    That's a pain.  Anyone seen this before?  Have a better fix?
    Configs as follows
    Working configuration (generates keys)
    iMac 27" 3.4ghz Intel core i7
    Mac os 10.7.2
    keychain 5.0
    Broken configurations (not generating keys)
    Mac Mini 2.66 intell core duo
    Mac os 10.7.2
    keychain 5.0

    Hi Jack,
    Open Keychain Access in Utilities, use Keychain First Aid under the Keychain Menu item, then either check the Password under that item, change it, or delete it and start over.
    Resetting your keychain in Mac OS X...
    If Keychain First Aid finds an issue that it cannot repair, or if you do not know your keychain password, you may need to reset your keychain.
    http://support.apple.com/kb/TS1544

  • Support for Generated Keys in oracke jdbc drivers?

    Hi All,
    Anyone know when the Oracle Thin JDBC drivers are going to support generated keys?
    Regards,
    Lee

    Sure Justin,
    I should have made myself more clear. Sorry about that.
    I have a sequence for generating primary keys in a particular table, and a trigger to replace a null in my insert query with the nextval. That all works fine. I would like to use the DB-neutral means of getting a generated primary key...
    prepStmt = connection.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS);
    prepStmt.executeUpdate();
    ... and later ...
    ResultSet generatedKeyRS = prepStmt.getGeneratedKeys();
    ... etc.
    I am aware that I can get this information several other Oracle9i-specific ways, including using the sequence's .currval and creating a callable statement that returns the primary key column. However I would still like to use the nice, neat, vendor-neutral JDBCv3 way. Call me pedantic.
    Regards,
    Lee

  • Question about generate key press event to system. help plz.

    I am wondering if it is possible to write a java program to generate key press event(not receiving)to the local system so all the other program in the system receive those key press event also. Thanks.

    Hi,
    :. I don't know what exactly you intend to accomplish. But, I have used the following code in order to simulate keys pressing inside a Java application.
          /* - - - Simulates TAB (java.awt.Event)
          EventQueue evtq = Toolkit.getDefaultToolkit().getSystemEventQueue();
          evtq.postEvent( new KeyEvent(this, KeyEvent.KEY_PRESSED,
                          0, 0, KeyEvent.VK_TAB, KeyEvent.CHAR_UNDEFINED) );
          evtq.postEvent( new KeyEvent(this, KeyEvent.KEY_RELEASED,
                          0, 0, KeyEvent.VK_TAB, KeyEvent.CHAR_UNDEFINED) );
          /* - - - Simulates Shift+TAB (java.awt.Event)
          evtq.postEvent( new KeyEvent(this, KeyEvent.KEY_PRESSED, 0,
                          InputEvent.SHIFT_DOWN_MASK, KeyEvent.VK_TAB,
                          KeyEvent.CHAR_UNDEFINED) );
          evtq.postEvent( new KeyEvent(this, KeyEvent.KEY_RELEASED, 0,
                          InputEvent.SHIFT_DOWN_MASK, KeyEvent.VK_TAB,
                          KeyEvent.CHAR_UNDEFINED) );:. However, as far as I know to send keys to the whole OS you have to create a 'Hook' as described inside Win32API documentation. That's for Microsoft Windows naturaly.
    Cheers.
    Roque

  • Prioryly generated key problem.

    public String encrypt(String password){
    String algo = "DESede";
    Cipher cipher = Cipher.getInstance(algo);
    String s = null;
    Key key = (Key)com.sun.crypto.provider.DESedeKey@4f9655f1;
    cipher.init(Cipher.ENCRYPT_MODE, key);
         byte[] inputBytes = password.getBytes();
         s = cipher.doFinal(inputBytes);
    return s;
    public String decrypt(String password){
    String algo = "DESede";
    Cipher cipher = Cipher.getInstance(algo);
    String s = null;
    Key key = (Key)com.sun.crypto.provider.DESedeKey@4f9655f1;
    cipher.init(Cipher.DECRYPT_MODE, key);
         byte[] vas = cipher.doFinal(password);
         String s = new String(vas);
         return s;
    I got a problem with the above code the character @ in the object com.sun.crypto.provider.DESedeKey@4f9655f1 does not compile in java, but I need to use it because its my pre generated key that i wish to use in encryption and decryption function.
    Thanks for the help in advance.

    Wow. This one is...surreal. Let's see:
    As silk.m noted, what you're trying to do doesn't make any sense. You need to have the bytes of the Key in order to recreate it. Use generatedKey.getEncoded() to get the byte[] that describes the Key, and then look at how to use DESedeKeySpec and SecretKeyFactory to recreate the Key object.
    On top of that, you have other problems.
    You can't just use byte[] plaintext = s.getBytes(). The default String encoding is different on different platforms. You need to tell getBytes() which encoding you really want.
    You're attempting to return the output of doFinal() as a String. You can't - it's a byte[]. You can't just return "new String(doFinalBytes)", either - ciphertext IS NOT "String-able". You'll need to Base64 it first.
    It's also possible you'll be bitten by having a plaintext that isn't an integral number of blocks. I don't recall if the default DESede uses padding, or assumes NoPadding.
    You've got some fixing to do...
    Good luck,
    Grant

  • JPA: Availability of generated keys

    Hi
    If I create an entity which has an auto-generated key and call the persist method of the entity manager, can I then assume that the key is immediately available in the entity or only after a subsequent call of the flush method?
    In general, the synchronization with the datasource may be delayed by the application server until commit time of the associated transaction. But the immediate availability of auto-generated keys can be crucial in session beans with container-managed transactions if the corresponding creation methods want to make use of the keys.
    Unfortunately, I couldn't find any definitive answer in the JPA specification.
    Thanks for any help, Stephan

    I must be missing something. I'm calling persist on my entity but i'm not getting a value for my primary key. Then when I go to put it in a relationship the whole thing dies. I even tried using flush and that didn't help either.
    I'm essentially doing
    beginTransaction();
    EntityA a = new EntityA();
    persist(a);
    EntityB b = new EntityB();
    b.setA(a);
    persist(b);
    commit();Is there some annotation I need so that the primary key is set on persist ?

  • Generating Key for new installation

    Hi,
    I installing IDES , I am trying to generate key. the FYI steps i have involved
    goto SMSY>select other object>generate key==yes got the key
    When i entered the generated key from solution manager
    The system does not accepts.
    The error message read as<b>"HOST directory to central instance and system ID incorrect"
    Please help tell me basic requiremnts or procedures to follow.
    REGARDS,
    PAUL

    Hello Paul,
    Kindly follow the steps mentioned in the below link to generate the key...
    http://help.sap.com/saphelp_sm40/helpdata/en/45/51fbdbd4941803e10000000a1553f7/frameset.htm
    regards,
    Anand

  • Generate Key Events without pressing key???

    Hi,
    Is it possible to generate key events without pressing a key in swing???
    i dont know if the question is logical or not, but just i want to generate some display as if the key is pressed by the user
    Ashish

    assuming c represents a text field.
    This will type the character 'a' in the text field:
    c.dispatchEvent( new KeyEvent (c, KeyEvent.KEY_TYPED, 0, 0, KeyEvent.VK_UNDEFINED, 'a') );
    This will invoke Ctrl+F1, which will show the tool tip for the text field:
    c.dispatchEvent( new KeyEvent (c, KeyEvent.KEY_PRESSED, 0, KeyEvent.CTRL_MASK, KeyEvent.VK_F1) );

  • CF 9.0.2 and Oracle - On update returns error "Auto-generated keys were not requested..."

    We have a simple update statement to Oracle 11g Database. When running the statement the data is not getting updated and we are getting an error "Auto-generated keys were not requested, or the SQL was not a simple INSERT statement. ErrorCode=0 SQLState=HY000". We found this error by dumping the SQL to a file.
    But most other Update statements are working fine.
    Also, the same statment works for Oracle 10g and Coldfusion 9.0.0.
    Any idea if this is a problem with Coldfusion or Oracle? Is there any resolution.
    I found the CF 8 had a similar issue and was fixed in a hotfix (http://helpx.adobe.com/coldfusion/kb/error-auto-generated-keys-requested.html).

    Hi,
    Thanks. I compiled my code using JDeveloper 10.1.2, didn't dare to use the latest. It works in 10g apps server. When I deployed to 9ias apps server, those weird errors showed up. Unfornately, our dev environment is at a newer version than the production one.
    So, you think the error is generated because I referenced some newer technologies that was not provided by 9ias?
    Jia

  • Finding generated keys with batch updates

    Is it possible to combine reading generated keys with batch updates?
    In trying to improve performance of inserts into a database, I am using the addBatch functionality of the JDBC driver to insert several rows at once. Unfortunately some of my tables have auto numbered fields and I need to know what values they are taking so I can update my in-memory representation of the object in the database.
    The getGeneratedKeys functionality works fine when I insert one row at a time, but when I try to insert a batch of rows, the method returns null.
    I am using DB2 with the com.ibm.db2.jcc driver, though I have a hunch that this is a limitation of the JDBC spec. Can anyone confirm this? Does anyone have any ideas about a workaround?

    What is "several"?
    And have you actually measured the speed difference?
    Since returning generated keys is a rather recent addition to JDBC I doubt that it exists for batch updates.

  • Keytool generated keys portable to other platforms?

    I generated asymmetric keys using Java Keytool on Windows NT. Are the keys portable to Unix ? Also, can programs like Perl, ASP read these keys? Thank you.

    This is not completely correct. Key tool does not allow export of
    private keys. It is a real drag for developers.You no need to export anything. BTW, PKCS#12 keystore could be created by using keytool with appropriate JCE provider with PKCS#12 support. Then you can open it on other platform, that supports PKCS#12 (i.e. mozilla or msie web browsers can do it). For more detail on PKCS#12 refer to OpenSSL PKCS#12 FAQ. http://www.drh-consultancy.demon.co.uk/pkcs12faq.html

  • Generate key in ABAP

    Hi all,
    I need to generate a random key in order to insert a row in some table (SRGBTBREL).
    A key like that :'DEE01961112730F1BF77001A6431651A'
    Maybe there is already a function for that but have no clue
    How could I do it ?
    Thanks for answer !

    Hi,
    Try function module GUID_CREATE.
    /Christoffer

  • What will happen if clicked on the generate key in the security setting of SLD in SAP B1

    Hi
    I clicked on the  generate dynamic key in the security setting of SLD in SAP B1, now when i am trying to open companies , it is asking for update , how can i  reset it to previous step.

    Hi Isaac Kalii,
    Can you please tell me the database names that i need to restore or i need to restore all the databases.
    Thanks

  • 1.3, rel fields and automatically generated keys

    Hi
    I'm just starting to play with relationship fields and I'm working through the RosterApp example <http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/CMP3.html> trying to apply it to my application.
    It says I should define relationship fields like this:
    public abstract Collection getTeams();
    public abstract void setTeams(Collection teams);But my related table is indexed by an automatically generated primary key which I think we treat as an Object, don't we?
    So, should my relationship fields be:
    public abstract ProductType getProductType();
    public abstract void setProductType(Object ProductType);& if so, what protects me, besides my own skill and intelligence, from trying to setProductType with a String, for example, when what's actually being used for that key is an Integer.
    I mean, I guess we'll always setProductType with the results of a finder method or something so maybe I'm pondering about nothing. My question really is, is that the correct way to define a relationship field where the key is automatically generated.
    Also, Annie, are you paid by Sun to always be around answering these questions?
    Cheers anyway
    J

    But my related table is indexed by an automatically
    generated primary key which I think we treat as an
    Object, don't we?http://e-docs.bea.com/wls/docs61/ejb/cmp.html#1066165
    The key field must be declared to be of type java.lang.Integer in the abstract `get' and `set' methods of the bean. -- That's what I know for WebLogic. I am not sure about what other app servers do about this.
    for example, when what's actually being used
    for that key is an Integer.
    Also, Annie, are you paid by Sun to always be around
    answering these questions? :) Nope, but I get payed for posting on Java forums, since I am short of work and only conduct trainings for newbies joining the company. That's the best way I get to learn, rather than asking questions. And the best part is when I write something wrong and someone hits out at me; helps me a lot. ;)
    I hope this one helped you.
    ***Annie***

Maybe you are looking for