异或加密解密字符串
程序员文章站
2022-03-27 08:52:27
异或加密字符串/** * @Description 加密版本号(与“EtopliveVersion”密钥做异或运算) * @Author wangli * @Date 2020/9/5 15:04 **/public class XORCrypto { private byte[] keyBytes; // 密钥 private int k; public XORCrypto(String key) { keyBytes = key.getBytes()...
异或加密字符串
/**
* @Description 加密版本号(与“EtopliveVersion”密钥做异或运算)
* @Author wangli
* @Date 2020/9/5 15:04
**/
public class XORCrypto {
private byte[] keyBytes; // 密钥
private int k;
public XORCrypto(String key) {
keyBytes = key.getBytes();
k = keyBytes.length;
}
private String coding(String message) {
byte[] origin = message.getBytes();
byte[] master = new byte[origin.length];
for (int i = 0, len = origin.length; i < len; i++) {
master[i] = (byte) (origin[i] ^ keyBytes[i % k]);
}
return new String(master);
}
public String encoding(String plaintext) {
return coding(plaintext);
}
public String decoding(String cipherText) {
return coding(cipherText);
}
public static void main(String[] args) throws Exception {
String name = "V2.1.2.200526";//加密字符串
String key = "EtopliveVersion";//异或字符串秘钥
XORCrypto crypto = new XORCrypto(key);
String cipherText = crypto.encoding(name); // 将name加密成密文
String plaintext = crypto.decoding(cipherText); // 解密
System.out.println(cipherText);
System.out.println(plaintext);
InputStream in = null;
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(new File("D:\\version.txt")), 10240);//将加密内容写到本地文件
int b = -1;
long i = 0;
// OutputStream out = new FileOutputStream("/storage/sdcard0/aaa");
InputStream is = new ByteArrayInputStream(cipherText.getBytes());
byte[] buff = new byte[1024];
int len = 0;
while((len=is.read(buff))!=-1){
out.write(buff, 0, len);
}
is.close();
out.close();
} finally {
close(in);
close(out);
}
}
private static void close(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException e) {
// nothing
}
}
}
}
本文地址:https://blog.csdn.net/weixin_44305208/article/details/111994172