Format Preserving Encryption介绍

来源:互联网 发布:c语言实现网络爬虫 编辑:程序博客网 时间:2024/06/06 00:00

Format Preserving Encryption介绍

1. Introduction

分组密码(block cipher)的工作模式是基于分组密码算法,如AES等,它能够加密任意长度的binary,解决了分组密码只能加密固定长度的问题(如:AES-128只能加密128bit长度的明文)。对于非binary的情况,分组密码可以先将其转换为binary再进行加密,但是不能并不能保持相同的格式。例如,美国的社保号(Society Security Number,SSN)由九位数字组成,经过加密之后长度远超9位数字,所以传统的工作模式并不适用,所以FPE(Format Preserving Encryption)因此被设计出来处理这类问题。FPE通常用于数据脱敏中,因为它需要保持明密文的格式相同,例如社保号经过加密之后并不是固定长度的杂文,而是相同格式、打乱的号码,依然是社保号的格式。如下图所示:
这里写图片描述
这就变得有意义,之后可以用于数据挖掘等更广泛的用途,平衡了机密性和可用性。

2. Format Preserving Encryption算法

目前常用的由两种FPE模式,FF1和FF3,都是基于Festiel的加密模式,FF2被设计出来的时候不满足期望的128bit的安全强度,因此被弃用。每一个FPE模式控制在一个更大的叫做FFX的框架中。FF1和FF3都采用Festiel结构,以Triple Data Encryption Algorithm(TDEA)为基础。

FF1和FF3都有一个额外的输入参数,“tweak”(公开的参数);tweak可以被视为密钥的可变部分,因为他们同时决定了加密解密函数。Tweaks that vary can be especially important for implementations of FPE modes, because the number of possible values for the confidential data is often relatively small.

FF1 and FF3 offer somewhat different performance advantages. FF1 supports a greater range of lengths for the protected, formatted data, as well as flexibility in the length of the tweak. FF3 achieves greater throughput, mainly because its round count is eight, compared to ten for FF1.

2.1 Preliminary

Alphabet:A finite set of two or more symbols is called an alphabet.

Character:The symbols in an alphabet are called the characters of the alphabet.

radix:The number of characters in an alphabet is called the base, denoted by radix; thus, radix2.
在对字符进行FPE加密之前,需要先进行编码,例如对于小写英文字母:

a0,b1,c2,...,z25

此时radix=26,明文字符串 hello 则可以被表示为 {7,4,11,11,4}

Tweak:Tweaks(调整)are important for FPE modes, because FPE may be used in settings where the number of possible character strings is relatively small. 在这种情况下,tweak应该随着每次加密而改变。比如,应用在信用卡(Credit Card Number CCNs)中的话,前6位和后4位是不会变的,只有中间的6位会被加密。中间6位数字会产生1百万种可能,对于有着1亿条信用卡来说,大概每100张会有相同的中间6位数字,那么加密结果就相同了。如果让其他10位作为tweak,再将中间6位加密,那么结果就不一样。Therefore, for example, learning that the decryption of 111111-770611-1111 is 111111-123456-1111 would not reveal any information about the decryption of 999999-770611-9999, because the tweak in that case was different. 一般情况下,如果有些信息是不变的话,建议在加密的情况下将这些信息作为tweak。

FF1和FF3使用不同的方法去编码明文为数字。For FF1, numbers are represented by strings of numerals with decreasing order of significance; for FF3, numbers are represented by strings of numerals in the reverse order.

2.2 FPE的结构

这里写图片描述

2.3 FF1算法

parameters:

  • radix[2...216]
  • radixminlen100, and
  • 2minlenmaxlen<232
    这里写图片描述
    这里写图片描述
    这里写图片描述

2.4 FF3算法

parameters

  • radix[2...216]
  • radixminlen100, and
  • 2minlenmaxlen2logradix(296)
    这里写图片描述
    这里写图片描述

详细的函数实现请查看[1]。

3. Implementation

本例子参考[2]中的sample,来编译测试结果的正确性。FPE源代码和.class文件可以从这里下载。

  1. 首先先把下载下来的bin文件下的class文件打包为jar:jar cvf FPE.jar -c 目录
  2. 然后导入新建的程序。
    测试源代码:

    import org.fpe4j.*;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;import java.security.InvalidKeyException;public class PFPEProject {public static void main(String[] args){    //base = 10, {0,1,2,3,...,9}    int radix = 10;    int maxTlen = 20;    int[] plaintext = {0,1,2,3,4,5,6,7,8,9};    SecretKey secretKey;    byte[] tweak = {};    String[] strKey = {"2B", "7E", "15", "16", "28", "AE", "D2", "A6", "AB", "F7", "15", "88", "09", "CF", "4F", "3C"};    byte[] byteKey = new byte[16];    //密钥string转换为byte    int strLen = strKey.length;    for(int i = 0; i < strLen; ++i){        byteKey[i] = (byte) Integer.parseInt(strKey[i], 16);    }    //产生secretKey    try{        secretKey = new SecretKeySpec(byteKey, "AES");        FF1 Ff1class = new FF1(radix, maxTlen);        int[] cipher = Ff1class.encrypt(secretKey, tweak, plaintext);        //输出        for(int i=0; i<cipher.length; ++i){            System.out.print(cipher[i]);        }    }catch(IllegalArgumentException e){        e.printStackTrace();    }     catch (InvalidKeyException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }      }}

结果:
这里写图片描述

结论:和sample中的一致。

Reference

[1]. NIST AES FFX Format-Preserving Encryption (FPE) mode standard
[2]. FPE模式的sample

0 0
原创粉丝点击