Javaは、暗号化と復号化のためのXOR(Xor)アルゴリズムを実装しています



Java Implements Xor Algorithm



この記事のリンク: https://blog.csdn.net/xietansheng/article/details/88420949

1.XOR暗号化の原則

整数aと任意の整数bは2回XORであり、結果は整数a自体です。つまり:a == a ^ b ^ b



ここで、aは暗号化する必要のある元のデータであり、bはキーです。 a ^ bは暗号化プロセスです。 XORの結果は、暗号化された暗号文です。暗号文(a ^ b)は、復号化プロセスであるキーbとXORされます。結果は、元のデータ自体です。

a = raw data b = Key // One-time XOR, encrypted to get ciphertext c = a ^ b // quadratic XOR, decrypted to get the original data (d == a) d = c ^ b

XOR暗号化元のテキストと暗号文の両方がわかっている場合は、元のテキストとパスワードを比較することでキーを取得できます。したがって、XOR暗号化は安全性が低く、一般的に単純な暗号化に使用されます。



2.XOR暗号化コードインスタンス

2.1 XOR暗号化ツールクラスパッケージ:XORUtils

XORUtils.java完全なソースコード:

package com.xiets.xor import java.io.BufferedOutputStream import java.io.Closeable import java.io.File import java.io.FileInputStream import java.io.FileOutputStream import java.io.IOException import java.io.InputStream import java.io.OutputStream /** * XOR (xor) algorithm encryption/decryption tool * * @author xietansheng */ public class XORUtils { /** * XOR algorithm encryption/decryption * * @param data data (ciphertext/clear text) * @param key key * @return returns decrypted/encrypted data */ public static byte[] encrypt(byte[] data, byte[] key) { if (data == null || data.length == 0 || key == null || key.length == 0) { return data } byte[] result = new byte[data.length] / / Use the key byte array to cyclically encrypt or decrypt for (int i = 0 i < data.length i++) { // The data is XORed with the key, and then XORed with the lower 8 bits of the loop variable (increased complexity) result[i] = (byte) (data[i] ^ key[i % key.length] ^ (i & 0xFF)) } return result } /** * Encryption/decryption of file XOR algorithm * * @param inFile input file (ciphertext / plain text) * @param outFile result output file * @param key key */ public static void encryptFile(File inFile, File outFile, byte[] key) throws Exception { InputStream in = null OutputStream out = null try { // file input stream in = new FileInputStream(inFile) / / Result output stream, XOR operation, the byte is a read and write, here must use the cache stream, // Wait until the cache is up to a certain number of bytes (10240 bytes) before writing to disk (otherwise it will write too many times, the speed will be very slow) out = new BufferedOutputStream(new FileOutputStream(outFile), 10240) int b = -1 long i = 0 // Read one byte of the file each time, use the key byte array to cyclically encrypt or decrypt while ((b = in.read()) != -1) { // The data is XORed with the key, and then XORed with the lower 8 bits of the loop variable (increased complexity) b = (b ^ key[(int) (i % key.length)] ^ (int) (i & 0xFF)) // Write an encrypted/decrypted byte out.write(b) // loop variable increment i++ } out.flush() } finally { close(in) close(out) } } private static void close(Closeable c) { if (c != null) { try { c.close() } catch (IOException e) { // nothing } } } }

XOR暗号化と復号化に使用されるメソッドは同じです。XORUtilsクラスには2つのパブリック静的メソッドがあります。

// Encrypt/decrypt byte array data static byte[] encrypt(byte[] data, byte[] key) // Encrypt/decrypt file static void encryptFile(File inFile, File outFile, byte[] key)

2.2XORUtilsツールクラスの使用

package com.xiets.aes import com.xiets.xor.XORUtils import java.io.File /** * @author xietansheng */ public class Main { public static void main(String[] args) throws Exception { String content = 'Hello world!' // original content String key = '123456' // original password for XOR encryption/decryption // Encrypt data, return ciphertext byte[] cipherBytes = XORUtils.encrypt(content.getBytes(), key.getBytes()) // decrypt the data, return the plaintext byte[] plainBytes = XORUtils.encrypt(cipherBytes, key.getBytes()) // Output the decrypted plaintext: 'Hello world!' System.out.println(new String(plainBytes)) /* * XOR encryption/decryption of files */ // Encrypt the file demo.java and output it to the file demo.jpg_cipher XORUtils.encryptFile(new File('demo.jpg'), new File('demo.jpg_cipher'), key.getBytes()) // Decode the file demo.jpg_cipher to the file demo.jpg_plain XORUtils.encryptFile(new File('demo.jpg_cipher'), new File('demo.jpg_plain'), key.getBytes()) // Compare the original file demo.jpg with the decrypted file demo.jpg_plain and the MD5 will be identical } }