0

I'm making a program that can encrypt and decrypt text using various methods, for DES encryption, my program generates a key by doing

KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();

The string of text supplied is successfully encrypted, the key converted to string using the toString() method looks like something like this

com.sun.crypto.provider.DESKey@18738

However, when I supply the same key (the toString one), like this

byte[] decodedKey = com.sun.crypto.provider.DESKey@18738.getBytes();

it says "Invalid key length: 36 bytes" which is weird, because the same key was used to encrypt and decrypt the same string of text.

Here is how I encrypt:

    try {
 Cipher desCipher;
 byte[] decodedKey = pass.getText().getBytes();//the password is supplied here
 SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "DES");
 desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
 byte[] text = input.getText().getBytes();
 desCipher.init(Cipher.DECRYPT_MODE, originalKey);
 byte[] textDecrypted = desCipher.doFinal(input.getText().getBytes());
 output.setText(Arrays.toString(textDecrypted));
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException ex) {
 Error(ex.getMessage());
 Logger(ex.getMessage());
}

Here is how I decrypt:

            try {
                        Cipher desCipher;
                        byte[] decodedKey = pass.getText().getBytes();
                        SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "DES");
                        desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
                        byte[] text = input.getText().getBytes();
                        desCipher.init(Cipher.DECRYPT_MODE, originalKey);
                        byte[] textDecrypted = desCipher.doFinal(input.getText().getBytes());
                        output.setText(Arrays.toString(textDecrypted));
                    } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException ex) {
                        Error(ex.getMessage());
                        Logger(ex.getMessage());
                    }

For some reason, the code is poorly indented even though I used netbeans to fix it, probably because it's in like 10 switch blocks...

Thank you for your help!

Gamer818
  • 102
  • 11
  • Read about object.toString()!! You should use ` keygenerator.generateKey().getBytes()` – Jens Mar 09 '17 at 13:40
  • @Jens I can't find a getBytes method for generateKey() – Gamer818 Mar 09 '17 at 13:42
  • The key is an array of bytes, it's not a `String`, and it definitely is not the `String` you think it is in your question (see duplicate). – Kayaman Mar 09 '17 at 13:46
  • @Kayaman I don't really understand that other article, could you provide me with a clear example please? – Gamer818 Mar 09 '17 at 13:50
  • The duplicate is quite clear and thorough. Until you understand basic things like `toString()`, you should forget about trying more advanced things like cryptography. – Kayaman Mar 09 '17 at 13:53
  • @Kayaman well, the duplicate shows how to override the toString() method, but it don't see how I can use it to get the bytes of the key, I'm very confused. – Gamer818 Mar 09 '17 at 13:54
  • You shouldn't use it. That was just to explain that `"com.sun.crypto.provider.DESKey@18738"` is **not** your key. You can get the bytes from the key itself, if you start converting it to a `String` you're doing things wrong. – Kayaman Mar 09 '17 at 13:58
  • Then the duplicate doesn't answer my question, so it technically isn't a duplicate, isn't it? – Gamer818 Mar 09 '17 at 13:59
  • Well, it covers about 90% of the things that are wrong in your question. – Kayaman Mar 09 '17 at 14:00
  • It doesn't solve my question... do you know the answer to my question? – Gamer818 Mar 09 '17 at 14:02
  • @Kayaman Thank you for all the help you've given. – Gamer818 Mar 09 '17 at 14:08
  • Use `getEncoded()` to get the bytes from the key, and forget the Strings. – Kayaman Mar 09 '17 at 14:09
  • I am still getting invalid key lengths for some reason, you see, when I generate the key, I display the key in a text field, and it also tells the user that a key will be generated for this encryption, to display the key in a text field, I need to use the toString() method, so what I did was myDesKey.getEncoded().toString(), also, when I System.out.println(myDesKey.getEncoded) and I get the bytes, it still says invalid key length. – Gamer818 Mar 09 '17 at 14:16
  • These are exactly the issues which are discussed in the duplicate. You can't just go around calling `toString()` without knowing what kind of representation it returns. – Kayaman Mar 09 '17 at 14:19
  • @Kayaman I can get the hashcode from the object, but it shows as invalid key length, I got it by using println – Gamer818 Mar 09 '17 at 14:20
  • That's nice. But you still need to use the actual 64-bit `byte[]` instead of calculating hashcodes or calling `toString()`. See the duplicate on how to display arrays in more readable format. – Kayaman Mar 09 '17 at 14:22
  • Sooo I'm suppose to wrap myDesKey.getEncoded() in Arrays.toString? This is pretty hard for me... should I start searching up byte to 64 bit? – Gamer818 Mar 09 '17 at 14:27
  • If you want to display your key in a *reasonable* format, then yes. The `byte[]` returned by `getEncoded()` is 64-bits, i.e. 8 bytes (unsurprisingly), but you're definitely lacking a lot of rudimentary knowledge for someone who's trying to write cryptography related code. – Kayaman Mar 09 '17 at 14:33
  • Oh, I'm doing this for a community service project for my middle school, I'm raising money, my friend gave me this idea and I decided to make it. So do I use Base64 to convert the byte to 64 bits and see what happens? Or do I like search up custom made DES stuff? – Gamer818 Mar 09 '17 at 14:36
  • I told you already, the 64-bit key is returned from `getEncoded()`. You can convert it to Base64 for display purposes if you want to, however. – Kayaman Mar 09 '17 at 14:40
  • I tried using the Base64 encoded as the key, it looks like this "[B@52823724" aaand it still has the same problem... This is the hardest problem I've encountered in my programming career... – Gamer818 Mar 09 '17 at 14:51
  • You've managed to make this question 100% about the duplicate. Maybe it's time you read it until you understand what it's about? It's clear and well explained, I'm assuming you're a native English speaker, or at least possess a good enough understanding. Good luck. – Kayaman Mar 09 '17 at 14:55
  • Well, I'm off to bed, and again, thank you Kayaman, for all your help! I really appreciate it. I'll probably look back on all these comments in like a year and be like "this isn't as hard as I remembered" haha, thank you! – Gamer818 Mar 09 '17 at 14:56
  • Thanks, but I'm not a native English speaker and I'm not really that good at Java, thanks for the good luck. – Gamer818 Mar 09 '17 at 14:57

0 Answers0