{"id":15011681,"url":"https://github.com/maymeow/maymeow.cryptography","last_synced_at":"2025-04-12T03:31:21.492Z","repository":{"id":45285505,"uuid":"239025380","full_name":"MayMeow/MayMeow.Cryptography","owner":"MayMeow","description":"🔐 Wrapper arround .NET Cryptography library","archived":false,"fork":false,"pushed_at":"2024-02-03T19:37:14.000Z","size":88,"stargazers_count":17,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-08T10:02:45.531Z","etag":null,"topics":["cryptography","csharp","dotnet","dotnet-core","dotnet-standard","hacktoberfest","hacktoberfest2021","nuget-package"],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/MayMeow.Cryptography/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MayMeow.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["maymeow"],"ko_fi":"maymeow"}},"created_at":"2020-02-07T21:25:19.000Z","updated_at":"2024-09-21T18:16:48.000Z","dependencies_parsed_at":"2024-09-24T19:43:58.088Z","dependency_job_id":"9128d797-4a8d-4d5e-868b-8d0d0a9cf8ea","html_url":"https://github.com/MayMeow/MayMeow.Cryptography","commit_stats":{"total_commits":49,"total_committers":5,"mean_commits":9.8,"dds":0.4693877551020408,"last_synced_commit":"8f19bf6c4c77f445df1120862475fff9296472ca"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MayMeow%2FMayMeow.Cryptography","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MayMeow%2FMayMeow.Cryptography/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MayMeow%2FMayMeow.Cryptography/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MayMeow%2FMayMeow.Cryptography/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MayMeow","download_url":"https://codeload.github.com/MayMeow/MayMeow.Cryptography/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248512606,"owners_count":21116639,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cryptography","csharp","dotnet","dotnet-core","dotnet-standard","hacktoberfest","hacktoberfest2021","nuget-package"],"created_at":"2024-09-24T19:41:26.190Z","updated_at":"2025-04-12T03:31:19.420Z","avatar_url":"https://github.com/MayMeow.png","language":"C#","funding_links":["https://github.com/sponsors/maymeow","https://ko-fi.com/maymeow","https://ko-fi.com/D1D5DMOTA"],"categories":[],"sub_categories":[],"readme":"![.NET Core](https://github.com/MayMeow/MayMeow.Cryptography/workflows/.NET%20Core/badge.svg)\r\n\r\n# MayMeow.Cryptography\r\n\r\nWrapper arround .NET Cryptography library.\r\n\r\n[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/D1D5DMOTA)\r\n\r\n## Installation\r\n\r\nThis library can be installed to your project with NuGet package manager\r\n\r\n```powershell\r\nInstall-Package MayMeow.Cryptography -Version 1.1.0\r\n```\r\n\r\nor with dotnet cli\r\n\r\n```powershell\r\ndotnet add package MayMeow.Cryptography --version 1.1.0\r\n```\r\n\r\nFor more installation methods refer [NuGet page](https://www.nuget.org/packages/MayMeow.Cryptography) of this project.\r\n\r\n## Usage\r\n\r\n### Using this library\r\n\r\nUse in you project\r\n\r\n```csharp\r\nusing MayMeow.Cryptography;\r\n```\r\n\r\n### AES encryption (symmetric one)\r\n\r\n- Symmetric-key algorithm\r\n- Approved and used by NSA\r\n- Much faster than DES and 3DES for bulk data encryption.\r\n- Original name is **Rijndael**, named to AES after [Advanced Encryption Standard](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard_process) contest.\r\n\r\nInitialize aes and generate new key and IV. AES is an symmetric encryption which using same key to encrypt and decrypt.\r\n\r\n```csharp\r\nAES aes = new AES();\r\n\r\nstring AesKey = aes.GetAesKey();\r\nstring AesIV = aes.GetIV();\r\n```\r\n\r\nTo encrypt your text use\r\n\r\n```csharp\r\nstring AesEncrypted = AES.Encrypt(message, AesKey, AesIV);\r\n```\r\n\r\nand simillarly to decrypt use\r\n\r\n```csharp\r\nstring AesDecrypted = AES.Decrypt(AesEncrypted, AesKey);\r\n```\r\n\r\nExample above using generated and unprotected key for your encryption. \r\n\r\n### RSA Encryption (asymmetric one)\r\n\r\n- Achieving strong encryption through the use of two large prime numbers [Wikipedia](https://en.wikipedia.org/wiki/RSA_(cryptosystem))\r\n- Encrypts your data with public key\r\n- Decrypts your data with private key\r\n- **Solves** I want anybody to be able to encrypt a message, but I'm the only one who can decrypt it. I don't want to share decryption keys with anybody.\r\n\r\nFirst initialize RSA and create your public and private key\r\n\r\n```csharp\r\nRSA rsa = new RSA(RSA.KEY_SIZE);\r\n\r\nstring pubKey = TextConversion.Base64Encode(rsa.GetPublicKey());\r\nstring privKey = TextConversion.Base64Encode(rsa.GetPrivateKey());\r\n```\r\n\r\nNow encryption is easy as\r\n\r\n```csharp\r\nstring message = \"Hello world\";\r\nstring encryptedText = RSA.Encrypt(message, RSA.SetKeyFromString(pubKey));\r\nstring plainText = RSA.Decrypt(encryptedText, RSA.SetKeyFromString(privKey));\r\n```\r\n\r\n### AES GCM encryption with protected key (combination of asymmetric and symmetric one)\r\n\r\nThis is more advande example where key for encryption is protected with RSA. RSA is asymetric encryption where public key is used for encryption your data and for decryption is used private key, which is in most time also protected by password. :warning: Do not share your private key with anyone!\r\n\r\n#### Initialize RSA keys\r\n\r\n```csharp\r\nRSA rsa = new RSA(RSA.KEY_SIZE);\r\n\r\nstring pubKey = TextConversion.Base64Encode(rsa.GetPublicKey());\r\nstring privKey = TextConversion.Base64Encode(rsa.GetPrivateKey());\r\n```\r\n\r\nInitialize key and aad for GCM encryption\r\n\r\n```csharp\r\n// Create AES Keys\r\nbyte[] key = new byte[16];\r\nRandomNumberGenerator.Fill(key);\r\n\r\nbyte[] aad = new byte[32];\r\nRandomNumberGenerator.Fill(aad);\r\n```\r\n\r\nNow secure your key\r\n\r\n```csharp\r\nbyte[] encryptedAeskey = RSA.EncryptBytes(key, RSA.SetKeyFromString(pubKey));\r\n```\r\n\r\nbefore using it you have to decrypt it\r\n\r\n```csharp\r\nbyte[] decryptedAesKey = RSA.DecryptBytes(encryptedAeskey, RSA.SetKeyFromString(privKey));\r\n```\r\n\r\nKey above was secured with asymmetric cryptography. **Never share your private key with anyone.**\r\n\r\nNow encryption is simmilar as in our first example\r\n\r\n```csharp\r\nbyte[] encryptedData = GCM.Encrypt(dataToEncrypt, key, aad);\r\nbyte[] decryptedData = GCM.Decrypt(encryptedData, decryptedAesKey, aad);\r\n```\r\n\r\nIf you want to encrypt string you have to do it as follows\r\n\r\n```csharp\r\nbyte[] encryptedStringData = GCM.Encrypt(Encoding.UTF8.GetBytes(stringToEncrypt), key, aad);\r\n```\r\n\r\nFor decryption is it same as above.\r\n\r\n## Sign and verify data using RSA (a.k.a. Digital signature)\r\n\r\n- Sign data using private key\r\n- Verify data using public key\r\n- Usually used in cases where it is important to detect forgery or tampering\r\n- Provides cryptographic way of Authentication, Integrity, Non-repudiation\r\n\r\nFor more information check this [Wikipedia](https://en.wikipedia.org/wiki/Digital_signature) page.\r\n\r\n### Initialize RSA parameters (a.k.a. get your keys)\r\n\r\nFirst you will need to get your public and private key. You can do this as in  [RSA encryption](#rsa-encryption-asymmetric-one). Or you can use random generated one as follows:\r\n\r\n```csharp\r\nRSA rsa = new RSA(RSA.KEY_SIZE);\r\nRSAParameters key = rsa.GetRSAParameters(true);\r\n```\r\n\r\nmethod above will be available from version `1.3.0` for all RSA related tasks.\r\n\r\nNow you can Sign your data like follows. (You are signing your data with your private key)\r\n\r\n```csharp\r\nbyte[] signedData = RSA.HashAndSignBytes(dataToSign, key);\r\n\r\n// if you using method with keys provided from string use\r\nbyte[] signedData = RSA.HashAndSignBytes(dataToSign, RSA.SetKeyFromString(privKey));\r\n```\r\n\r\nAnd for verification use following lines (You are verifying your data with your Public key)\r\n\r\n```csharp\r\n// will be TRUE if your data wasn't modified from time of your signature, otherwise it will be FALSE\r\nbool isVerified = RSA.VerifySignedHash(dataToSign, key);\r\n\r\n// if you using method with keys provided from string use\r\nbool isVerified = RSA.VerifySignedHash(dataToSign, RSA.SetKeyFromString(pubKey));\r\n```\r\n\r\n## Key derivation with PBKDF2\r\n\r\nThis function is used to derive you key (for example for unlocking private key) from your password. You can read more about it on [Wikipedia](https://en.wikipedia.org/wiki/PBKDF2)\r\n\r\nto derive key use following snippet\r\n\r\n```csharp\r\n// string password = \"my$up3r$3cr3tP4$$w0rd1\";\r\n// string salt = \"8VySCxa42j9McSqGjZxCVQnH4x4rSZszEL9YQT3VkZ75xbBD\";\r\nvar derivedKey = PBKDF2.keyDerivate(password, salt, 1024, 10);\r\n```\r\n\r\nLicense MIT\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaymeow%2Fmaymeow.cryptography","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaymeow%2Fmaymeow.cryptography","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaymeow%2Fmaymeow.cryptography/lists"}