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?
我是密码学的新手。我希望学习如何加密和解密文件中的文本…当我在网上查阅相关文章时。我怀疑,当加密在同一文本上多次执行时,加密文本是否会与单个文本相同?有人能澄清我的疑问吗?
3 个解决方案
#1
22
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) {
}
#2
2
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,基于事实,哈希文本对于每个加密/散列是相同的。
#3
0
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_vector that 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.html and "Beginning Cryptography with Java" by David Hook. The last book is full of examples of usage of http://www.bouncycastle.org library.
我建议你阅读更多关于*的密码学,Bruce Schneier http://www.schneier.com/books.html和David Hook的“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.
如果你对密码学感兴趣,有一个密码工具:http://www.cryptool.org/ CrypTool是一个免费的、开源的电子学习应用程序,在全球范围内用于密码算法的实现和分析。
#1
22
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) {
}
#2
2
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,基于事实,哈希文本对于每个加密/散列是相同的。
#3
0
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_vector that 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.html and "Beginning Cryptography with Java" by David Hook. The last book is full of examples of usage of http://www.bouncycastle.org library.
我建议你阅读更多关于*的密码学,Bruce Schneier http://www.schneier.com/books.html和David Hook的“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.
如果你对密码学感兴趣,有一个密码工具:http://www.cryptool.org/ CrypTool是一个免费的、开源的电子学习应用程序,在全球范围内用于密码算法的实现和分析。