问题描述
我需要一个尽可能短而快的代码来将String更改为不可读的(对于人类而言),并使其再次可读。这一切都需要在java中发生。
I need an as short and fast as possible code to change a String to something unreadable (for humans that is), and also to make it readable again. It all needs to happen in java.
这样的事情:
encrypt("test");
会产生如下结果:
ôT¿ÄÜTV CÁˆ"5="ËÂÀœššbÀß{¡ä³
和
decrypt("ôT¿ÄÜTV CÁˆ"5 1="ËÂÀœššbÀß{¡ä³");
将再次导致原始
test
我应该去哪个方向,是否有课程那可以帮我吗?我不是指像Base68Encryption或其他任何东西,我的意思是一个真正难以理解的文本,我可以安全地通过互联网发送。
What direction should I go, are there any classes that can do this for me? I don't mean something like Base68Encryption or whatever it might be called, I mean a true unreadable text that I can safely send over the internet.
推荐答案
这是使用真正加密的一个非常简短的例子。它是128位AES,非常安全 - 在任何想象中都无法读取。
Here is a very short example of using true encryption. It's 128 bit AES, which is farily secure - certainly not readable by any stretch of the imagination.
它生成一个随机密钥,因此每次运行都会有所不同。您需要在两个以某种方式交换数据的程序之间共享密钥。
It generates a random key, so it would be different on each run. You would need to share they key between the two programs exchanging data somehow.
private static final String ENCRYPTION_ALGORITHM = "AES/ECB/PKCS5Padding";
private static final SecureRandom RANDOM = new SecureRandom();
public static void main(String[] args) throws UnsupportedEncodingException, GeneralSecurityException {
final KeyGenerator keyGen = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM.substring(0, ENCRYPTION_ALGORITHM.indexOf('/')));
keyGen.init(128, RANDOM);
final SecretKey key = keyGen.generateKey();
final String s = "My topsecret string";
System.out.println(s);
final Cipher encryption = getCipher(key, Cipher.ENCRYPT_MODE);
final String enc = DatatypeConverter.printBase64Binary(encryption.doFinal(s.getBytes("UTF-8")));
System.out.println(enc);
final Cipher decryption = getCipher(key, Cipher.DECRYPT_MODE);
final String dec = new String(decryption.doFinal(DatatypeConverter.parseBase64Binary(enc)), "UTF-8");
System.out.println(dec);
}
private static Cipher getCipher(final Key key, final int mode) throws GeneralSecurityException {
final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
cipher.init(mode, key, RANDOM);
return cipher;
}
输出:
My topsecret string
ip4La5KUBJGTTYenoE920V5w0VBHwALv4fp3qyLTY9o=
My topsecret string
这篇关于java - 使字符串不可读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!