-2

I have DES Encryption Algorithm implementation in JAVA (javax.crypto.Cipher), it is successfully encoding and decoding (most) strings... the problem is that, sometimes, it message specific blocks (since DES uses 8-character blocks in block mode).

In my case, almost always the 3rd block is messed up and rest shows fine.

for example:

key: thisiskey

message to encrypt: Google is an American multinational technology company specializing in Internet-related services

encrypted message (in UTF-8):

mñqè•ÀPŒ�øf"
ߦ\±õ¤ù'È9¢ëyT ÍQEÁ|;ëâÉ÷JWú

Now, when i go and decrypt this, i get this:

Decrypted message:

Google i,í\O¯‹Ýbº-¸�¬ltinational technology company specializHôJ—=ÊÍnternet-related services

As far as i understand the issue, it is due to the fact that UTF-8 CANNOT show all characters and thus, while showing as well as copying for decryption, this problem occurs.

Can anyone suggest me a solution? Preferably, either a character-set that can handle this, or, a way to convert Binary directly to HEX (that can be output to user) and then Vice Versa (decrypted, after copying/pasting) in JAVA.

EDIT This is 'approximate' code, not exact (for example encrypted message is not properly paste-able and these are parts of the function, but it should give the idea). Even in base64 encoding , i am unable to get this decrypted properly.

Encrypt Function code:

boolean base64 = true;
key = "thisiskey";
plainText = "Google is an American multinational technology company specializing in Internet-related services";

SecretKeyFactory MyKeyFactory = SecretKeyFactory.getInstance("DES");
byte[] keyBytes = key.getBytes();

DESKeySpec generatedKeySpec = new DESKeySpec(keyBytes);
SecretKey generatedSecretKey = MyKeyFactory.generateSecret(generatedKeySpec);

Cipher generatedCipher = Cipher.getInstance("DES");
generatedCipher.init(Cipher.ENCRYPT_MODE, generatedSecretKey);

byte[] messsageStringBytes = plainText.getBytes();
byte[] encryptedMessage = generatedCipher.doFinal(messsageStringBytes);

String encryptedMessageString = new String(encryptedMessage);

if (base64) {
    encryptedMessageString = Base64.getEncoder().encodeToString(encryptedMessageString.getBytes("utf-8"));
}

return encryptedMessageString;

Decrypt Function code:

boolean dbase64 = true;
dkey = "thisiskey";
messageToDecrypt = "mñqè•ÀPŒ�øf\"ߦ\±õ¤ù'È9¢ëyT ÍQEÁ|;ëâÉ÷JWú"; // Message from above code

SecretKeyFactory MyKeyFactory = SecretKeyFactory.getInstance("DES");
byte[] dkeyBytes = dkey.getBytes();

DESKeySpec generatedKeySpec = new DESKeySpec(dkeyBytes);
SecretKey generatedSecretKey = MyKeyFactory.generateSecret(generatedKeySpec);

Cipher generatedCipher = Cipher.getInstance("DES");
generatedCipher.init(Cipher.DECRYPT_MODE, generatedSecretKey);

if (dbase64) {
    byte[] decodedBytes = Base64.getDecoder().decode(dencryptedText);
    dencryptedText = new String(decodedBytes, "utf-8");
}

byte[] messsageStringBytes = dencryptedText.getBytes();
byte[] encryptedMessage = generatedCipher.doFinal(messsageStringBytes);

String decryptedMessageString = new String(encryptedMessage);

return decryptedMessageString;
i333
  • 13
  • 8
  • How could you possibly expect anyone to be able to help you without your code? – Luke Joshua Park Oct 24 '16 at 00:43
  • 1
    DES is a great encryption algorithm—if security is not required. DES has been superseded by AES, use AES. Please don't create bad security, we have enough already. – zaph Oct 24 '16 at 01:48

1 Answers1

2

"Encrypted message in UTF-8" makes no sense. The ciphertext is binary and not UTF-8. You need to put it into a byte[], not a String.

If you need a String, use Base64 or Hex encoding.


Even in base64 encoding , i am unable to get this decrypted properly.

String encryptedMessageString = new String(encryptedMessage);

if (base64) {
  encryptedMessageString =  Base64.getEncoder().encodeToString(encryptedMessageString.getBytes("utf-8"));
}

That does not work. You are encoding to Base64 after the data is already broken (by calling new String). Do not put it in a String at all. Go directly from encryptedMessage (the byte[]) to Base64.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • So, how do you suggest i 'show' the text for using/copying? can you help a bit please? – i333 Oct 24 '16 at 00:49
  • 1
    If you need a String, use Base64 or Hex encoding. See the linked threads for details. – Thilo Oct 24 '16 at 00:51