最安全的加密算法

在密码学里,有一种理想的加密方案,叫做一次一密乱码本(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);

//解密
byte[] cipherPlainBytes = OneTimePadUtil.xor(cipherBytes, keyBytes);


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

posted on 2005-03-27 11:28  温少  阅读(12562)  评论(14编辑  收藏  举报

导航