加密http请求返回结果

因为发现有些其他偷懒的开发者可能盗用我们restful服务器的api接口,所以对response进行加密是一个不错的解决方法。

##HTTPS 也许你马上会想到用https,但是我们可以通过在wikipedia上发现有这么一段对https的解释(我们都知道https是基于TLS的):

The TLS protocol allows client-server applications to communicate across a network in a way designed to prevent eavesdropping and tampering.

所以可以从文中看出https是为了防止篡改监听请求的。这里所说的监听指的是通过对response进行截取并且重新打包发送的技术。所以说https并不能够阻止数据的获取和解析。

##AES 在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。

###安全性 截至2006年,针对AES唯一的成功攻击是旁道攻击。美国国家安全局审核了所有的参与竞选AES的最终入围者(包括Rijndael),认为他们均能够满足美国政府传递非机密文件的安全需要。2003年6月,美国政府宣布AES可以用于加密机密文件:

The design and strength of all key lengths of the AES algorithm (i.e., 128, 192 and 256) are sufficient to protect classified information up to the SECRET level. TOP SECRET information will require use of either the 192 or 256 key lengths. The implementation of AES in products intended to protect national security systems and/or information must be reviewed and certified by NSA prior to their acquisition and use.1

(译:AES加密算法(使用128,192,和256比特密钥的版本)的安全性,在设计结构及密钥的长度上俱已到达保护机密信息的标准。最高机密信息的传递,则至少需要192或256比特的密钥长度。用以传递国家安全信息的AES实现产品,必须先由国家安全局审核认证,方能被发放使用。)

###nodejs实现

var crypto = require('crypto');

var data = "156156165152165156156";
console.log('Original cleartext: ' + data);
var algorithm = 'aes-128-ecb';
var key = '78541561566';
var clearEncoding = 'utf8';
var iv = "";
//var cipherEncoding = 'hex';
//If the next line is uncommented, the final cleartext is wrong.
var cipherEncoding = 'base64';
var cipher = crypto.createCipheriv(algorithm, key,iv);

var cipherChunks = [];
cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
cipherChunks.push(cipher.final(cipherEncoding));
console.log(cipherEncoding + ' ciphertext: ' + cipherChunks.join(''));

var decipher = crypto.createDecipheriv(algorithm, key,iv);
var plainChunks = [];
for (var i = 0;i < cipherChunks.length;i++) {
    plainChunks.push(decipher.update(cipherChunks[i], cipherEncoding, clearEncoding));

}
plainChunks.push(decipher.final(clearEncoding));
console.log("UTF8 plaintext deciphered: " + plainChunks.join(''));

###android实现

AES解密在android中是自带的,调用方法如下:

private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {    
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");    
    Cipher cipher = Cipher.getInstance("AES");    
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);    
    byte[] decrypted = cipher.doFinal(encrypted);    
    return decrypted;    
}    

###性能

High speed and low RAM requirements were criteria of the AES selection process. Thus AES performs well on a wide variety of hardware, from 8-bit smart cards to high-performance computers.

On a Pentium Pro, AES encryption requires 18 clock cycles per byte,[35] equivalent to a throughput of about 11 MB/s for a 200 MHz processor. On a 1.7 GHz Pentium M throughput is about 60 MB/s.

On Intel Core i3/i5/i7 CPUs supporting AES-NI instruction set extensions, throughput can be over 700 MB/s per thread.[36]

wiki

对于古董奔腾都能够有60MB/s的解码速度,相信足以应对大部分文本形式的请求

http://www.biubiubiu.me/httpqing-qiu-de-jia-mi/

转载于:https://my.oschina.net/u/236164/blog/274776

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值