java异或加密解密
时间: 2023-11-13 17:57:30 浏览: 48
Java中的异或加密解密可以使用位运算符^来实现。具体实现方法如下:
加密:
```java
public static String encrypt(String str, String key) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
sb.append((char) (str.charAt(i) ^ key.charAt(i % key.length())));
}
return sb.toString();
}
```
解密:
```java
public static String decrypt(String str, String key) {
return encrypt(str, key); // 异或加密解密是对称的,解密就是加密
}
```
其中,str是要加密/解密的字符串,key是密钥。
相关问题
java异或加密解密器
好的,关于Java异或加密解密器,我可以给你一些基本的信息。异或加密是一种简单的加密方式,它使用异或运算对明文进行加密,再使用同样的密钥对密文进行解密。在Java中,可以使用位运算符^来进行异或运算。
下面是一个简单的Java异或加密解密器的示例代码:
```java
public class XorEncryptor {
private static final String KEY = "secret_key"; // 密钥
// 加密
public static String encrypt(String plainText) {
char[] chars = plainText.toCharArray();
char[] keys = KEY.toCharArray();
for (int i = 0; i < chars.length; i++) {
chars[i] ^= keys[i % keys.length];
}
return new String(chars);
}
// 解密
public static String decrypt(String cipherText) {
char[] chars = cipherText.toCharArray();
char[] keys = KEY.toCharArray();
for (int i = 0; i < chars.length; i++) {
chars[i] ^= keys[i % keys.length];
}
return new String(chars);
}
}
```
使用示例:
```java
String plainText = "Hello, world!";
String cipherText = XorEncryptor.encrypt(plainText);
System.out.println(cipherText); // 输出:\u0007\u001d\u001d\u0004\u0015\u001c\u001d\u0004\u0015\u001c\u001d\u0004\u0015
String decryptedText = XorEncryptor.decrypt(cipherText);
System.out.println(decryptedText); // 输出:Hello, world!
```
java用异或加密解密文件
以下是Java使用异或方式对文件进行加密解密的示例代码:
加密:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class XorEncryptor {
public static void encrypt(File inputFile, File outputFile, String key) throws IOException {
FileInputStream inputStream = new FileInputStream(inputFile);
FileOutputStream outputStream = new FileOutputStream(outputFile); byte[] buffer = new byte[1024];
int len;
int index = 0;
while ((len = inputStream.read(buffer)) != -1) {
for (int i = 0; i < len; i++) {
buffer[i] ^= key.charAt(index % key.length());
index++;
}
outputStream.write(buffer, 0, len);
}
inputStream.close();
outputStream.close();
}
}
```
解密:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class XorDecryptor {
public static void decrypt(File inputFile, File outputFile, String key) throws IOException {
FileInputStream inputStream = new FileInputStream(inputFile);
FileOutputStream outputStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int len;
int index = 0;
while ((len = inputStream.read(buffer)) != -1) {
for (int i = 0; i < len; i++) {
buffer[i] ^= key.charAt(index % key.length());
index++;
}
outputStream.write(buffer, 0, len);
}
inputStream.close();
outputStream.close();
}
}
```
其中,encrypt方法用于加密文件,decrypt方法用于解密文件。key为加密解密的密钥。
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)