 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu May 03, 2007 12:55 am Post subject: I want to save a Key in Mysql as String |
|
|
ok
so I'm trying to store a key for late for later use with a password
and i can get as far as saving and retrieving it. but can't use it
again
this is what i have:
KeyGenerator kg = KeyGenerator.getInstance("DES");
Key key = kg.generateKey();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] encrypted_password = cipher.doFinal(password.getBytes());
JOptionPane.showMessageDialog(null,"Encrypted Password "+ new
String(encrypted_password));
String coughtPassword = new String(encrypted_password);
cipher.init(Cipher.WRAP_MODE,key);
byte[] keyAsByte = cipher.wrap(key);
String keyAsString = new String(keyAsByte);
//coughtpassword and keyAsString are save in mySql by a Dao
// the piece i have been unable to do is here i want to reconstruct
keyAsString into the workable key2
//any suggestions are welcome
cipher.init(Cipher.DECRYPT_MODE,key2);
byte[] decrypted_password2 =
cipher.doFinal(coughtPassword.getBytes());
JOptionPane.showMessageDialog(null,"Decrypted Password "+new
String(decrypted_password2)); |
|
| Back to top |
|
 |
x x Guest
|
Posted: Fri May 04, 2007 7:12 am Post subject: Re: I want to save a Key in Mysql as String |
|
|
Never convert the binary byte[] (encrypted_password) into String.
You can use key.getEncoded() which prints an encoded string. Save it in the
database.
When you read the string, you can create the keyspecification using
EncodedKeySpec(byte[] encodedKey)
And Then, use SecretKeyFactory.generateSecret(KeySpec .....) to derive at
the Key.
Thanks,
Selva-
InfoTekies Corporation.
<iszekeell (AT) gmail (DOT) com> wrote in message
news:1178135700.773704.281480 (AT) l77g2000hsb (DOT) googlegroups.com...
| Quote: | ok
so I'm trying to store a key for late for later use with a password
and i can get as far as saving and retrieving it. but can't use it
again
this is what i have:
KeyGenerator kg = KeyGenerator.getInstance("DES");
Key key = kg.generateKey();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] encrypted_password = cipher.doFinal(password.getBytes());
JOptionPane.showMessageDialog(null,"Encrypted Password "+ new
String(encrypted_password));
String coughtPassword = new String(encrypted_password);
cipher.init(Cipher.WRAP_MODE,key);
byte[] keyAsByte = cipher.wrap(key);
String keyAsString = new String(keyAsByte);
//coughtpassword and keyAsString are save in mySql by a Dao
// the piece i have been unable to do is here i want to reconstruct
keyAsString into the workable key2
//any suggestions are welcome
cipher.init(Cipher.DECRYPT_MODE,key2);
byte[] decrypted_password2 =
cipher.doFinal(coughtPassword.getBytes());
JOptionPane.showMessageDialog(null,"Decrypted Password "+new
String(decrypted_password2));
|
|
|
| Back to top |
|
 |
Mike Amling Guest
|
Posted: Sat May 05, 2007 2:32 am Post subject: Re: I want to save a Key in Mysql as String |
|
|
iszekeell (AT) gmail (DOT) com wrote:
| Quote: | ok
so I'm trying to store a key for late for later use with a password
and i can get as far as saving and retrieving it. but can't use it
again
this is what i have:
KeyGenerator kg = KeyGenerator.getInstance("DES");
Key key = kg.generateKey();
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] encrypted_password = cipher.doFinal(password.getBytes());
JOptionPane.showMessageDialog(null,"Encrypted Password "+ new
String(encrypted_password));
|
What makes you think that encrypted_password is a legitimate encoding
of characters using the default character encoding?
| Quote: | String coughtPassword = new String(encrypted_password);
cipher.init(Cipher.WRAP_MODE,key);
byte[] keyAsByte = cipher.wrap(key);
String keyAsString = new String(keyAsByte);
|
What makes you think that encrypted_password is a legitimate encoding
of characters using the default character encoding?
Somebody did not read
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html, which
says "public String(byte[] bytes) ... The behavior of this constructor
when the given bytes are not valid in the default charset is
unspecified." That has always been true, and has been documented since
Java 1.4.
If you want to store arbitrary bytes as a String of characters, you
can convert them to hexadecimal:
final char[] keyAsHex=new char[keyAsBytes.length*2];
for (int jj=keyAsBytes.length, xx=keyAsHex.length; --jj>=0; ) {
final int oneByte=keyAsBytes[jj];
keyAsHex[--xx]="0123456789ABCDEF".charAt(oneByte & 0x0F);
keyAsHex[--xx]="0123456789ABCDEF".charAt((oneByte>>4) & 0x0F);
}
final String keyAsString=new String(keyAsHex);
| Quote: | //coughtpassword and keyAsString are save in mySql by a Dao
// the piece i have been unable to do is here i want to reconstruct
keyAsString into the workable key2
//any suggestions are welcome
cipher.init(Cipher.DECRYPT_MODE,key2);
byte[] decrypted_password2 =
cipher.doFinal(coughtPassword.getBytes());
JOptionPane.showMessageDialog(null,"Decrypted Password "+new
String(decrypted_password2));
|
|
|
| Back to top |
|
 |
Guest
|
Posted: Wed May 09, 2007 10:55 pm Post subject: Re: I want to save a Key in Mysql as String |
|
|
thanks man didn't know that bout string and thanks for showing me how
to store a Byte array as a String |
|
| Back to top |
|
 |
Mike Amling Guest
|
Posted: Thu May 10, 2007 11:32 pm Post subject: Re: I want to save a Key in Mysql as String |
|
|
iszekeell (AT) gmail (DOT) com wrote:
| Quote: | thanks man didn't know that bout string and thanks for showing me how
to store a Byte array as a String
|
You're welcome. Converting back is easy, too. If you know that keyHex
contains an even number of hexadecimal digits (0..9, A..F), then
String keyHex=...
byte[] keyBytes=new byte[keyHex.length()/2];
for (int jb=0, jh=0; jb<keyBytes.length; ++jb) {
keyBytes[jb]=
(("0123456789ABCDEF".pos(keyHex.charAt(jh++)) & 0x0F))<<4)+
("0123456789ABCDEF".pos(keyHex.charAt(jh++)) & 0x0F);
}
--Mike Amling |
|
| Back to top |
|
 |
Guest
|
Posted: Fri May 11, 2007 12:58 am Post subject: Re: I want to save a Key in Mysql as String |
|
|
kool thanks again,
i was wondering would you know of good reading material for
encryption in java. i have only started looking at encryption and it's
causing my more than one headache, like that byte[] to String section,
i'm going to try it with Blob as well think it might be simpler, but i
be really greatful if you could point me in the right direction in
relation to books thanks again!
Michael(Ben) |
|
| Back to top |
|
 |
Guest
|
Posted: Fri May 11, 2007 1:06 am Post subject: Re: I want to save a Key in Mysql as String |
|
|
x x
thank you very much for all your help it really helped with my
encrypion problems, still I just too much of a noob to encryption
thanks so much tho.
could to tell me how to construct a SecretKeyFactory (can't get my
head round provider class) or a link for such. thanks again
Michael(Ben) |
|
| Back to top |
|
 |
Mike Amling Guest
|
Posted: Fri May 11, 2007 4:05 am Post subject: Re: I want to save a Key in Mysql as String |
|
|
Mike Amling wrote:
| Quote: | iszekeell (AT) gmail (DOT) com wrote:
thanks man didn't know that bout string and thanks for showing me how
to store a Byte array as a String
You're welcome. Converting back is easy, too. If you know that keyHex
contains an even number of hexadecimal digits (0..9, A..F), then
String keyHex=...
byte[] keyBytes=new byte[keyHex.length()/2];
for (int jb=0, jh=0; jb<keyBytes.length; ++jb) {
keyBytes[jb]=
(("0123456789ABCDEF".pos(keyHex.charAt(jh++)) & 0x0F))<<4)+
("0123456789ABCDEF".pos(keyHex.charAt(jh++)) & 0x0F);
}
|
Hmm... The &0x0F is superfluous, it needs a cast and to use indexOf.
String keyHex=...
byte[] keyBytes=new byte[keyHex.length()/2];
for (int jb=0, jh=0; jb<keyBytes.length; ++jb) {
keyBytes[jb]=(byte)(
("0123456789ABCDEF".indexOf(keyHex.charAt(jh++))<<4)+
"0123456789ABCDEF".indexOf(keyHex.charAt(jh++)));
}
--Mike Amling |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|