Java简单加密
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1354803/
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
Java simple encryption
提问by
I would like to encrypt a textual (configuration) file stored on disk.
Trying to use DESencryption I've had fatal error on client machines, I later found out the algorithm could not handle accented characters (!)
I suspect that was because I was using old packages (sun.misc.BASE64Decoder
) - but I'm not sure that is the reason.
我想加密存储在磁盘上的文本(配置)文件。尝试使用DES加密时,我在客户端机器上遇到了致命错误,后来我发现该算法无法处理重音字符 (!) 我怀疑这是因为我使用的是旧包 ( sun.misc.BASE64Decoder
) - 但我不确定这是原因。
However, I'm looking for a simpler solution - I need a really simple encryption (I know some people would not agree on that) - not RSAof 128 bit keys or so, just obscuring the text from curious eyes.
但是,我正在寻找一个更简单的解决方案 - 我需要一个非常简单的加密(我知道有些人不会同意这一点) - 不是128 位密钥左右的RSA,只是从好奇的眼睛中遮挡文本。
It is really weird that I could not find a simple trivial solution on the web.
我在网上找不到一个简单的琐碎解决方案真的很奇怪。
How can I implement a simple encryption scheme?
如何实现简单的加密方案?
回答by rogeriopvl
How about ROT13? It's probably the most simple and worst encryption ever (it was also called the Caeser's Cipher)
ROT13怎么样?这可能是有史以来最简单和最糟糕的加密(它也被称为 Caeser 的密码)
Here's a basic implementation in Java by Jay Kominek:
这是 Jay Kominek 在 Java 中的基本实现:
import java.io.*;
public class rot13 {
public static void main (String args[]) {
int abyte = 0;
try { while((abyte = System.in.read())>=0) {
int cap = abyte & 32;
abyte &= ~cap;
abyte = ((abyte >= 'A') && (abyte <= 'Z') ? ((abyte - 'A' + 13) % 26 + 'A') : abyte) | cap;
System.out.print(String.valueOf((char)abyte));
} } catch (IOException e) { }
System.out.flush();
}
}
回答by oxbow_lakes
If you have a single piece of text to encrypt, what about a one-time-pad? A one-time-pad is very easy to create; all you need is a random sequence of bytes the same length as the data you are encrypting
如果您有一段文本要加密,那么一次性密码呢?一次性纸板很容易创建;您所需要的只是一个与您要加密的数据长度相同的随机字节序列
回答by Yuval
If you're not looking to really encrypt the text, why not encode with Base64? It'll look like nonsense, and it's very easy to decode. Plus, you're already using Base64 code...
如果您不想真正加密文本,为什么不使用 Base64 进行编码?它看起来像废话,而且很容易解码。另外,您已经在使用 Base64 代码...
回答by Eugene Yokota
See How do I use 3des encryption/decryption in Java?BASE64Encoder
is used to represent the ciphered array of bytes, not the actual input.
请参阅如何在 Java 中使用 3des 加密/解密?BASE64Encoder
用于表示加密的字节数组,而不是实际输入。
回答by SLaks
Encryption algorithms work on raw bytes, not characters.
加密算法处理原始字节,而不是字符。
The reason you couldn't handle accented characters was because the code you were using to convert the characters to and from raw bytes didn't handle Unicode.
您无法处理重音字符的原因是您用于将字符与原始字节相互转换的代码无法处理 Unicode。
You should use AES; for an example of how to use it in Java, see here.
你应该使用AES;有关如何在 Java 中使用它的示例,请参见此处。
EDIT: Right now, you might just be hiding it from curious eyes, but there's no telling what the future will hold, and it is always muchbetter to use strong encryption now and not find out, to late, that you should have but didn't.
编辑:现在,你可能只是在好奇的眼睛里隐藏它,但不知道未来会怎样,现在使用强加密总是要好得多,而且很晚才发现你应该有但没有'不。
回答by Mads Hansen
Check out the Java Simplified Encryption (Jasypt).
Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.
- High-security, standards-based encryption techniques, both for unidirectional and bidirectional encryption. Encrypt passwords, texts, numbers, binaries...
- Transparent integration with Hibernate.
- Suitable for integration into Spring-based applications and also transparently integrable with ACEGI (Spring Security).
- Integrated capabilities for encrypting the configuration of applications (i.e. datasources).
- Open API for use with any JCE provider.
- ...and much more
Jasypt 是一个 Java 库,它允许开发人员以最少的努力为他/她的项目添加基本的加密功能,而无需深入了解密码学的工作原理。
- 高安全性、基于标准的加密技术,适用于单向和双向加密。加密密码、文本、数字、二进制文件......
- 与 Hibernate 的透明集成。
- 适用于集成到基于 Spring 的应用程序中,也可以与 ACEGI (Spring Security) 透明集成。
- 用于加密应用程序(即数据源)配置的集成功能。
- 用于任何 JCE 提供程序的开放 API。
- ...以及更多
回答by yegor256
I'm using this simple One-Time-Padalgorithm:
我正在使用这个简单的One-Time-Pad算法:
import org.apache.commons.codec.binary.Base64;
public class Cipher {
private static final String KEY = "some-secret-key-of-your-choice";
public String encrypt(final String text) {
return Base64.encodeBase64String(this.xor(text.getBytes()));
}
public String decrypt(final String hash) {
try {
return new String(this.xor(Base64.decodeBase64(hash.getBytes())), "UTF-8");
} catch (java.io.UnsupportedEncodingException ex) {
throw new IllegalStateException(ex);
}
}
private byte[] xor(final byte[] input) {
final byte[] output = new byte[input.length];
final byte[] secret = this.KEY.getBytes();
int spos = 0;
for (int pos = 0; pos < input.length; ++pos) {
output[pos] = (byte) (input[pos] ^ secret[spos]);
spos += 1;
if (spos >= secret.length) {
spos = 0;
}
}
return output;
}
Don't forget to add commons-codec
to classpath.
不要忘记添加commons-codec
到类路径。