zoukankan      html  css  js  c++  java
  • 最安全的加密算法

    在密码学里,有一种理想的加密方案,叫做一次一密乱码本(one-time pad)。

    one-time pad的算法有以下要求:
    1、密钥必须随机产生
    2、密钥不能重复使用
    3、密钥和密文的长度是一样的。

    one-time pad是最安全的加密算法,双方一旦安全交换了密钥,之后交换信息的过程就是绝对安全的啦。这种算法一直在一些要求高度机密的场合使用,据说美国和前苏联 之间的热线电话、前苏联的间谍都是使用One-time pad的方式加密的。不管超级计算机工作多久,也不管多少人,用什么方法和技术,具有多大的计算能力,都不可能破解。

    一次一密的一种实现方式,如下:

    public class OneTimePadUtil {
        public static byte[] xor(byte[] bytes, byte[] keyBytes) {
            if (keyBytes.length != bytes.length) {
                throw new IllegalArgumentException();
            }
    
            byte[] resultBytes = new byte[bytes.length];
    
            for (int i = 0; i < resultBytes.length; ++i) {
                resultBytes[i] = (byte) (keyBytes[i] ^ bytes[i]);
            }
    
            return resultBytes;
        }
    }

    使用例子:

         String plainText = "用户";
            String keyText = "密码";
    
            byte[] plainBytes = plainText.getBytes();
            byte[] keyBytes = keyText.getBytes();
    
            assert plainBytes.length == keyBytes.length;
    
            // 加密
            byte[] cipherBytes = OneTimePadUtil.xor(plainBytes, keyBytes);
            System.out.println(new String(cipherBytes));
            // 解密
            byte[] cipherPlainBytes = OneTimePadUtil.xor(cipherBytes, keyBytes);
            System.out.println(new String(cipherPlainBytes));

    这是最简单的加密算法,但也是最安全的机密算法。前天和朋友讨论到了这个问题,所以写了这篇文章。

  • 相关阅读:
    测试模式 windows2008 内部版本7601
    移动端UC /QQ 浏览器的部分私有Meta 属性
    正则表达式 正向预查 负向预查
    获取指定元素的某一个样式属性值
    把普通对象转换成json格式的对象
    求平均数-----类数组转换成数组
    轮播图
    倒计时
    JS 预解释相关理解
    ul ol di三者区别
  • 原文地址:https://www.cnblogs.com/longshiyVip/p/5117458.html
Copyright © 2011-2022 走看看