javascript中的3des

今天需要在客户端实现des 很快就找到了 http://www.tero.co.uk/des/code.php
应用示例 
var key = "this is a 24 byte key !!";
var message = "This is a test message";
var ciphertext = des (key, message, 1, 0);
document.writeln ("DES Test: " + stringToHex (ciphertext));


当然发现这不是我们所需要的。我们服务端使用的是des-ede,java中好象叫triple des
这儿有个小坑 des方法本身支持3des
//how many iterations (1 for des, 3 for triple des)
  var iterations = key.length > 8 ? 3 : 1; //changed by Paul 16/6/2007 to use Triple DES for 9+ byte keys


但是对于key的使用有个小小的错误
如果是64位是des 正解,但是3des有可能是128位或192位
3des的过程是 en(k3,  de(k2, en(k1, P)))
其中k1=k3的时候可以使用简化的128位密钥。
key="k1k1k1k1k2k2k2k2k2" 其实与 k1k1k1k1k2k2k2k2k1k1k1k1应该是等价的。作者没有实现
另外一个就是padding模式,简单的说就是块加密的时候,不足的空位应该怎么补起来 下面是源码
if (padding == 2) message += "        "; //pad the message with spaces
  else if (padding == 1) {temp = 8-(len%8); message += String.fromCharCode (temp,temp,temp,temp,temp,temp,temp,temp); if (temp==8) len+=8;} //PKCS7 padding
  else if (!padding) message += "\0\0\0\0\0\0\0\0"; //pad the message out with null bytes


现在一般使用的是PKCS5/7
调用的注意一下这两点,其实des-ede也就实现了
示例
var key ="b964b7c58e99b59"+"b964b7c5";
var message = "This is a test message";
var ciphertext = des(key, message, 1, 0,0,1);
document.writeln("<br/>DES3 Test: " + stringToHex (ciphertext));


再次感谢原作者。再次声明出处
http://www.tero.co.uk/des/code.php
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值