在java中加密和解密一个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4487525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Encrypt and decrypt a String in java
提问by deepa
I am new to cryptography. I wish to learn how to encrypt and decrypt the text in a file... when I refer the related articles in net. I had a doubt that whether the encrypted text will be same for single text when encryption is done multiple times on the same text? Can anyone please clear my doubt?
我是密码学的新手。我想学习如何加密和解密文件中的文本……当我参考 net 中的相关文章时。我怀疑当对同一文本进行多次加密时,单个文本的加密文本是否相同?任何人都可以请清除我的疑问吗?
采纳答案by Pratik
public String encrypt(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (javax.crypto.BadPaddingException e) {
} catch (IllegalBlockSizeException e) {
} catch (UnsupportedEncodingException e) {
} catch (java.io.IOException e) {
}
return null;
}
public String decrypt(String str) {
try {
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
} catch (javax.crypto.BadPaddingException e) {
} catch (IllegalBlockSizeException e) {
} catch (UnsupportedEncodingException e) {
} catch (java.io.IOException e) {
}
return null;
}
}
Here's an example that uses the class:
这是一个使用该类的示例:
try {
// Generate a temporary key. In practice, you would save this key.
// See also Encrypting with DES Using a Pass Phrase.
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
// Create encrypter/decrypter class
DesEncrypter encrypter = new DesEncrypter(key);
// Encrypt
String encrypted = encrypter.encrypt("Don't tell anybody!");
// Decrypt
String decrypted = encrypter.decrypt(encrypted);
} catch (Exception e) {
}
回答by Ralph
I had a doubt that whether the encrypted text will be same for single text when encryption done by multiple times on a same text??
我有一个疑问,当对同一文本进行多次加密时,单个文本的加密文本是否会相同?
This depends strongly on the crypto algorithm you use:
这在很大程度上取决于您使用的加密算法:
- One goal of some/most (mature) algorithms is that the encrypted text is different when encryption done twice. One reason to do this is, that an attacker how known the plain and the encrypted text is not able to calculate the key.
- Other algorithm (mainly one way crypto hashes) like MD5 or SHA based on the fact, that the hashed text is the same for each encryption/hash.
- 一些/大多数(成熟)算法的一个目标是加密两次时加密的文本是不同的。这样做的一个原因是,攻击者如何知道明文和加密文本无法计算出密钥。
- 其他算法(主要是一种加密散列方式),如 MD5 或 SHA,基于这样一个事实,即每个加密/散列的散列文本都是相同的。
回答by Micha? Niklas
Whether encrypted be the same when plain text is encrypted with the same key depends of algorithm and protocol. In cryptography there is initialization vector IV: http://en.wikipedia.org/wiki/Initialization_vectorthat used with various ciphers makes that the same plain text encrypted with the same key gives various cipher texts.
明文用相同的密钥加密时加密是否相同取决于算法和协议。在密码学中,有初始化向量 IV:http: //en.wikipedia.org/wiki/Initialization_vector,它与各种密码一起使用,使得使用相同密钥加密的相同纯文本给出各种密文。
I advice you to read more about cryptography on Wikipedia, Bruce Schneier http://www.schneier.com/books.htmland "Beginning Cryptography with Java" by David Hook. The last book is full of examples of usage of http://www.bouncycastle.orglibrary.
我建议您在 Wikipedia、Bruce Schneier http://www.schneier.com/books.html和 David Hook 的“Beginning Cryptography with Java”上阅读有关密码学的更多信息。最后一本书充满了http://www.bouncycastle.org库的使用示例。
If you are interested in cryptography the there is CrypTool: http://www.cryptool.org/CrypTool is a free, open-source e-learning application, used worldwide in the implementation and analysis of cryptographic algorithms.
如果您对密码学感兴趣,可以使用 CrypTool:http: //www.cryptool.org/ CrypTool 是一个免费的开源电子学习应用程序,在全球范围内用于密码算法的实现和分析。