Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Theinsanekiller · Aug 25, 2017 at 10:47 AM · spawn.netsystem.ioencryptioncryptography

HMAC Initialization throws and error. How to solve it

Getting a null reference exception when decrypting a json file using AES. Its some HMAC Initialize error.

I have used system.cryptorgraphy's AES encryption and decryption algorithm to use encryption in my game. Does anyone have Idea about What HMAC is and how to solve this error? I have pasted the code which I am using to to decrypt. The screenshot of the error is all attached.

 public  string Decrypt (string cipherText)
     {
         string EncryptionKey = "abc123";
         cipherText = cipherText.Replace (" ", "+");
         byte[] cipherBytes = Convert.FromBase64String (cipherText);
         using (Aes encryptor = Aes.Create ()) {
             Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes (EncryptionKey, new byte[] {
                 0x49,
                 0x76,
                 0x61,
                 0x6e,
                 0x20,
                 0x4d,
                 0x65,
                 0x64,
                 0x76,
                 0x65,
                 0x64,
                 0x65,
                 0x76
             });
             encryptor.Key = pdb.GetBytes (32);
             encryptor.IV = pdb.GetBytes (16);
             using (MemoryStream ms = new MemoryStream ()) {
                 using (CryptoStream cs = new CryptoStream (ms, encryptor.CreateDecryptor (), CryptoStreamMode.Write)) {
                     cs.Write (cipherBytes, 0, cipherBytes.Length);
                     cs.Close ();
                 }
                 cipherText = Encoding.Unicode.GetString (ms.ToArray ());
             }
         }
         return cipherText;
     }
 

alt text

screenshot-20170825-153107.png (122.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Aug 25, 2017 at 02:19 PM

Well, as you can see from the stacktrace the problem comes from your the "Rfc2898DeriveBytes" class. It uses the "HMACSHA1" class internally. That class is derived from the HMAC class. This in turn will try to create an instance of the "System.Security.Cryptography.SHA1CryptoServiceProvider" using reflection (System.Activator). It's most likely that this creation fails for some reason.

It's most likely this line inside tne Initialize method of the HMAC class:

 this._algo.Initialize();

"_algo" should contain the crypto provider. I suspect that "_algo" is null because the creation has failed. At least this is the most likely cause for the null ref. This is the complete Initialize method:

 public override void Initialize()
 {
     if (this._disposed)
     {
         throw new ObjectDisposedException("HMAC");
     }
     this.State = 0;
     this.Block.Initialize();
     byte[] array = this.KeySetup(this.Key, 54);
     this._algo.Initialize();
     this.Block.Core(array);
     Array.Clear(array, 0, array.Length);
 }

The only two points where a null reference exception can be thrown is the Block.Initialize and the _algo.Initialize line. Block is a self initializing property that's can't really fail to initialize. Therefore _algo is much more likely the cause.

This might be related to the target platform that you're using or to the .NET compatibility level that you're using. Try using ".NET 2.0" if you're currently using ".NET 2.0 subset".

Ohh btw this is a HMAC

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Theinsanekiller · Aug 28, 2017 at 05:25 AM 0
Share

Thanks for the reply. Yeah if it uses reflection then i guess the problem is with .Net 2.0 subset. I will try build using .Net 2.0.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to encrypt/lock a File.CreateText file? (its my highscore) 1 Answer

How do I make it so players can't edit the file but the game can? File.CreateText 2 Answers

Sha1/2 encryption on Unity 4.2? 2 Answers

System.IO.Ports missing for Unity with .NET 4.x 3 Answers

System.IO.Ports with .Net Standard 2.0? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges