Last active
January 22, 2020 20:49
-
-
Save legndery/dbb640a3010cdabdae5e881e51af18a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controller | |
import ( | |
"crypto-service/util" | |
"encoding/json" | |
"fmt" | |
"github.com/gin-gonic/gin" | |
"io/ioutil" | |
"net/http" | |
"net/url" | |
) | |
type BackBaseController interface { | |
EncryptUrl(c *gin.Context) | |
} | |
type backBaseController struct { | |
} | |
func NewBackBaseController() BackBaseController { | |
return backBaseController{} | |
} | |
func (cryptoController backBaseController) EncryptUrl(ctx *gin.Context) { | |
const ( | |
l_userId = "SOLEPROP" | |
l_session = "WbvM3C-FuIJb-RXnzXKRI4HO1iNsN753wcyNvIjAIzfVuumV5KUc!-968812618!1579689708937" | |
l_sourceSystemId = "BBP" | |
l_destinationSystemId = "CBX" | |
) | |
l_corporateId := "1000122078" | |
l_randomNum := "-2089140915" | |
l_urlQueryString := fmt.Sprintf("UserID=%s&SessionID=%s&TokenID=%s&CorpID=%s&SourceSystemID=%s&DestSystemID=%s", | |
l_userId, l_session, l_randomNum, l_corporateId, l_sourceSystemId, l_destinationSystemId) | |
l_urlQueryString = url.QueryEscape("UserID=SOLEPROP&SessionID=WbvM3C-FuIJb-RXnzXKRI4HO1iNsN753wcyNvIjAIzfVuumV5KUc!-968812618!1579689708937&TokenID=-2089140915&CorpID=1000122078&SourceSystemID=BBP&DestSystemID=CBX") | |
bodyBytes, _ := ioutil.ReadAll(ctx.Request.Body) | |
jsonBody := make(map[string]interface{}) | |
json.Unmarshal(bodyBytes, &jsonBody) | |
l_urlQueryString = jsonBody["url"].(string) | |
encryptedKey := util.Sha1Digest([]byte("IDFC CBX SSO"), 16) | |
encryptedQueryString := util.AESEncrypt(l_urlQueryString, encryptedKey) | |
//decodedString, _ := base64.StdEncoding.DecodeString("ktgPAGHPbo3E+L5GAIQ4GVIpitCG0/CxUuJ8zXI4769XeWY3o1aW9KrTpwSEpc+QVYj2c7eSkQpLAsAeaPGYEA2nISnzhQIp7R8xGSEURNSAifiNFAGEpDKaJ2xb9B4rZOm7UHJ5WmgeHP+kmF5acLG3cTkzt9farQdLNrLkCL6aDfXZFPm10qxTI4eeGel+VbaSFdpQFyw1jd3q+XoWBZ+gVgKcXCU/h9V57p3PA3h2iTXnFvmUNGy7BPV4IuTfWb7xx9dKgqALEHN/WwIXgQ==") | |
//decryptedUrl := util.AESDecrypt(decodedString, encryptedKey) | |
fmt.Println(encryptedQueryString) | |
//"3raKNCRKiJLNm0CfRriNJA==" | |
ctx.JSON(http.StatusOK, map[string]interface{}{ | |
"url": l_urlQueryString, | |
"encryptedString": encryptedQueryString, | |
//"decryptedUrl": string(decryptedUrl), | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"errors" | |
"crypto/aes" | |
"crypto/cipher" | |
"crypto/sha1" | |
"fmt" | |
"net/url" | |
"encoding/base64" | |
) | |
func main() { | |
encryptedKey := Sha1Digest([]byte("IDFC CBX SSO"), 16) | |
l_urlQueryString := url.QueryEscape("UserID=SOLEPROP&SessionID=WbvM3C-FuIJb-RXnzXKRI4HO1iNsN753wcyNvIjAIzfVuumV5KUc!-968812618!1579689708937&TokenID=-2089140915&CorpID=1000122078&SourceSystemID=BBP&DestSystemID=CBX") | |
encryptedQueryString := AESEncrypt(l_urlQueryString, encryptedKey) | |
originalString := "ktgPAGHPbo3E+L5GAIQ4GVIpitCG0/CxUuJ8zXI4769XeWY3o1aW9KrTpwSEpc+QVYj2c7eSkQpLAsAeaPGYEA2nISnzhQIp7R8xGSEURNSAifiNFAGEpDKaJ2xb9B4rZOm7UHJ5WmgeHP+kmF5acLG3cTkzt9farQdLNrLkCL6aDfXZFPm10qxTI4eeGel+VbaSFdpQFyw1jd3q+XoWBZ+gVgKcXCU/h9V57p3PA3h2iTXnFvmUNGy7BPV4IuTfWb7xx9dKgqALEHN/WwIXgQ==" | |
decodedString , _ := base64.StdEncoding.DecodeString(originalString) | |
decryptedUrl := Decrypt(originalString, encryptedKey) | |
fmt.Println(base64.StdEncoding.EncodeToString(encryptedQueryString)) | |
fmt.Println(string(decryptedUrl)) | |
fmt.Println(string(DecryptAes128Ecb(decodedString, encryptedKey))) | |
fmt.Println(base64.StdEncoding.EncodeToString(EncryptAes128Ecb([]byte(l_urlQueryString), encryptedKey))) | |
} | |
func DecryptAes128Ecb(data, key []byte) []byte { | |
cipher, _ := aes.NewCipher([]byte(key)) | |
decrypted := make([]byte, len(data)) | |
size := 16 | |
for bs, be := 0, size; bs < len(data); bs, be = bs+size, be+size { | |
cipher.Decrypt(decrypted[bs:be], data[bs:be]) | |
} | |
return decrypted | |
} | |
func EncryptAes128Ecb(data, key []byte) []byte { | |
cipher, _ := aes.NewCipher([]byte(key)) | |
size := 16 | |
data = PKCS5Padding(data, size) | |
encrypted := make([]byte, len(data)) | |
for bs, be := 0, size; bs < len(data); bs, be = bs+size, be+size { | |
cipher.Encrypt(encrypted[bs:be], data[bs:be]) | |
} | |
return encrypted | |
} | |
func AESEncrypt(src string, key []byte) []byte { | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
fmt.Println("key error1", err) | |
} | |
if src == "" { | |
fmt.Println("plain content empty") | |
} | |
//iv := key[:aes.BlockSize] | |
ecb := cipher.NewCBCEncrypter(block, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) | |
content := []byte(src) | |
content = PKCS5Padding(content, block.BlockSize()) | |
crypted := make([]byte, len(content)) | |
ecb.CryptBlocks(crypted, content) | |
return crypted | |
} | |
func AESDecrypt(crypt []byte, key []byte) []byte { | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
fmt.Println("key error1", err) | |
} | |
if len(crypt) == 0 { | |
fmt.Println("plain content empty") | |
} | |
ecb := cipher.NewCBCDecrypter(block, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) | |
decrypted := make([]byte, len(crypt)) | |
ecb.CryptBlocks(decrypted, crypt) | |
d, r := pkcs7Unpad(decrypted, block.BlockSize()) | |
fmt.Println(r) | |
// return PKCS5Trimming(decrypted) | |
return d; | |
} | |
func PKCS5Padding(ciphertext []byte, blockSize int) []byte { | |
padding := blockSize - len(ciphertext)%blockSize | |
fmt.Println(padding) | |
padtext := bytes.Repeat([]byte{byte(padding)}, padding) | |
return append(ciphertext, padtext...) | |
} | |
func pkcs7Unpad(b []byte, blocksize int) ([]byte, interface{}) { | |
if blocksize <= 0 { | |
return nil, errors.New("1") | |
} | |
if b == nil || len(b) == 0 { | |
return nil, errors.New("2") | |
} | |
if len(b)%blocksize != 0 { | |
return nil, errors.New("3") | |
} | |
c := b[len(b)-1] | |
n := int(c) & 0x0ff | |
if n == 0 || n > len(b) { | |
return nil, n | |
} | |
for i := 0; i < n; i++ { | |
if b[len(b)-n+i] != c { | |
return nil, errors.New("5") | |
} | |
} | |
return b[:len(b)-n], nil | |
} | |
func PKCS5Trimming(encrypt []byte) []byte { | |
padding := encrypt[len(encrypt)-1] | |
return encrypt[:len(encrypt)-int(padding)] | |
// return encrypt | |
} | |
func Sha1Digest(keyBytes []byte, blockSize int) []byte { | |
key := make([]byte, blockSize) | |
h := sha1.New() | |
h.Write(keyBytes) | |
bs := h.Sum(nil) | |
copy(key, bs) | |
fmt.Println(base64.StdEncoding.EncodeToString(key)) | |
return key | |
} | |
func Decrypt(encrypted string, passphrase []byte) (string) { | |
ct, _ := base64.StdEncoding.DecodeString(encrypted) | |
if len(ct) < 16 { | |
return "" | |
} | |
iv := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} | |
key:= passphrase | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
panic(err) | |
} | |
cbc := cipher.NewCBCDecrypter(block, iv) | |
dst := make([]byte, len(ct)) | |
cbc.CryptBlocks(dst, ct) | |
// block.Decrypt(dst, ct); | |
return string((dst)) | |
} | |
func __PKCS7Padding(ciphertext []byte, blockSize int) []byte { | |
padding := blockSize - len(ciphertext)%blockSize | |
padtext := bytes.Repeat([]byte{byte(padding)}, padding) | |
return append(ciphertext, padtext...) | |
} | |
func __PKCS7Trimming(encrypt []byte) []byte { | |
padding := encrypt[len(encrypt)-1] | |
return encrypt[:len(encrypt)-int(padding)] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package router | |
import ( | |
"crypto-service/controller" | |
"crypto-service/service" | |
"crypto-service/util" | |
"github.com/gin-gonic/gin" | |
) | |
func Init() *gin.Engine { | |
r := gin.Default() | |
registerRoutes(r) | |
return r | |
} | |
func registerRoutes(r *gin.Engine) { | |
healthCheckController := controller.NewHealthCheckController() | |
cryptoUtil := util.NewCryptoUtil() | |
cryptoService := service.NewCryptoService(cryptoUtil) | |
cryptoController := controller.NewCryptoController(cryptoService) | |
backBaseController := controller.NewBackBaseController() | |
routerGroup := r.Group("/api") | |
{ | |
routerGroup.GET("/crypto/healthz", healthCheckController.HealthCheck) | |
routerGroup.GET("/crypto/id-token/encrypt", cryptoController.EncryptIdToken) | |
routerGroup.GET("/crypto/id-token/decrypt", cryptoController.DecryptIdToken) | |
routerGroup.POST("/crypto/decrypt", cryptoController.Decrypt) | |
routerGroup.POST("/crypto/make-encrypted-url", backBaseController.EncryptUrl) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util | |
import ( | |
"bytes" | |
"crypto/aes" | |
"crypto/cipher" | |
"crypto/sha1" | |
"fmt" | |
) | |
func AESEncrypt(src string, key []byte) []byte { | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
fmt.Println("key error1", err) | |
} | |
if src == "" { | |
fmt.Println("plain content empty") | |
} | |
//iv := key[:aes.BlockSize] | |
ecb := cipher.NewCBCEncrypter(block, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) | |
content := []byte(src) | |
content = PKCS5Padding(content, block.BlockSize()) | |
crypted := make([]byte, len(content)) | |
ecb.CryptBlocks(crypted, content) | |
return crypted | |
} | |
func AESDecrypt(crypt []byte, key []byte) []byte { | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
fmt.Println("key error1", err) | |
} | |
if len(crypt) == 0 { | |
fmt.Println("plain content empty") | |
} | |
ecb := cipher.NewCBCDecrypter(block, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) | |
decrypted := make([]byte, len(crypt)) | |
ecb.CryptBlocks(decrypted, crypt) | |
return PKCS5Trimming(decrypted) | |
} | |
func PKCS5Padding(ciphertext []byte, blockSize int) []byte { | |
padding := blockSize - len(ciphertext)%blockSize | |
padtext := bytes.Repeat([]byte{byte(padding)}, padding) | |
return append(ciphertext, padtext...) | |
} | |
func PKCS5Trimming(encrypt []byte) []byte { | |
padding := encrypt[len(encrypt)-1] | |
return encrypt[:len(encrypt)-int(padding)] | |
} | |
func Sha1Digest(keyBytes []byte, blockSize int) []byte { | |
key := make([]byte, blockSize) | |
h := sha1.New() | |
h.Write([]byte(keyBytes)) | |
bs := h.Sum(nil) | |
copy(key, bs) | |
return key | |
} |
Author
legndery
commented
Jan 22, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment