使用 SHA1 加密
我正在开发一个大型应用程序,当数据在不同大陆的两台机器之间传输时,我需要加密。我从来没有研究过加密。我想要一个简单的加密,可以在 PHP / Ruby / Python 中处理,无需任何依赖。
所以我决定使用 HMAC SHA1。
$pad=hash_hmac("sha1","The quick brown....","mykey");
这是我在互联网上进行一些研究后发现的。
如果有人不知道密钥,解密它有多困难?另外,还有什么替代方案吗?
更新 - 感谢您的所有回复。问题解决了。
I am developing a large application and i need encryption when a data is traveling between two machines in different continents. I have never worked on encryption. I want a simple encryption which can be handled in PHP / Ruby / Python without any dependencies.
So i decided to use HMAC SHA1.
$pad=hash_hmac("sha1","The quick brown....","mykey");
This is what i found out after some research on the internet.
How hard it is to decrypt it if someone doesn't know the key? Also, any alternatives to this?
UPDATE - thanks for all the responses. Problem solved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
即使你知道密钥,也不可能解密它。 HMAC SHA1 是一种密钥哈希算法,而不是加密。
哈希是一种加密单向函数,无论输入的长度如何,它总是生成相同长度的值(我认为 SHA1 是 128 位)。哈希的要点是,给定输出值,在计算上无法找到输入值来产生该输出。键控哈希用于防止彩虹表攻击。即使您知道密钥,也无法逆转哈希过程。
对于加密,您需要查看 AES。
It's impossible to decrypt it, even if you know the key. HMAC SHA1 is a keyed hash algorithm, not encryption.
A hash is a cryptographic one-way function that always generates a value of the same length (I think SHA1 is 128-bits) regardless of the length of the input. The point of a hash is that, given the output value, it's computationally infeasible to find an input value to produce that output. A keyed hash is used to prevent rainbow table attacks. Even if you know the key you can't reverse the hash process.
For encryption you want to look at AES.
SHA1 是一种单向哈希函数,根据定义,任何人都无法解密。问题是,如果你有一个散列到 H 的明文 T。找到另一个也散列到 H 的 T 有多难。
根据 Wikipedia,对于 SHA1,最著名的暴力攻击需要 2^51 次评估才能找到一个匹配的纯文本。
如果您需要可以逆转该过程的实际加密,您应该看看 AES256。
看:
http://en.wikipedia.org/wiki/Cryptographic_hash_function
对此进行一般性讨论。
SHA1 is a one-way-hash function, by definition it is not decryptable by anyone. The question becomes if you have a plaintext T that hashes to H. How hard is it to find another T which also hashes to H.
According to Wikipedia, for SHA1, the best known brute force attack would take 2^51 evlautions to find a plain text that matches.
If you need actual encryption where you can reverse the process, you should take a look at AES256.
See:
http://en.wikipedia.org/wiki/Cryptographic_hash_function
For a general discussion on this.
就像Andrew所说的SHA1是一种哈希算法,不能用于加密(因为你无法取回原始值)。它生成的摘要可用于验证数据的完整性。
HMAC 是接受密钥的哈希算法之上的构造。然而,它并不是用于加密(同样它无法解密),但它允许您签名数据,即使用相同的密钥,您将能够确保数据不被篡改在它的传输过程中。
您应该考虑使用 AES 进行加密,或者如果适用于您的应用程序,则使用 HTTPS(这将处理比您想知道的更多问题;-)
Like Andrew said SHA1 is an hash algorithm and cannot be used for encryption (since you cannot get back the original value). The digest it produce can be used to validate the integrity of the data.
An HMAC is a construct above an hash algorithm that accept a key. However it's not for meant for encryption (again it can't be decrypted) but it allows you to sign the data, i.e. with the same key you'll be able to ensure the data was not tampered with during it's transfer.
Foe encryption you should look at using AES or, if applicable to your application, HTTPS (which will deal with more issues than you want to know about ;-)
SHA-1、MD-5都是单向哈希算法。
他们只是生成一个很长的字符串。每个字符串在执行这些函数时都会产生一个无法保留的冗长字符串。
它们离加密还很远。
如果您正在寻找加密算法,请选择 AES(高级加密标准)、DES(数据加密标准)算法。
SHA-1 , MD-5 are all one way Hashing algorithms.
They just generate a lengthy string. Each and every string when subjected to these functions will yield you a lengthy string which cannot be retained back.
They are far from encryptions.
If you are looking for encryption algorithms , go for AES (Advanced Encryption Standard) , DES (Data Encryption Standard) Algorithms.
正如我所说,这是一个哈希值,所以不是加密/解密问题。如果您想实现简单的加密算法,我建议您研究异或加密。如果密钥足够长(比消息长)并且您的密钥共享策略足够安全,那么这是一次性一密本;否则,它可能会被统计分析所破坏。
As I say, this is a hash, so not an encryption/decryption problem. If you want to implement a straightforward encryption algorithm, I would recommend looking into XOR encryption. If the key is long enough (longer than the message) and your key sharing policy is suitably secure, this is a one time pad; otherwise, it can potentially be broken using statistical analysis.