-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.go
77 lines (73 loc) · 2.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"encoding/hex"
"fmt"
"goRecrypt/curve"
"goRecrypt/recrypt"
)
func main() {
// Alice Generate Alice key-pair
aPriKey, aPubKey, _ := curve.GenerateKeys()
// Bob Generate Bob key-pair
bPriKey, bPubKey, _ := curve.GenerateKeys()
// plain text
m := "Hello, Proxy Re-Encryption"
fmt.Println("origin message:", m)
// Alice encrypts to get cipherText and capsule
cipherText, capsule, err := recrypt.Encrypt(m, aPubKey)
if err != nil {
fmt.Println(err)
}
capsuleAsBytes, err := recrypt.EncodeCapsule(*capsule)
if err != nil {
fmt.Println("encode error:", err)
}
capsuleTest, err := recrypt.DecodeCapsule(capsuleAsBytes)
if err != nil {
fmt.Println("decode error:", err)
}
fmt.Println("capsule before encode:", capsule)
fmt.Println("capsule after decode:", capsuleTest)
fmt.Println("ciphereText:", cipherText)
// Test recreate aes key
keyBytes, err := recrypt.RecreateAESKeyByMyPriKey(capsule, aPriKey)
if err != nil {
fmt.Println("Recreate key error:", err)
}
fmt.Println("recreate key:", hex.EncodeToString(keyBytes))
// Alice generates re-encryption key
rk, pubX, err := recrypt.ReKeyGen(aPriKey, bPubKey)
if err != nil {
fmt.Println(err)
}
fmt.Println("rk:", rk)
// Server executes re-encrypt
newCapsule, err := recrypt.ReEncryption(rk, capsule)
if err != nil {
fmt.Println(err.Error())
}
// Bob decrypts the cipherText
plainText, err := recrypt.Decrypt(bPriKey, newCapsule, pubX, cipherText)
if err != nil {
fmt.Println(err)
}
plainTextByMyPri, err := recrypt.DecryptOnMyPriKey(aPriKey, capsule, cipherText)
if err != nil {
fmt.Println(err)
}
fmt.Println("PlainText by my own private key:", string(plainTextByMyPri))
// get plainText
fmt.Println("plainText:", string(plainText))
fileCapsule, err := recrypt.EncryptFile("a.txt", "a_encrypt.txt", aPubKey)
if err != nil {
fmt.Println("File Encrypt Error:", err)
}
fileNewCapsule, err := recrypt.ReEncryption(rk, fileCapsule)
if err != nil {
fmt.Println("ReEncryption Error:", err)
}
err = recrypt.DecryptFile("a_encrypt.txt", "a_decrypt.txt", bPriKey, fileNewCapsule, pubX)
if err != nil {
fmt.Println("Decrypt Error:", err)
}
}