I have the following CryptoJS code for encryption and decryption.
Encryption Code
<script type="text/javascript">
$(document).on("click", "#crack", function(evt) {
evt.preventDefault();
var e = $("#plaintext").val();
var key = $("#key").val();
var iv = CryptoJS.enc.Utf8.parse(key);
var r, t = CryptoJS.enc.Utf8.parse(key), // key
n = CryptoJS.DES.encrypt(CryptoJS.enc.Utf8.parse(e), t,{
iv:iv,
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
r = n.toString(CryptoJS.enc.Utf8);
$(".result").text(n);
});
</script>
Decryption Code
<script type="text/javascript">
$(document).on("click", "#crack", function(evt) {
evt.preventDefault();
var e = $("#ciphertext").val();
var key = $("#key").val();
var r, t = CryptoJS.enc.Utf8.parse(key), // key
n = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(e.replace('"', ""))
}, t, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
}),
r = n.toString(CryptoJS.enc.Utf8);
$(".result").text(r);
});
</script>
I want to recreate the same logic in Java but it's not quite working our well. I followed most of the existing questions which are already asked here on StackOverflow but it didn't help out.
I found another article and sample code which seemed like it would work but it didn't.
Article Link: https://androidfreetutorial.wordpress.com/2017/03/14/android-encryptiondecryption-with-tripledes-3des-algorithm/
I get the following error message:
java.security.NoSuchAlgorithmException: Cannot find any provider supporting DESede/ECB/PKCS7Padding
What am I doing wrong and how is CryptoJS code here is different from the Java one?
Sample Inputs:
Key:
CipherText: 3OwGE1y7zFLyPT49uGMvFQ==
Key: 75a1b1ae20af8de7bdfb6fac8ca9d36c0ab91930
EDIT: Added Working Code. Also the CryptoJS is Simple DES and not Triple DES as Suggested by @James. the application I was testing used tripledes.js in the resource file which mislead me to believe maybe Triple DES is used. I crossed checked that in CryptoJS if you want to use TripleDES you have to use something like CryptoJS.TripleDES.encrypt(...)
. Also, I forgot that DES Key size is 8 Bytes and the key that I gave wasn't being handled properly causing the bad padding exception, which I workedaround using Arrays.copyOf, and lastly Thanks to P.Soutzikevich.
The following code works exactly how I want it to be.
private final int DES_KEY_SIZE = 8;
private final String DES_MODE = "DES/ECB/PKCS5Padding";
Encryption
byte[] keyBytes = Arrays.copyOf(secretKey.getBytes(), DES_KEY_SIZE);
byte[] plainTextBytes = plainText.getBytes();
desKeySpec = new SecretKeySpec(keyBytes, "DES");
desCipher = Cipher.getInstance(DES_MODE);
desCipher.init(Cipher.ENCRYPT_MODE, desKeySpec);
byte[] encryptedText = desCipher.doFinal(plainTextBytes);
data = Base64.getEncoder().encodeToString(encryptedText);
Decryption:
byte[] keyBytes = Arrays.copyOf(secretKey.getBytes(), DES_KEY_SIZE);
byte[] encryptedTextBytes = Base64.getDecoder().decode(cipherText);
desKeySpec = new SecretKeySpec(keyBytes, "DES");
desCipher = Cipher.getInstance(DES_MODE);
desCipher.init(Cipher.DECRYPT_MODE, desKeySpec);
data = new String(desCipher.doFinal(encryptedTextBytes));