admin管理员组

文章数量:1393052

I am trying to encrypt given plain text by Triple DES algorithm with CBC mode, but unable to get Desired Cipher Text which i have given below.

I need this encryption and decryption by CryptoJs using Javascript, but unable to do this.

Plain Text: "AN=1234567890;[email protected];TS=2009-06-03 17:04:39" Desired Cipher Text: "LK0Y/C2GWAfVguTEwFBIzlLBKsp4v61yHCUTj0iKtxcG60Hzprx/zlCofYHsjYWnJt78ao9l6cA7TE fIl+u0QA=="

Key = "1234567890ABCDEFGHIJKLMN"

IV = "12345678"

Mode: "CBC"

Algorithm : "TripleDES"

Javascript Code:

<!DOCTYPE html>
<html>
<head>
<title>Testing Encryption & Decryption</title>
<script src=".3.1/jquery.min.js"></script>
<script src=".1.2/ponents/core.js"> 
</script>
<script src=".1.2/ponents/enc-base64.js"></script>
<script src=".1.2/rollups/tripledes.js"> 
</script>
<script src=".1.2/ponents/mode-ecb.js"></script>
<script src=".1.2/ponents/pad-nopadding.js"></script>
<script type="text/javascript">
var key = 'E821752166E916AEEF940855';
//CBC Mode encryption 
function encryptByDESModeCBC(message) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var ivHex = 'E8217521';//CryptoJS.enc.Utf8.parse(key);
console.log(ivHex);
encrypted = CryptoJS.DES.encrypt(message, keyHex, {
iv:ivHex,
mode: CryptoJS.mode.CBC
}
);
return encrypted.ciphertext.toString(); 
//CBC Schema decryption 
function decryptByDESModeCBC(ciphertext2) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var ivHex = CryptoJS.enc.Utf8.parse(key);
// direct decrypt ciphertext
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Hex.parse(ciphertext2)
}, keyHex, {
iv:ivHex,
mode: CryptoJS.mode.CBC
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
function test(){
 var source = $("#source").val();
 var cc = encryptByDESModeCBC(CryptoJS.enc.Utf8.parse(source));
 $("#target").val(cc);
}
function test1(){
var source = $("#sourceS").val();
var dd = decryptByDESModeCBC(source);
$("#jiemi").val(dd);
}
</script>
</head>
<body>
  <div>
    Before encryption :<textarea id="source" value="" 
     style="width:500px;height:90px;" /></textarea>
   <hr>
  After encryption :<textarea id="target" value="" 
  style="width:500px;height:90px;" ></textarea>
  <hr>
  <input type="button" onclick="test();" name="" value=" encryption " />
  <hr>
  ciphertext :<textarea id="sourceS" value="" width="400px" 
  style="width:500px;height:90px;" ></textarea>
  <hr>
  After decryption :<textarea id="jiemi" value="" 
  style="width:500px;height:90px;" ></textarea>
  <hr>
  <input type="button" onclick="test1();" name="" value=" Decrypt "/>
  </div>
  </body>
  </html>

I am trying to encrypt given plain text by Triple DES algorithm with CBC mode, but unable to get Desired Cipher Text which i have given below.

I need this encryption and decryption by CryptoJs using Javascript, but unable to do this.

Plain Text: "AN=1234567890;[email protected];TS=2009-06-03 17:04:39" Desired Cipher Text: "LK0Y/C2GWAfVguTEwFBIzlLBKsp4v61yHCUTj0iKtxcG60Hzprx/zlCofYHsjYWnJt78ao9l6cA7TE fIl+u0QA=="

Key = "1234567890ABCDEFGHIJKLMN"

IV = "12345678"

Mode: "CBC"

Algorithm : "TripleDES"

Javascript Code:

<!DOCTYPE html>
<html>
<head>
<title>Testing Encryption & Decryption</title>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://cdn.bootcss./crypto-js/3.1.2/ponents/core.js"> 
</script>
<script src="http://cdn.bootcss./crypto-js/3.1.2/ponents/enc-base64.js"></script>
<script src="https://cdn.bootcss./crypto-js/3.1.2/rollups/tripledes.js"> 
</script>
<script src="https://cdn.bootcss./crypto-js/3.1.2/ponents/mode-ecb.js"></script>
<script src="https://cdn.bootcss./crypto-js/3.1.2/ponents/pad-nopadding.js"></script>
<script type="text/javascript">
var key = 'E821752166E916AEEF940855';
//CBC Mode encryption 
function encryptByDESModeCBC(message) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var ivHex = 'E8217521';//CryptoJS.enc.Utf8.parse(key);
console.log(ivHex);
encrypted = CryptoJS.DES.encrypt(message, keyHex, {
iv:ivHex,
mode: CryptoJS.mode.CBC
}
);
return encrypted.ciphertext.toString(); 
//CBC Schema decryption 
function decryptByDESModeCBC(ciphertext2) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var ivHex = CryptoJS.enc.Utf8.parse(key);
// direct decrypt ciphertext
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Hex.parse(ciphertext2)
}, keyHex, {
iv:ivHex,
mode: CryptoJS.mode.CBC
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
function test(){
 var source = $("#source").val();
 var cc = encryptByDESModeCBC(CryptoJS.enc.Utf8.parse(source));
 $("#target").val(cc);
}
function test1(){
var source = $("#sourceS").val();
var dd = decryptByDESModeCBC(source);
$("#jiemi").val(dd);
}
</script>
</head>
<body>
  <div>
    Before encryption :<textarea id="source" value="" 
     style="width:500px;height:90px;" /></textarea>
   <hr>
  After encryption :<textarea id="target" value="" 
  style="width:500px;height:90px;" ></textarea>
  <hr>
  <input type="button" onclick="test();" name="" value=" encryption " />
  <hr>
  ciphertext :<textarea id="sourceS" value="" width="400px" 
  style="width:500px;height:90px;" ></textarea>
  <hr>
  After decryption :<textarea id="jiemi" value="" 
  style="width:500px;height:90px;" ></textarea>
  <hr>
  <input type="button" onclick="test1();" name="" value=" Decrypt "/>
  </div>
  </body>
  </html>
Share Improve this question asked Apr 3, 2018 at 5:37 Anuj K DwivediAnuj K Dwivedi 291 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Just use CryptoJS's TripleDes method (CryptoJS.TripleDES.encrypt) to encrypt. Should do the work. This matches what you want as you mentioned in your question.

Make sure to parse as UTF-8.

let data = "AN=1234567890;[email protected];TS=2009-06-03 17:04:39"
let key = "1234567890ABCDEFGHIJKLMN"
let IV = "12345678"
let cipher = CryptoJS.TripleDES.encrypt(data, CryptoJS.enc.Utf8.parse(key), {
  iv: CryptoJS.enc.Utf8.parse(IV),
  mode: CryptoJS.mode.CBC
});

console.log(cipher.toString())
<script src="https://cdnjs.cloudflare./ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>

You can try this

<!DOCTYPE html>
<html>
<head>
<title>Testing Encryption & Decryption</title>
<script src="https://ajax.googleapis./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://cdn.bootcss./crypto-js/3.1.2/ponents/core.js"> 
</script>
<script src="http://cdn.bootcss./crypto-js/3.1.2/ponents/enc-base64.js"></script>
<script src="https://cdn.bootcss./crypto-js/3.1.2/rollups/tripledes.js"> 
</script>
<script src="https://cdn.bootcss./crypto-js/3.1.2/ponents/mode-ecb.js"></script>
<script src="https://cdn.bootcss./crypto-js/3.1.2/ponents/pad-nopadding.js"></script>

</head>
<body>
  <div>
    Before encryption :<textarea id="source" value="" 
     style="width:500px;height:90px;" /></textarea>
   <hr>
  After encryption :<textarea id="target" value="" 
  style="width:500px;height:90px;" ></textarea>
  <hr>
  <!-- <input type="button" onclick="test();" name="" value=" encryption " />
   -->
  <input type="button" id="js-encypt" name="" value=" encryption " />
  <hr>
  ciphertext :<textarea id="sourceS" value="" width="400px" 
  style="width:500px;height:90px;" ></textarea>
  <hr>
  After decryption :<textarea id="jiemi" value="" 
  style="width:500px;height:90px;" ></textarea>
  <hr>
   <input type="button" id="js-decrypt" name="" value=" Decrypt "/>
<!--   <input type="button" onclick="test1();" name="" value=" Decrypt "/>
 -->  </div>
  </body>
  <script type="text/javascript">
var key = 'E821752166E916AEEF940855';
//CBC Mode encryption 

function encryptByDESModeCBC(message) {
    var ivHex = 'E8217521';//CryptoJS.enc.Utf8.parse(key);
    console.log(ivHex);
    var keyHex = CryptoJS.enc.Utf8.parse(key);
    var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    console.log(encrypted.toString());
    return encrypted.toString();

}


function decryptByDESModeCBC(ciphertext) {
    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);
}



$(document).on('click','#js-encypt',function(){
   var source = $("#source").val();
   var cc = encryptByDESModeCBC(CryptoJS.enc.Utf8.parse(source));
   $("#target").val(cc);
});

$(document).on('click','#js-decrypt',function(){
 var source = $("#sourceS").val();
 var dd = decryptByDESModeCBC(source);
 $("#jiemi").val(dd);
})

</script>
  </html>

本文标签: CryptoJsTripleDES encryption and Decryption with CBC Mode using JavascriptStack Overflow