Okay. I have encrypt and decrypt file done. Let start at being I ask user for key and what file what they want to read or encrypt. And encrypt. For decrypt it same code. Now i have problem at let say the user enter 5 for key. How can i write code that find the key and understand that file is not encrypt any more . Anyone? I try for if while dowhile

Recommended Answers

All 16 Replies

What key??? Why do you need to find the key? If your encryption algorithm takes in a key, it should use the key in the encryption process. If one wants to decrypt and give a wrong key, the content from decryption should be garbage anyway.

The decision to encrypt/decrypt should be the user, not your program. Why do you want to do it automatically? Unless you have other purposes...

I needs to do it. Let someone give a encrypt file but dont me a key to decrypt it. I neeed to find key from 0 to 256 byte. To unlock it

If there are only 256 possible keys, just try each one in a loop until you get one that works.

I know but how do you detemind the code is having key right? Let say in txt file it have "hello there" and it encrypt and now let say the key is 5 but perten i didnot the key at all. I have for loop that go via all the ket number but how i do determind if the codw is in english and right key?

You will need to know something about the file so you can see if it decrypted correctly.
Many file types have a standard header ("magic number") at the start, eg pdf files start "%PDF", so you can look for that.
If you know it's an English text file then the decoded file should just contain ASCII characters in the range 00 - 7F, so if there are any chars > 7F it's not decoded right.

It should be txt only and correctly decrypt.

So it gonna be

Int ascii = 7f I am sure about this;
There will a for loop that will determine the index.
If (index == 00 < 7f)
(

Plus or minus file ascii key to key
Wrtite to file?
)
Countine do it until it find correct key

Is that how should work

I look at ascii key from 00 to 7f it contain some like /;()\<>= in setence you wont have that. I think I should check for that too

I would structure it like (pseudo code)

for (int key = 0 to 255) { // try every key
   String decoded = decode(encoded, key) // decode encrypted data using key
   if (isValidText(decoded)) {
      write decoded to file
      break;
   }
}

...

boolean isValidText(String d) {
   for each char in d
      if char is >127 return false // not ASCII text
   return true
}

But I need still need the pass reading from file to function right? Other question is when I cout right before it write it read to the file. I see some negative number.that mean is not char?

WHAT IF people encode only char there is not some wired char. My code will oh this chat and it write but code still not decode
Is there a way to check that?

I see some negative number.that mean is not char?

chars are alwaya positive - see tha Java Language Specification

WHAT IF people encode only char there is not some wired char

You can't solve that. If one key decodes it to 'A' and another key decodes it to 'z' there's no way to know which is correct.

Last questoion is !"':;/- and etc is a char tooo? Like you say in code if that is char write to file. If !":'/;?: is not char it will skip this I hope you understand what I mean

if char is >127

127? I throught you say is from 00 to 7f is only viald as char.

Java chars are 16 bit UniCode, ans include Hebrew, Arabic, Chinese... all kinds of symbols.
Ordinary English text can be encoded in just 7 bits (values 0 - 127 or 7f) using the ASCII codes. Java UniCode is the same as ASCII for up to 127 (hex 7f).

Oh i got it now. First I was thinking you say in bits. So i was think 7f couldnt be a bits. and I like how how could enter 7f into the code? lol thank you for example. I am complete understand now.

0x7F. Or just 127

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.