当前位置:   article > 正文

利用Base64加密算法将数据加密解密_based64的加解密

based64的加解密

1. Base64加密算法

Base64准确来说并不像是一种加密算法,而更像是一种编码标准。

我们知道现在最为流行的编码标准就是ASCLL,它用八个二进制位(一个char的大小)表示了127个字符,任何二进制序列都可以用这127个字符表示出来。

而Base64则是用6个二进制位表示了64个字符,也就是说,任何的二进制序列也都可以用这64个字符表示出来,这也是其名称的来源。

我们加密的方法,就是将原来的信息以ACSLL的标准转化为二进制序列(如果不是6的倍数,就用0补齐),然后按照Base64的标准,将其转化为该标准下的字符。

例如:

ASCLL(含换行符):

hello world
hello world
hello world

Base64:

aGVsbG8gd29ybGQKaGVsbG8gd29ybGQKaGVsbG8gd29ybGQA

如此加密之后的信息具有不可读性,广泛运用通信领域。

但是,这样加密过后的信息并不安全,只要你有心,就可以对照Basd64对照表和ASCLL码表解密出原来的内容。

对于Base64算法,我们仅做这么多介绍,感兴趣的小伙伴可以自己去深入了解一下。

2. 加密解密函数函数

2.1 加密函数Into_Base64

函数原型如下方代码所示。

在使用时,要求用户传入目标文件和原文件的文件名,函数就可以将原文件的信息加密存储到目标文件中。

  1. void Into_Base64(const char dest[], const char src[])//加密函数//目标文件,源文件
  2. {
  3. FILE* srcx = fopen(src, "r");
  4. if(srcx == NULL)
  5. {
  6. perror("source:");
  7. return;
  8. }
  9. FILE* destx = fopen(dest, "w");
  10. if(destx == NULL)
  11. {
  12. perror("destination:");
  13. return;
  14. }
  15. int flag = 1;
  16. while(flag)
  17. {
  18. char secret[5] = {0};//存储加密后数据
  19. int binary[25] = {0};//6和8的最小公倍数为24
  20. char ch = 0;
  21. for(int i = 0; i < 3; i++)
  22. {
  23. ch = getc(srcx);
  24. if(ch == -1)
  25. {
  26. flag = 0;
  27. goto interruption;
  28. }
  29. Into_Binary(binary + i * 8, ch, 0);
  30. }
  31. interruption:
  32. for(int i = 0; i < 4; i++)
  33. {
  34. Into_Char(secret + i, binary + i * 6, 1);
  35. }
  36. fprintf(destx, "%s", secret);
  37. }
  38. fclose(srcx);
  39. fclose(destx);
  40. }

在while循环中,我们每次读取24个字节的数据,这是因为6(Base64中一个字符的二进制长度)和8(ASCLL中一个字符的二进制长度)的最小公倍数为24,便于实现两种编码之间的转换。

2.2 解密函数Out_Base64

函数原型如下方代码所示。

在使用时,要求用户传入目标文件和原文件的文件名,函数就可以将原文件的信息解密存储到目标文件中。

  1. void Out_Base64(const char dest[], const char src[])//解密函数//目标文件,源文件
  2. {
  3. FILE* srcx = fopen(src, "r");
  4. if(srcx == NULL)
  5. {
  6. perror("source:");
  7. return;
  8. }
  9. FILE* destx = fopen(dest, "w");
  10. if(destx == NULL)
  11. {
  12. perror("destination:");
  13. return;
  14. }
  15. int flag = 1;
  16. while(flag)
  17. {
  18. char secret[4] = {0};//存储解密后数据//
  19. int binary[25] = {0};//6和8的最小公倍数为24
  20. char ch = 0;
  21. for(int i = 0; i < 4; i++)
  22. {
  23. ch = getc(srcx);
  24. if(ch == -1)
  25. {
  26. flag = 0;
  27. goto interruption;
  28. }
  29. Into_Binary(binary + i * 6, ch, 1);
  30. }
  31. interruption:
  32. for(int i = 0; i < 3; i++)
  33. {
  34. Into_Char(secret + i, binary + i * 8, 0);
  35. }
  36. fprintf(destx, "%s", secret);
  37. }
  38. fclose(srcx);
  39. fclose(destx);
  40. }

该函数与加密函数基本相同,只是在调用Into_Binary函数和Into_Char函数时的传参时有所差别,并且每24个字节解密得到的字符的仅有三个,所以secret数组的容量也相应减少了。

2.3 字符转二进制序列函数Into_Binary

函数原型如下方代码所示。

在使用时,要求用户传入存储二进制序列的数组的起始地址,要转换的字符,以及flag。

其中,flag==1时,将字符按照Base64编码转换为二进制;flag==0时,将字符按照ASCLL编码转换为二进制。

在加密函数中,flag==0;在解密函数中flag==1。

  1. //将src转换为二进制存到dest中
  2. //flag==1将Base64编码转换为二进制,flag==0将ascll转换为二进制
  3. void Into_Binary(int* dest, char src, int flag)
  4. {
  5. int n = 2;
  6. int num = 0;
  7. if(flag)
  8. {
  9. num = 5;//六位,最高位下标5
  10. src = Base64_Int(src);
  11. }
  12. else
  13. {
  14. num = 7;//八位,最高位下标7
  15. }
  16. for(int i = num; i >= 0; i--)
  17. {
  18. *(dest + i) = src % n;
  19. src /= 2;
  20. }
  21. }

由于计算机是以ASCLL编码标准识别字符的,所以我们直接将其当作整形处理即可。

但是计算机并不懂得Base64的编码标准是怎样的,所以我写了一个函数,将字符按照Base64标准转换为整形。

  1. char Base64_Int(char tem)//字符转Base64编码
  2. {
  3. if(tem >= 'A'&&tem <= 'Z')
  4. return tem - 'A';
  5. else if(tem >= 'a'&&tem <= 'z')
  6. return tem + 26 - 'a';
  7. else if(tem >= '0'&&tem <= '9')
  8. return tem + 52 - '0';
  9. else if(tem == '+')
  10. return 62;
  11. else if(tem == '/')
  12. return 63;
  13. }

2.4 二进制序列转字符函数

函数原型如下方代码所示。

在使用时,要求用户传入存储要转换的二进制序列的数组的起始地址,存储转换后字符的地址,以及flag。

其中,flag==1时,将二进制序列按Base64编码转换为字符;flag==0时,将二进制序列按ASCLL编码转换为字符。

在加密函数中,flag==1;在解密函数中flag==0。

  1. //将二进制数转换为char类型
  2. //flag==1转换为Base64,flag==0转换为ascll
  3. void Into_Char(char* dest, int* src, int flag)
  4. {
  5. int tem = 0;
  6. int n = 1;
  7. int num = 0;
  8. if(flag)
  9. num = 5;
  10. else
  11. num = 7;
  12. for(int i = num; i >= 0; i--)
  13. {
  14. tem += *(src + i) * n;
  15. n *= 2;
  16. }
  17. if(flag)
  18. *dest = Base64_Char(tem);
  19. else
  20. *dest = tem;
  21. }

与刚才同理,Base64_Char用于将整形按照Base64标准转换为字符。

  1. char Base64_Char(int tem)//Base64编码转字符
  2. {
  3. if(tem >= 0&&tem <= 25)
  4. return tem + 'A';
  5. else if(tem >= 26&&tem <= 51)
  6. return tem + 'a' - 26;
  7. else if(tem >= 52&&tem <= 61)
  8. return tem + '0' - 52;
  9. else if(tem == 62)
  10. return '+';
  11. else if(tem == 63)
  12. return '/';
  13. }

3. 测试

写好之后,我们就可以写个main函数来进行一下测试啦!

  1. int main()
  2. {
  3. Into_Base64("test2.txt", "test1.txt");//把test1.txt加密后存到test2.txt
  4. Out_Base64("test3.txt", "test2.txt");//把test2.txt解密后存到test3.txt
  5. return 0;
  6. }

注意要先创建好“test1.txt”文件,以免打开文件时失败。

运行之后得到两个文件

 

也是包成功的啊。 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/439282
推荐阅读
相关标签
  

闽ICP备14008679号