假设我有一个简单的文本,一杯不错的奶茶,它将与密钥12345进行XOR密码交换.
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
public class XORTest {
public static void main(String args[]){
String plaintext = "a nice cup of milk tea";
String key = "12345";
String encrypted = xor_encrypt(plaintext,key);
String decrypted = xor_decrypt(encrypted,key);
System.out.println("Encrypted: "+encrypted);
System.out.println("Decrypted: "+decrypted);
}
public static String xor_encrypt(String message,String key){
try {
if (message==null || key==null ) return null;
char[] keys=key.@R_502_294@arArray();
char[] mesg=message.@R_502_294@arArray();
BASE64Encoder encoder = new BASE64Encoder();
int ml=mesg.length;
int kl=keys.length;
char[] newmsg=new char[ml];
for (int i=0; i502_294@arArray();
message = new String(decoder.decodeBuffer(message));
char[] mesg=message.@R_502_294@arArray();
int ml=mesg.length;
int kl=keys.length;
char[] newmsg=new char[ml];
for (int i=0; i
给我:
Encrypted: UBJdXVZUElBBRRFdVRRYWF5YFEFUUw==
Decrypted: a nice cup of milk tea
PHP
$input = "a nice cup of milk tea";
$key = "12345";
$encrypted = XOR_encrypt($input,$key);
$decrypted = XOR_decrypt($encrypted,$key);
echo "Encrypted: " . $encrypted . "
给我:
Encrypted: MTIzNDUxMjM0NTEyMzQ1MTIzNDUxMg==
Decrypted:
不知道为什么两个结果都不同.
我必须承认,PHP不是我的一杯茶.
顺便说一下,我将它用于玩具项目,因此不需要高安全性.
最佳答案
原文链接:https://www.f2er.com/java/438255.html