关于#二进制#的问题,如何解决?

8、文件加密程序:将待加密文件以二进制打开,读入32个字节,分别与口令异或,在将4、5、6三个字分别与0x四个a异或,即完成加密,执行第二次就解密

参考:https://blog.csdn.net/qq_72111942/article/details/126477652

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7584383
  • 我还给你找了一篇非常好的博客,你可以看看是否有帮助,链接:编写函数,判断一个正整数a是否为完数,如果是完数,函数返回值为1;否则为0。
  • 除此之外, 这篇博客: 算法笔记习题 2-5小节中的 问题 A: 习题6-4 有序插入 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    1. 题目描述 ,有一个已排好序的数组,要求输入一个数后,按原来排序的规律将它插入到数组中。
      假设数组长度为10,数组中前9个数(这9个数要求从键盘上输入,输入时要满足自小到大的输入顺序)已经按从小到大进行排序。
      然后再从键盘上输入一个整数,将此整数插入到前有序的9个数中,使得最终的10个数依然是从小到大有序的。
    2. 输入,第一行输入以空格分隔的9个整数数,要求按从小到大的顺序输入。
      第二行输入一个整数
    3. 输出,从小到大输出这10个数,每个数一行。
    4. 样例输入, 1 11 21 31 41 51 61 71 81
      45
    5. 样例输出,
      1
      11
      21
      31
      41
      45
      51
      61
      71
      81

    我的理解
    1.先输入已排好序的数组
    2.判断输入的数应该在哪个位置
    3.这个位置包括这个位置之后的数依此向后挪一位,把这个数放进去
    4.整个数组输出

    **以下是代码**
    
    // Ada
    #include<stdio.h>
    
    int main(){
    	int a[10], n, k, j = 0;
    	for(int i = 0; i < 9; i++){
    		scanf("%d ", &a[i]);
    	} 
    	scanf("%d", &n);
    	while(n > a[j])
    	{
    		j++;
    	}
    	for( k = 8; k >= j; k--){ //小心临界点
    		a[k+1] = a[k];
    	}
    	a[k+1] = n;  //小心临界点
    
    	for(int i = 0; i < 10; i++){
    		printf("%d\n", a[i]);
    	}
    	
    	return 0;
    } 
    
    **临界点有时会出现问题,大于还是大于等于呀等等,临界点还是要先试数,不然容易想糊涂**
    
  • 以下回答来自chatgpt:

    解决方案

    根据问题描述,我们可以按照以下步骤实现文件的加密和解密:

    1. 打开待加密的文件,并以二进制形式读取文件内容。
    2. 读取文件的前32个字节。
    3. 将每个字节与一个口令进行异或操作。
    4. 将第4、5、6个字节分别与0x四个a(即0xaaaa)进行异或操作,完成加密过程。
    5. 如果需要解密文件,再次执行以上步骤即可还原密文为明文。

    下面我们分别介绍每个步骤的具体实现。

    步骤1: 打开待加密的文件,并以二进制形式读取文件内容

    def read_file(file_path):
        with open(file_path, 'rb') as file:
            content = file.read()
        return content
    

    步骤2: 读取文件的前32个字节

    def read_first_32_bytes(content):
        return content[:32]
    

    步骤3: 将每个字节与一个口令进行异或操作

    def xor_bytes(content, password):
        encrypted_content = []
        for byte in content:
            encrypted_byte = byte ^ password
            encrypted_content.append(encrypted_byte)
        return bytes(encrypted_content)
    

    步骤4: 将第4、5、6个字节分别与0x四个a(即0xaaaa)进行异或操作,完成加密过程

    def special_xor(content):
        encrypted_content = bytearray(content)
        encrypted_content[3] ^= 0xaa
        encrypted_content[4] ^= 0xaa
        encrypted_content[5] ^= 0xaa
        return bytes(encrypted_content)
    

    步骤5: 如果需要解密文件,再次执行以上步骤即可还原密文为明文

    解密和加密过程是相同的,所以可以直接再次执行以上步骤即可还原密文为明文。

    完整的代码如下所示:

    def read_file(file_path):
        with open(file_path, 'rb') as file:
            content = file.read()
        return content
    
    
    def read_first_32_bytes(content):
        return content[:32]
    
    
    def xor_bytes(content, password):
        encrypted_content = []
        for byte in content:
            encrypted_byte = byte ^ password
            encrypted_content.append(encrypted_byte)
        return bytes(encrypted_content)
    
    
    def special_xor(content):
        encrypted_content = bytearray(content)
        encrypted_content[3] ^= 0xaa
        encrypted_content[4] ^= 0xaa
        encrypted_content[5] ^= 0xaa
        return bytes(encrypted_content)
    
    
    # 加密文件
    def encrypt_file(file_path, password):
        content = read_file(file_path)
        first_32_bytes = read_first_32_bytes(content)
        encrypted_first_32_bytes = xor_bytes(first_32_bytes, password)
        encrypted_first_32_bytes = special_xor(encrypted_first_32_bytes)
    
        encrypted_content = encrypted_first_32_bytes + content[32:]
        return encrypted_content
    
    
    # 解密文件
    def decrypt_file(file_path, password):
        encrypted_content = read_file(file_path)
        decrypted_first_32_bytes = special_xor(encrypted_content[:32])
        decrypted_first_32_bytes = xor_bytes(decrypted_first_32_bytes, password)
    
        decrypted_content = decrypted_first_32_bytes + encrypted_content[32:]
        return decrypted_content
    
    
    # 测试加密文件
    encrypted_content = encrypt_file("test.txt", 12345)
    with open("encrypted.txt", 'wb') as file:
        file.write(encrypted_content)
    
    # 测试解密文件
    decrypted_content = decrypt_file("encrypted.txt", 12345)
    with open("decrypted.txt", 'wb') as file:
        file.write(decrypted_content)
    

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^