Skip to content

Instantly share code, notes, and snippets.

Created May 15, 2013 03:34
  • Star 55 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Use CryptoJS encrypt message by DES and direct decrypt ciphertext, compatible with Java Cipher.getInstance("DES")
<!-- test pass with CryptoJS v3.1.2 -->
<script src="rollups/tripledes.js"></script>
<script src="components/mode-ecb.js"></script>
* Encrypt message by DES in ECB mode and Pkcs7 padding scheme
* NOTE: DES is weak, please use 3DES(Triple DES) or AES
* @param {String} message
* @param {String} key
* @return {String} ciphertext(base64 string)
* @author Sun
* @version 2013-5-15
* @see <a href="">des encrypion: js encrypted value does not match the java encrypted value</a>
* In cryptoJS you have to convert the key to hex
* and useit as word just like above (otherwise it will be considered as passphrase)
* @see <a href="">C# and Java DES Encryption value are not identical</a>
* SunJCE provider uses ECB as the default mode,
* and PKCS5Padding as the default padding scheme for DES.(JCA Doc)
* This means that in the case of the SunJCE provider,
* Cipher c1 = Cipher.getInstance("DES/ECB/PKCS5Padding");
* and
* Cipher c1 = Cipher.getInstance("DES");
* are equivalent statements.
* @see <a href=""> Cannot find any provider supporting AES/ECB/PKCS7PADDING</a>
* I will point out that PKCS#5 and PKCS#7 actually specify exactly
* the same type of padding (they are the same!),
* but it's called #5 when used in this context. :)
function encryptByDES(message, key) {
// For the key, when you pass a string,
// it's treated as a passphrase and used to derive an actual key and IV.
// Or you can pass a WordArray that represents the actual key.
// If you pass the actual key, you must also pass the actual IV.
var keyHex = CryptoJS.enc.Utf8.parse(key);
// console.log(CryptoJS.enc.Utf8.stringify(keyHex), CryptoJS.enc.Hex.stringify(keyHex));
// console.log(CryptoJS.enc.Hex.parse(CryptoJS.enc.Utf8.parse(key).toString(CryptoJS.enc.Hex)));
// CryptoJS use CBC as the default mode, and Pkcs7 as the default padding scheme
var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
// decrypt encrypt result
// var decrypted = CryptoJS.DES.decrypt(encrypted, keyHex, {
// mode: CryptoJS.mode.ECB,
// padding: CryptoJS.pad.Pkcs7
// });
// console.log(decrypted.toString(CryptoJS.enc.Utf8));
// when mode is CryptoJS.mode.CBC (default mode), you must set iv param
// var iv = 'inputvec';
// var ivHex = CryptoJS.enc.Hex.parse(CryptoJS.enc.Utf8.parse(iv).toString(CryptoJS.enc.Hex));
// var encrypted = CryptoJS.DES.encrypt(message, keyHex, { iv: ivHex, mode: CryptoJS.mode.CBC });
// var decrypted = CryptoJS.DES.decrypt(encrypted, keyHex, { iv: ivHex, mode: CryptoJS.mode.CBC });
// console.log('encrypted.toString() -> base64(ciphertext) :', encrypted.toString());
// console.log('base64(ciphertext) <- encrypted.toString():', encrypted.ciphertext.toString(CryptoJS.enc.Base64));
// console.log('ciphertext.toString() -> ciphertext hex :', encrypted.ciphertext.toString());
return encrypted.toString();
* Decrypt ciphertext by DES in ECB mode and Pkcs7 padding scheme
* @param {String} ciphertext(base64 string)
* @param {String} key
* @return {String} plaintext
* @author Sun
* @version 2013-5-15
function decryptByDES(ciphertext, key) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
// direct decrypt ciphertext
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
}, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
return decrypted.toString(CryptoJS.enc.Utf8);
var message = 'Message';
var key = 'abcd1234';
var ciphertext = encryptByDES(message, key);
// ciphertext: 8dKft9vkZ4I='ciphertext:', ciphertext);
var plaintext = decryptByDES(ciphertext, key);
// plaintext : Message'plaintext :', plaintext);
Copy link

houhlin commented Aug 12, 2015


Copy link


Copy link

danmo commented Oct 5, 2017

Thanks, this is a great example!

Copy link

Yunfly commented Jan 23, 2018

why the key More than 9 characters will be different with JAVA

Copy link

good job, thx

Copy link

Very well done! Life Saver!

Copy link

nice work, Thanks a lot

Copy link

very good, Thanks a lot

Copy link

Thanks good work!

Copy link


Copy link

good job!

Copy link

jakazzy commented Jul 27, 2020


Copy link

perfect, inspiring!

Copy link

thank you. Replacing DES with AES works just as well and compatible with Java:

Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(message.getBytes());

Copy link

guguji5 commented Jan 6, 2023


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment