Last active
January 5, 2016 02:29
-
-
Save jimwei/b65eeea1ad9d403a4ee5 to your computer and use it in GitHub Desktop.
translate donet DESCryptoServiceProvider
This file contains 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
/*public static string DecryptStringEx(string source) | |
{ | |
//判断是否为空字符串 | |
if (string.IsNullOrEmpty(source)) | |
{ | |
//直接返回原字符串 | |
return source; | |
} | |
//获得要解密的数据 | |
byte[] data = Convert.FromBase64String(source); | |
//创建DES算法提供者 | |
DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); | |
//创建加密转换运算对象 | |
ICryptoTransform transform = provider.CreateDecryptor(DEFAULT_KEY, DEFAULT_IV); | |
//判断数据是否满足输入数据块要求 | |
if (data.Length < transform.InputBlockSize) | |
{ | |
//说明字符串没有加密,则直接返回 | |
return source; | |
} | |
//获得解密的结果 | |
byte[] result = transform.TransformFinalBlock(data, 0, data.Length); | |
//返回解密结果 | |
return Encoding.UTF8.GetString(result); | |
} | |
*/ | |
func Decrypt(source string) string { | |
data, _ := base64.StdEncoding.DecodeString(source) | |
iv := []byte{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF} | |
block, err := des.NewCipher([]byte("11111111")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
m := cipher.NewCBCDecrypter(block, iv) | |
log.Println("the aes blocksize is", aes.BlockSize) | |
data = data[:8] | |
m.CryptBlocks(data, data) | |
return string(data) | |
} | |
func Encrypt(source string) string { | |
data := []byte(source) | |
block, err := des.NewCipher([]byte("11111111")) | |
if err != nil { | |
log.Fatal(err) | |
} | |
iv := []byte{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF} | |
m := cipher.NewCBCEncrypter(block, iv) | |
data = data[:8] | |
m.CryptBlocks(data, data) | |
str := base64.StdEncoding.EncodeToString(data) | |
return str | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment