目录
记录下格式保留加密算法及其Demo。
一、概述
格式保留加密(Format Preserving Encrypt,FPE)是一种可以保证密文与明文具有相同格式与长度的加密方式。
FPE常应用于数据去标志化或者脱敏,能保持明文和密文的格式相同,如英文加密后仍为英文,数字加密后仍为数字。
二、加解密原理
常见的FF1加解密算法如下:
三、使用Demo
package main
import (
"encoding/hex"
"fmt"
"github.com/capitalone/fpe/ff1"
)
// panic(err) is just used for example purposes.
func main() {
// Key and tweak should be byte arrays. Put your key and tweak here.
// To make it easier for demo purposes, decode from a hex string here.
key, err := hex.DecodeString("EF4359D8D580AA4F7F036D6F04FC6A94")
if err != nil {
panic(err)
}
tweak, err := hex.DecodeString("D8E7920AFA330A73")
if err != nil {
panic(err)
}
// Create a new FF1 cipher "object"
// 10 is the radix/base, and 8 is the tweak length.
FF1, err := ff1.NewCipher(10, 8, key, tweak)
if err != nil {
panic(err)
}
original := "123456789"
// Call the encryption function on an example SSN
ciphertext, err := FF1.Encrypt(original)
if err != nil {
panic(err)
}
plaintext, err := FF1.Decrypt(ciphertext)
if err != nil {
panic(err)
}
fmt.Println("Original:", original)
fmt.Println("Ciphertext:", ciphertext)
fmt.Println("Plaintext:", plaintext)
}
输出为:
Original: 123456789
Ciphertext: 956289744
Plaintext: 123456789