Skip to content

Instantly share code, notes, and snippets.

Embed
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>
<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="https://groups.google.com/d/msg/crypto-js/I378fq3esK8/HZ2P2Xtuzk8J">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="http://stackoverflow.com/questions/12894722/c-sharp-and-java-des-encryption-value-are-not-identical">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="http://stackoverflow.com/questions/10193567/java-security-nosuchalgorithmexception-cannot-find-any-provider-supporting-aes">java.security.NoSuchAlgorithmException: 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=
console.info('ciphertext:', ciphertext);
var plaintext = decryptByDES(ciphertext, key);
// plaintext : Message
console.info('plaintext :', plaintext);
</script>
@obullxl

This comment has been minimized.

Copy link

obullxl commented Aug 20, 2014

Good job.

@leozzyzheng

This comment has been minimized.

Copy link

leozzyzheng commented Jun 17, 2015

Nice work.

@houhlin

This comment has been minimized.

Copy link

houhlin commented Aug 12, 2015

good

@ajimenezg

This comment has been minimized.

Copy link

ajimenezg commented Oct 23, 2015

Excellent..!!

@danmo

This comment has been minimized.

Copy link

danmo commented Oct 5, 2017

Thanks, this is a great example!

@Yunfly

This comment has been minimized.

Copy link

Yunfly commented Jan 23, 2018

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

@Young1993

This comment has been minimized.

Copy link

Young1993 commented Jun 27, 2018

good job, thx

@IEnoobong

This comment has been minimized.

Copy link

IEnoobong commented Oct 2, 2018

Very well done! Life Saver!

@wuzhefang

This comment has been minimized.

Copy link

wuzhefang commented Nov 2, 2018

nice work, Thanks a lot

@MahdadGhasemian

This comment has been minimized.

Copy link

MahdadGhasemian commented Nov 1, 2019

very good, Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.