Am Donnerstag, 30. Juni 2016 08:57:11 UTC+2 schrieb Lazytiger:
>
> Hi, all
>
> I have a C# code to decrypt data as following
>
>         public static string test(string input, string key)
>         {
>             if (((input == null) || string.IsNullOrEmpty(input.Trim()))
>  || ((input == "false") || (input == "null")))
>             {
>                 return string.Empty;
>             }
>             RijndaelManaged managed = new RijndaelManaged {
>                 KeySize = 0x100,
>                 BlockSize = 0x100,
>                 Mode = CipherMode.CBC,
>                 Padding = PaddingMode.PKCS7,
>                 Key = Encoding.ASCII.GetBytes(key),
>                 IV = Encoding.ASCII.GetBytes(key)
>             };
>             try
>             {
>                 byte[] buffer = Convert.FromBase64String(input);
>                 ICryptoTransform transform = managed.CreateDecryptor();
>                 byte[] bytes = null;
>                 using (MemoryStream stream = new MemoryStream())
>                 {
>                     using (CryptoStream stream2 = new CryptoStream(stream,
>  transform, CryptoStreamMode.Write))
>                     {
>                         stream2.Write(buffer, 0, buffer.Length);
>                     }
>                     bytes = stream.ToArray();
>                 }
>                 return Encoding.ASCII.GetString(bytes);
>             }
>             catch (Exception exception)
>             {
>                 Console.Write (exception.ToString ());
>                 return string.Empty;
>             }
>         } 
>
> And I have write a golang code as following:
>
> *func* test1(data, key string) {
> raw, err := base64.StdEncoding.DecodeString(data)
> *if* err != nil {
> *println*(err.Error())
> *return*
> }
>
> block, err := aes.NewCipher([]byte(key))
> *if* err != nil {
> *println*(err.Error())
> *return*
> }
> mode := cipher.NewCBCDecrypter(block, []byte(key)[:aes.BlockSize])
> mode.CryptBlocks(raw, raw)
>
> *println*(string(raw))
> }
>
> Besides the PKCS7 padding problem, I can’t get the right answer. Can 
> someone help me with this? Where do I get  this IV parameter?
> I Can’t set IV as  C# does, because golang just complains about block size 
> problem.  
>

You geht the IV parameter e.g. from  the beginning of the ciphertext 
[suggested by go examples 0].  
But be careful the IV have to be unique to ensure a safe en- and 
decyrption. 

I also have modified your example a little bit so that it is working [1]. I 
have ignored the error checking, the checking of the block size and the 
encoding of the crypted data.  Nevertheless I hope it will help you. 


[0] https://golang.org/pkg/crypto/cipher/#example_NewCBCDecrypter
[1] https://play.golang.org/p/wEQ9vQIdJS

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to