zoukankan      html  css  js  c++  java
  • 简单的数据加密解密

    简单的数据加密解密

    通常都是利用“^”异或运算对字符串进行加密


    通过char[] array = String.toCharArray();// 获取字符数组;
    然后遍历字符数组,进行异或运算
    按位做“异或”运算是:位值相同得1,不同得0
    原文和密钥进行异或运算得到密文,这是加密
    将密文和同一密钥进行异或运算就可以得到原文,这就是解密
    代码如下:

     1 import java.io.*;
     2 
     3 public class Example {
     4     public static void main(String[] args) {
     5         char a[] = "我真的超级帅".toCharArray();
     6         int n = 0;
     7         try {
     8             File out = new File("word.txt");
     9             for (int i = 0; i < a.length; i++) {
    10                 a[i] = (char) (a[i] ^ 'R');
    11             }
    12             FileWriter fw = new FileWriter(out);
    13             fw.write(a, 0, a.length);
    14             fw.close();
    15             FileReader fr = new FileReader(out);
    16             char tom[] = new char[10];
    17             System.out.println("加密后:");
    18             while ((n = fr.read(tom, 0, 10)) != -1) {
    19                 String s = new String(tom, 0, n);
    20                 System.out.println(s);
    21             }
    22             fr.close();
    23             fr = new FileReader(out);
    24             System.out.println("明文:");
    25             while ((n = fr.read(tom, 0, 10)) != -1) {
    26                 for (int j = 0; j < n; j++) {
    27                     tom[j] = (char) (tom[j] ^ 'R');
    28                 }
    29                 String str = new String(tom, 0, n);
    30                 System.out.println(str);
    31             }
    32             
    33             fr.close();
    34         } catch (Exception e) {
    35             e.printStackTrace();
    36         }
    37         
    38     }
    39     
    40 }

    加密后结果如下:

    务实,说实话!
  • 相关阅读:
    js参数自定义
    分页插件--记录
    .net mvc接收参数为null的解决方案
    c#枚举转字典或表格
    openlayers添加弹出框
    openlayers按坐标点播放
    openlayers轨迹匀速播放
    MyEclipse配置进行Hibernate逆映射
    BIO,NIO,AIO
    Git遇到的一点错误
  • 原文地址:https://www.cnblogs.com/xtuxiongda/p/8557207.html
Copyright © 2011-2022 走看看