0

Ok, so I have been messing around with encryption/decryption in java and the encryption I have created uses non-printable/special characters. I then have made an entry in the Registry so that I have the options to Encrypt or Decrypt files in the Windows Context Menu when you right click a file. It encrypts both the file contents, and the file name. Windows has no problem showing these file names but when I try to decrypt the file I get errors and FileNotFoundExceptions because the command prompt puts things like ?s in place of the non-printable characters. So how do I pass these file names/characters to my jar file so it can decrypt the file?

This is the batch file that the windows context menu Decrypt button calls to decrypt the file:

java -jar "E:\IdeaProjects\Encryption\out\artifacts\Encryption_jar2\DecryptContext.jar" "%1"

Code converting to Base64

private String encodeBase64(String s) {
    return new String(Base64.getEncoder().encode(s.getBytes()));
}

Used then to get:

String newFileName = encodeBase64(encryptedString);
file.renameTo(new File(newFileName + ".txt"));
Soggy_Toast
  • 57
  • 1
  • 9
  • 1
    If you use a real application rather than a batch file, you can get the Unicode version of the command line. But there are certain characters that are illegal in file names, so you should probably encode the encrypted filename using, e.g., one of the Base64 variants. – Harry Johnston Oct 10 '17 at 03:20
  • @HarryJohnston This appears to work when I test the encryption with basic strings and not doing anything to the file, as it appears to get rid of all the non-printable characters, but now for whatever reason when I try to call `file.renameTo(new File(newName + ".txt"))` it returns `false`. Any reason why the Base64 String would prevent renaming the file? – Soggy_Toast Oct 10 '17 at 04:31
  • @HarryJohnston It is also worth noting that the output I get for an encrypted string converted to Base64 in my IDE is different than what I get in a command prompt. I.e the output from IDE vs Command prompt: `IDE: 1bvOs9OxxYQ=` vs `Cmd: Pz8/Pw==` – Soggy_Toast Oct 10 '17 at 04:49
  • You'll need to use a variant that doesn't use the forward slash, since that's one of the illegal characters. I don't know why you're getting different outputs, you'd need to post the code and other details. More likely to be a problem with the encryption than with the Base64 encoding, though. – Harry Johnston Oct 10 '17 at 05:40
  • This probably depends on what you're doing to create the context menu. When I add a batch script as a simple "shell" command to "HKCU\Software\Classes\*", CMD is of course executed with and uses a Unicode command line, and `echo %*` in the batch script works fine if the filename has characters that aren't in my ANSI or OEM codepage. – Eryk Sun Oct 10 '17 at 10:48
  • @eryksun I added it to "HKEY_CLASSES_ROOT\*\shell" under the folder "\My Encrypt\command" and the default value for command is "batchfilepath "%1"". The batch file is then what is shown above. – Soggy_Toast Oct 10 '17 at 22:19
  • @HarryJohnston I added the code where I convert to Base64. Would you like to see the code where I get all the different non-printable characters from as well? – Soggy_Toast Oct 10 '17 at 22:22
  • You mean the command is like `"Path\to\batchScript.bat" "%1"`, right? The shell will use the Unicode name of the target as "%1" and call `CreateProcessW`, which will rewrite the command line (depending on the `ComSpec` environment variable) as, for example, `"C:\Windows\System32\cmd.exe /c "Path\to\batchScript.bat" "Path\to\targetFile"`. Then CMD will parse the "Path\to\targetFile" as its `%1` parameter variable. – Eryk Sun Oct 10 '17 at 22:34
  • 1
    Of course if the batch file simply has this single line, you'd be better off configuring the context menu command to run Java directly. – Eryk Sun Oct 10 '17 at 22:37

0 Answers0