Mad*_*sen 22
查看Java Simplified Encryption(Jasypt).
Jasypt是一个java库,它允许开发人员以最小的努力为他/她的项目添加基本的加密功能,而无需深入了解密码学的工作原理.
- 高安全性,基于标准的加密技术,适用于单向和双向加密.加密密码,文本,数字,二进制文件......
- 与Hibernate透明集成.
- 适合集成到基于Spring的应用程序中,并且可以与ACEGI(Spring Security)透明地集成.
- 用于加密应用程序配置(即数据源)的集成功能.
- Open API,用于任何JCE提供程序.
- ...以及更多
我正在使用这个简单的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;
}
Run Code Online (Sandbox Code Playgroud)
不要忘记添加commons-codec到classpath.
ROT13怎么样?这可能是有史以来最简单和最糟糕的加密(也称为凯撒密码)
下面是 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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33218 次 |
| 最近记录: |