java中的简单加密-无密钥/密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29226813/
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
Simple encryption in java-no key/password
提问by Mayukh Nair
Say I have an IP address, 192.168.1.1
假设我有一个 IP 地址,192.168.1.1
I want my program to create a random one-word string based on this IP address which can be easily decrypted without a key or password or additional security.
我希望我的程序基于此 IP 地址创建一个随机的单字字符串,无需密钥或密码或额外的安全性即可轻松解密。
eg.
例如。
I enter 192.168.1.1
Program converts it to AzlQrEHCSD or some other random string
I enter this string in the program
It gets converted back to 192.168.1.1
我输入 192.168.1.1
程序将其转换为 AzlQrEHCSD 或其他一些随机字符串
我在程序中输入这个字符串
它被转换回 192.168.1.1
Is there any simple algorithm that can do this without generating stuff like keys or additional passwords? I understand that keys and passwords are a mustfor encryption and decryption, but my scenario does not require it.
是否有任何简单的算法可以在不生成密钥或额外密码之类的东西的情况下做到这一点?我知道密钥和密码是加密和解密的必备条件,但我的场景不需要它。
采纳答案by Kiki
I know its overkill but i would use jasyptlibrary since its realy easy to use. All you need is random seed to encrypt or decrpyt.
我知道它的矫枉过正,但我会使用jasypt库,因为它真的很容易使用。您只需要随机种子来加密或解密。
Here is the source code for encrypting data:
这是加密数据的源代码:
String seed = "ipNumber";
String myIpValue = "192.168.0.1";
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword(seed);
String encrypted= encryptor.encrypt(myIpValue);
And for data decryption:
对于数据解密:
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword(seed);
String decrypted = encryptor.decrypt(encrypted);
Or you could just encode or decode your string to base64 example is show here: Base64 Java encode and decode a string
或者您可以将您的字符串编码或解码为 base64 示例如下所示: Base64 Java encode and decode a string
回答by GhostCat
If the generated string would be "random", then your application would have to keep track of any generated string for all time. Probably not a good design.
如果生成的字符串是“随机的”,那么您的应用程序必须始终跟踪任何生成的字符串。可能不是一个好的设计。
A fast poor man "encryption" would be ROT47 ( http://rot47.net/)
一个快速的穷人“加密”将是 ROT47 ( http://rot47.net/)
回答by higuaro
You can encode the ip String
to base64
, reverse the string, and then use a Caesar cipher:
您可以将 ip 编码String
为base64
,反转字符串,然后使用凯撒密码:
public String easeyEncrypt(String ip) {
String b64encoded = Base64.getEncoder().encode(ip);
// Reverse the string
String reverse = new StringBuffer(b64encoded).reverse().toString();
StringBuilder tmp = new StringBuilder();
final int OFFSET = 4;
for (int i = 0; i < reverse.length(); i++) {
tmp.append(reverse.charAt(i) + OFFSET);
}
return tmp.toString();
}
To decrypt procede backwards:
要解密,请向后进行:
public String easeyDecrypt(String secret) {
StringBuilder tmp = new StringBuilder();
final int OFFSET = 4;
for (int i = 0; i < secret.length(); i++) {
tmp.append(secret.charAt(i) - OFFSET);
}
String reversed = new StringBuffer(tmp.toString()).reverse().toString();
return Base64.encode(reversed);
}
回答by utkal patel
This is simplest encrypt/decrypt code but it is not secure.
这是最简单的加密/解密代码,但并不安全。
String strip = "192.168.1.11";
byte[] encryptArray = Base64.encodeBase64(strip.getBytes());
String encstr = new String(encryptArray,"UTF-8");
System.out.println("Enc >> "+ encstr);
String strDec = "MTkyLjE2OC4xLjEx";
byte[] dectryptArray = strDec.getBytes();
byte[] decarray = Base64.decodeBase64(dectryptArray);
String decstr = new String(decarray,"UTF-8");
System.out.println("Dec >>> "+ decstr);
For this you have to import org.apache.commons.codec.binary.Base64;
为此你必须 import org.apache.commons.codec.binary.Base64;
you can Download org-apache-commons-codec.jarfile from Link
您可以从链接下载 org-apache-commons-codec.jar文件
回答by Al-Mothafar
Almost the same as higuaro
solutions but with a lot of fixes to make it work, the following code tested and working since higuaro
not working well like characters went into numbers and when you reverse its get single number and damage everything:
几乎与higuaro
解决方案相同,但有很多修复以使其工作,以下代码经过测试和工作,因为higuaro
不能像字符进入数字一样正常工作,并且当您反转它时会得到单个数字并损坏所有内容:
public String caesarCipherEncrypt(String plain) {
String b64encoded = Base64.getEncoder().encodeToString(plain.getBytes());
// Reverse the string
String reverse = new StringBuffer(b64encoded).reverse().toString();
StringBuilder tmp = new StringBuilder();
final int OFFSET = 4;
for (int i = 0; i < reverse.length(); i++) {
tmp.append((char)(reverse.charAt(i) + OFFSET));
}
return tmp.toString();
}
To de-crypt procede backwards:
要解密,请向后进行:
public String caesarCipherDecrypte(String secret) {
StringBuilder tmp = new StringBuilder();
final int OFFSET = 4;
for (int i = 0; i < secret.length(); i++) {
tmp.append((char)(secret.charAt(i) - OFFSET));
}
String reversed = new StringBuffer(tmp.toString()).reverse().toString();
return new String(Base64.getDecoder().decode(reversed));
}
I hope its helpful.
我希望它有帮助。
回答by Ibrahim.H
Here is a simple random encryption:
这是一个简单的随机加密:
class SimpleStringEncryption {
public static String encrypt(String str){
int code;
String result = "";
for (int i = 0; i < str.length(); i++) {
code = Math.round((float) Math.random()*8+1);
result += code + Integer.toHexString( ((int) str.charAt(i) ) ^ code )+"-";
}
return result.substring(0, result.lastIndexOf("-"));
}
public static String decrypt(String str){
str = str.replace("-", "");
String result = "";
for (int i = 0; i < str.length(); i+=3) {
String hex = str.substring(i+1, i+3);
result += (char) (Integer.parseInt(hex, 16) ^ (Integer.parseInt(String.valueOf(str.charAt(i)))));
}
return result;
}
public static void main (String[] args) {
String e = "some text to encrypt";
String encrypted = encrypt(e);
System.out.println(encrypted);
System.out.println(decrypt(encrypted));
}
}
hope this helps.
希望这可以帮助。