コード例 #1
0
        public void TestAESWithEverything()
        {
            // string original = "Here is some data to encrypt!";

            byte[] plain = new byte[] { 0x01, 0x02 };

            // Create a new instance of the Aes
            // class.  This generates a new key and initialization
            // vector (IV).
            using (Aes myAes = Aes.Create())
            {
                AesManager aesManager = AesManager.Current;

                byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
                byte[] iv  = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05 };
                // Encrypt the string to an array of bytes.
                byte[] encrypted = aesManager.Encryptdata(plain, key, iv);

                // Decrypt the bytes to a string.

                //Display the original data and the decrypted data.
                Assert.Equal("f65f", encrypted.ToHex());
                Assert.Equal(plain, aesManager.Decryptdata(encrypted, key, iv));
            }
        }
コード例 #2
0
 public static string FromPassword(string password)
 {
     if (password.Length > 20 || password.Length < 8)
     {
         return(JsonConvert.SerializeObject(""));
     }
     else
     {
         KeyPair   keyPair   = KeyPair.generateEd25519KeyPair();
         PublicKey publicKey = keyPair.GetPublicKey();
         byte[]    salt      = new byte[saltLength];
         byte[]    iv        = new byte[ivLength];
         Org.BouncyCastle.Security.SecureRandom random = new Org.BouncyCastle.Security.SecureRandom();
         random.NextBytes(iv);
         Org.BouncyCastle.Security.SecureRandom sr = new Org.BouncyCastle.Security.SecureRandom();
         sr.NextBytes(salt);
         Argon2Manager argon2Manager = Argon2Manager.Current;
         byte[]        derivedKey    = argon2Manager.hash(System.Text.Encoding.ASCII.GetBytes(password), salt);
         AesManager    aes           = AesManager.Current;
         byte[]        cipherPrivKey = aes.Encryptdata(keyPair.GetPrivateKey().getBytes(), derivedKey, iv);
         Sha3Keccack   sha3Keccack   = Sha3Keccack.Current;
         byte[]        mac           = sha3Keccack.CalculateHash(Utils.Combine(derivedKey, cipherPrivKey));
         Crypto        crypto        = new Crypto(
             AesManager.cipher, new string(cipherPrivKey.ToHex()),
             new Cipherparams(
                 new string(iv.ToHex())
                 )
             );
         Kdfparams kdfparams = new Kdfparams(Argon2Manager.memoryCost, Argon2Manager.timeCost, Argon2Manager.parallelism, new string(salt.ToHex()));
         Address   address   = new Address(publicKey);
         Keystore  ks        = new Keystore(address.getAddress(), crypto, Utils.generateUUID(),
                                            newVersion, new string(mac.ToHex()), argon2Manager.kdf(), kdfparams
                                            );
         return(JsonConvert.SerializeObject(ks));
     }
 }