Jump to content



Photo

help with DES and AES

java

  • Please log in to reply
48 replies to this topic

#1 Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 20 November 2012 - 03:44

hi I have posted something like this before but I am still tackling the problem....

I am trying to encrypt bytes and then add a second encryption then remove the first without removing the second.... I have my reasons for this I need help with this not an alternative....
the encryption can be alternative so DES + DES or AES + DES or AES + AES or anything thing else but it has to be like this ... here is my code so far .... I have got the encryption layers on ... its just getting them off im struggling with (one page test code)....im getting (given final block is not correctly padded)

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class ObjectCrypter {

public static void main(String[] argv) {

try {
 
String str = "moo";

byte[] byted = str.getBytes();
 
Cipher desCipher;
Cipher enCipher;

  KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
	 SecretKey myDesKey = keygenerator.generateKey();
	
	 desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
	
	 desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
	 byte[] textEd = desCipher.doFinal(byted);
	
	 System.out.println("DES?" + new String(textEd));
	
	
  
    byte[] byt = textEd;
	
   KeyGenerator keygenerat = KeyGenerator.getInstance("AES");
	  SecretKey myD = keygenerat.generateKey();
	 
	  enCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
	 
	  enCipher.init(Cipher.ENCRYPT_MODE, myD);
	  byte[] tex = enCipher.doFinal(byt);
	 
	  System.out.println("AES?" + new String(tex));
	
	 desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
	 byte[] textDecrypted = desCipher.doFinal(tex);
	 
	  System.out.println("it work?" + new String(textDecrypted));
	 
	 
	 
	 
	 
}catch(NoSuchAlgorithmException e){
  e.printStackTrace();
}catch(NoSuchPaddingException e){
  e.printStackTrace();
}catch(InvalidKeyException e){
  e.printStackTrace();
}catch(IllegalBlockSizeException e){
  e.printStackTrace();
}catch(BadPaddingException e){
  e.printStackTrace();
}
}

}

if you can help it would be great :)


#2 Salutary7

Salutary7

    Neowinian

  • Joined: 05-March 12

Posted 20 November 2012 - 03:51

So it looks like you encrypted with DES followed by AES. Shouldn't you decrypt with AES followed by DES?

#3 The_Decryptor

The_Decryptor

    STEAL THE DECLARATION OF INDEPENDENCE

  • Tech Issues Solved: 5
  • Joined: 28-September 02
  • Location: Sol System
  • OS: iSymbian 9.2 SP24.8 Mars Bar

Posted 20 November 2012 - 07:32

I'm not sure how this could work, by encrypting it with AES you can't then decrypt it with DES, as it's not in the format the DES algorithm is capable of decrypting.

It's like doing ROT13 then Base64 encoding it, then doing ROT13 again without decoding from Base64, it won't give the intended results.

#4 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 20 November 2012 - 15:19

So it looks like you encrypted with DES followed by AES. Shouldn't you decrypt with AES followed by DES?

I'm not sure how this could work, by encrypting it with AES you can't then decrypt it with DES, as it's not in the format the DES algorithm is capable of decrypting.

It's like doing ROT13 then Base64 encoding it, then doing ROT13 again without decoding from Base64, it won't give the intended results.


even when I encrypt with DES both times i still get the same result ...the thing is im trying to set up a safe way to send information where neither party has the others keys so encryptions needs to be removed in the same order they were added .... so if i encrypt the string "moo" ... with a passkey such as "hello" then encrypt the new string with the passkey "goodbye" I then need to remove the encryption "hello" after which i then remove the passkey goodbye .... :p get it?

#5 +theblazingangel

theblazingangel

    Software Engineer

  • Tech Issues Solved: 6
  • Joined: 25-March 04
  • Location: England, UK

Posted 20 November 2012 - 17:23

even when I encrypt with DES both times i still get the same result ...the thing is im trying to set up a safe way to send information where neither party has the others keys so encryptions needs to be removed in the same order they were added .... so if i encrypt the string "moo" ... with a passkey such as "hello" then encrypt the new string with the passkey "goodbye" I then need to remove the encryption "hello" after which i then remove the passkey goodbye .... :p get it?

That's completely illogical and will never work. You always have to decrypt in the opposite order of encryption.

#6 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 20 November 2012 - 18:05

That's completely illogical and will never work. You always have to decrypt in the opposite order of encryption.



well then maybe you can help me..... I need to create a p2p chat system with encryption.... BUT the encryption key obviously cant be in the source code or sent over the internet in plain text ..... any helps ?

#7 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 20 November 2012 - 19:22

well then maybe you can help me..... I need to create a p2p chat system with encryption.... BUT the encryption key obviously cant be in the source code or sent over the internet in plain text ..... any helps ?

Use asymmetric encryption. A popular asymmetric encryption protocol is SSH. The JsCH library seems like a popular Java implementation of SSH.

#8 n_K

n_K

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 19-March 06
  • Location: here.
  • OS: FreeDOS
  • Phone: Nokia 3315

Posted 20 November 2012 - 19:36

Dude what!
This and your other thread added together... Either you are NOT meant to be a programmer or you've currently got no clue how to be a programmer.
Write down your ideas and think if they're possible or not and try to plot how they would work.
Other thread: I want to make my program impossible for others to decode -> to run on a PC, it needs to be readable by a PC -> by being readable by a PC that also means people can 'read' it -> if I obscure the code will it help? And what downsides will doing that have, will it create bugs or slow down the running of my program and how much, and what will it cost to implement?

This thread: I want to make a secure chat program -> what encrytion will I use? -> What encryption types will I use? -> I do not want to use the same key to encrypt is so I will use asymmetrical encryption.

Etc. otherwise you will unfortunately keep making junk and be a pretty awful programmer.

EDIT: also reguarding SSH, it's a protocol not an encryption standard :p SSH can use various but you'd probably want to look up PGP, RSA etc.

#9 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 14
  • Joined: 26-October 05

Posted 20 November 2012 - 19:48

also reguarding SSH, it's a protocol not an encryption standard :p

... that's why I said it's a protocol. Btw I don't think your ridiculing of the OP is a productive way of helping him.

#10 n_K

n_K

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 19-March 06
  • Location: here.
  • OS: FreeDOS
  • Phone: Nokia 3315

Posted 20 November 2012 - 20:18

... that's why I said it's a protocol. Btw I don't think your ridiculing of the OP is a productive way of helping him.

I'm not ridiculing him, if you walk into your job as project manager of something with no plan, not only will your project never exist, you'll be fired.
Programming is no different.
If you just think 'yeh I`ll do this but no idea how or if its even possible' then you will never do that.

#11 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 20 November 2012 - 20:44

Dude what!
This and your other thread added together... Either you are NOT meant to be a programmer or you've currently got no clue how to be a programmer.
Write down your ideas and think if they're possible or not and try to plot how they would work.
Other thread: I want to make my program impossible for others to decode -> to run on a PC, it needs to be readable by a PC -> by being readable by a PC that also means people can 'read' it -> if I obscure the code will it help? And what downsides will doing that have, will it create bugs or slow down the running of my program and how much, and what will it cost to implement?

This thread: I want to make a secure chat program -> what encrytion will I use? -> What encryption types will I use? -> I do not want to use the same key to encrypt is so I will use asymmetrical encryption.

Etc. otherwise you will unfortunately keep making junk and be a pretty awful programmer.

EDIT: also reguarding SSH, it's a protocol not an encryption standard :p SSH can use various but you'd probably want to look up PGP, RSA etc.


actually .... I get As in all my programming exams and assignments..... there are just some things I have yet to do .... I have moved on to network programming .... please do not underestimate me.... I dislike you **** OFF

I'm not ridiculing him, if you walk into your job as project manager of something with no plan, not only will your project never exist, you'll be fired.
Programming is no different.
If you just think 'yeh I`ll do this but no idea how or if its even possible' then you will never do that.


you are you are taking the ****... im asking questions to help me understand .... I find programming easy but guess what I have to learn it in specific ways I understand .... not everyone speaks english the same just like not everyone programs the same now go annoy someone else troll

Use asymmetric encryption. A popular asymmetric encryption protocol is SSH. The JsCH library seems like a popular Java implementation of SSH.


thank you for your tips /i will have a look I understand that SSH is a secure protocol used for things such as VPNs and i am feeling it is a good idea to try and use that protocol...

#12 n_K

n_K

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 19-March 06
  • Location: here.
  • OS: FreeDOS
  • Phone: Nokia 3315

Posted 20 November 2012 - 23:58

See you're saying to use the SSH protocol... You wouldn't use the SSH protocol unless you're communicating with an SSH server or making an SSH client.
All I'm saying is take a step back from what you're doing and look at the overall picture.
I'm not trolling, if anyone's trolling it's you 'how do i hide my code'.

SMH, welcome to the real world.

#13 +SharpGreen

SharpGreen

    Now with built-in BS detector.

  • Tech Issues Solved: 1
  • Joined: 20-August 04
  • Location: North Carolina
  • OS: Ubuntu 14.04 and Windows 10
  • Phone: Galaxy Nexus

Posted 21 November 2012 - 00:06

See you're saying to use the SSH protocol... You wouldn't use the SSH protocol unless you're communicating with an SSH server or making an SSH client.
All I'm saying is take a step back from what you're doing and look at the overall picture.
I'm not trolling, if anyone's trolling it's you 'how do i hide my code'.

SMH, welcome to the real world.


Why can't a chat client and server communicate over SSH? SSH is (among other things) a protocol for secure data communication. There are no set limitations on what that data has to be.

#14 n_K

n_K

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 19-March 06
  • Location: here.
  • OS: FreeDOS
  • Phone: Nokia 3315

Posted 21 November 2012 - 00:11

As far as I'm aware SSH is a tunneling communications protocol from an SSH client to an SSH daemon (running on a *nix system) which uses public and private RSA and ESSID keys to encrypt and decrypt the data sent to and from it, so if you were to use ssh then you'd ssh to a ssh server and run a program on it... The hassle of doing all that I can't see being worth it when you can just setup PGP easily, send over the public key and just use PGP or RSA.

#15 The_Decryptor

The_Decryptor

    STEAL THE DECLARATION OF INDEPENDENCE

  • Tech Issues Solved: 5
  • Joined: 28-September 02
  • Location: Sol System
  • OS: iSymbian 9.2 SP24.8 Mars Bar

Posted 21 November 2012 - 03:46

While you could make a chat client run over SSH, it's not a good "fit". Something like TLS is a much better option (You aren't dealing with SSH semantics then, it's just something that encrypts and decrypts incoming and outgoing communication)