public static Decrypt ( byte buffer ) : byte[] | ||
buffer | byte | |
return | byte[] |
public void EncryptTest() { var aesEncryptor = new AesEncryptor(Convert.ToBase64String(Encoding.GetEncoding("utf-8").GetBytes("1234567890abcdef"))); aesEncryptor.Mode = CipherMode.CBC; aesEncryptor.Padding = PaddingMode.PKCS7; aesEncryptor.KeySize = 128; var encryptedStr = aesEncryptor.Encrypt("helloworld"); var actual = aesEncryptor.Decrypt(encryptedStr); Assert.Equal("helloworld", actual); //加密后的二进制 var encryptedData = aesEncryptor.Encrypt(Encoding.UTF8.GetBytes("helloworld")); Assert.Equal(encryptedStr, Convert.ToBase64String(encryptedData)); //加密的二进制 var source = aesEncryptor.Decrypt(encryptedData); var actualSourceString = Encoding.GetEncoding("utf-8").GetString(source); Assert.Equal("helloworld", actualSourceString); }
private static bool GetObject <T>(string key, out T target, bool encrypted) { string str = PlayerPrefs.GetString(key); if (!string.IsNullOrEmpty(str)) { try { if (encrypted) { str = AesEncryptor.Decrypt(str); } target = JsonUtils.Deserialize <T>(str, true); return(true); } catch (Exception) { Debug.LogError("Cannot get object from PlayerPrefs: " + key); } } else { Debug.LogError("Player field not in PlayerPrefs: " + key); } target = default(T); return(false); }
public void TestAes5_Bytes() { int length = 100; var rand = new RandomGenerator(); //byte[] entropy = new byte[20]; for (int i = 0; i < length; i++) { byte[] newKey = new byte[rand.RandomNumber(75, 88)]; using (var rng = new RNGCryptoServiceProvider()) { //rng.GetBytes(entropy); rng.GetBytes(newKey); } var stringToEncrypt = Guid.NewGuid().ToString("N") + ":* d’une secrétairE chargée des affaires des étudiants de la section"; var bytes = System.Text.Encoding.UTF8.GetBytes(stringToEncrypt); // base 64 var encryptedBytes = AesEncryptor.Encrypt5(bytes, newKey); var decryptedBytes = AesEncryptor.Decrypt(encryptedBytes, newKey); Assert.IsTrue(decryptedBytes.SequenceEqual(bytes)); var decryptedString = System.Text.Encoding.UTF8.GetString(decryptedBytes); Assert.AreEqual(stringToEncrypt, decryptedString); } }
public void TestAes20000() { int length = 100; var rand = new RandomGenerator(); var stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < length; i++) { using (var password = rand.RandomSecureStringPassword(10, 50)) { var stringToEncrypt = Guid.NewGuid().ToString("N") + ":* d’une secrétairE chargée des affaires des étudiants de la section"; // base 64 var encryptedBase64 = AesEncryptor.Encrypt20000(stringToEncrypt, password); var decryptedBase64 = AesEncryptor.Decrypt(encryptedBase64, password); Assert.AreEqual(stringToEncrypt, decryptedBase64); // base 36 var encryptedBase36 = AesEncryptor.Encrypt20000(stringToEncrypt, password, true); var decryptedBase36 = AesEncryptor.Decrypt(encryptedBase36, password, true); Assert.AreEqual(stringToEncrypt, decryptedBase36); Console.WriteLine(string.Format("{0:N0}\t{1}", i, stopwatch.Elapsed)); stopwatch.Stop(); stopwatch.Reset(); stopwatch.Start(); } } }
public void TestProtectPassword() { var cert2 = LoadCertificate(); var publicKey = new X509CertificatePublicKey(cert2); var privateKey = new X509Certificate2KeyEncryptionKey(cert2); int length = 100; var rand = new RandomGenerator(); for (int i = 0; i < length; i++) { using (var password = rand.RandomSecureStringPassword(10, 50)) { var stringToEncrypt = Guid.NewGuid().ToString("N") + ":* d’une secrétairE chargée des affaires des étudiants de la section"; // base 64 var encryptedBase64 = AesEncryptor.Encrypt1000(stringToEncrypt, password); var decryptedBase64 = AesEncryptor.Decrypt(encryptedBase64, password); Assert.AreEqual(stringToEncrypt, decryptedBase64); // base 36 var encryptedBase36 = AesEncryptor.Encrypt1000(stringToEncrypt, password, true); var decryptedBase36 = AesEncryptor.Decrypt(encryptedBase36, password, true); Assert.AreEqual(stringToEncrypt, decryptedBase36); var protectedPwStr = AsymmetricEncryptor.EncryptToBase64StringAsync(password, publicKey).GetAwaiter().GetResult(); var unprotectedPwdStr = AsymmetricEncryptor.DecryptFromBase64StringAsync(protectedPwStr, privateKey).GetAwaiter().GetResult(); var decryptedString = AesEncryptor.Decrypt(encryptedBase64, unprotectedPwdStr); Assert.AreEqual(stringToEncrypt, decryptedString); } } }
public void EncryptTest() { var aesEncryptor = new AesEncryptor(Convert.ToBase64String(Encoding.GetEncoding("utf-8").GetBytes("1234567890abcdef"))); var encryptedStr = aesEncryptor.Encrypt("helloworld"); var actual = aesEncryptor.Decrypt(encryptedStr); Assert.Equal("helloworld", actual); }
void Load() { if (File.Exists(saveFilePath)) { //saveFile.SetFromJson(File.ReadAllText(saveFilePath)); saveFile.SetFromJson(encryptor.Decrypt(File.ReadAllBytes(saveFilePath), JSON_ENCRYPTED_KEY, JSON_ENCRYPTED_IV)); } }
public PasswordVerificationResult VerifyHashedPassword(ApplicationUser user, string hashedPassword, string providedPassword) { var decryptedPassword = AesEncryptor.Decrypt(hashedPassword); if (string.CompareOrdinal(decryptedPassword, providedPassword) == 0) { return(PasswordVerificationResult.Success); } return(PasswordVerificationResult.Failed); }
public void AesEncryptorTest() { string encryptedData = string.Empty; using (var aes = new AesEncryptor()) encryptedData = aes.Encrypt("my data", "my entropy"); using (var aes = new AesEncryptor()) Assert.AreEqual("my data", aes.Decrypt(encryptedData, "my entropy")); }
private static void Main() { var data = "test text"; var password = "******"; var salt = "salt"; var aes = new AesEncryptor(); var encrypted = aes.Encrypt(data, password, salt); var decrypted = aes.Decrypt(encrypted, password, salt); Console.WriteLine($"{decrypted} = {encrypted}"); }
public void svLoginEncrypted() { string parent = System.IO.Directory.GetParent("..").FullName; string dir = File.ReadAllText(parent + "/gokResources/c.gok"); string decode = AesEncryptor.Decrypt(dir); connectionString = decode; connection = new MySqlConnection(connectionString); }
public void EncryptAndDecryptString(string inputString) { byte[] key = { 0xCE, 0x95, 0x11, 0x33, 0x23, 0xFC, 0xAC, 0xCD, 0xCC, 0x41, 0x88, 0x99, 0x61, 0xF9, 0xC4, 0x33 }; var aes = new AesEncryptor(); var encrypted = aes.Encrypt(inputString, key); var encryptedString = Convert.ToBase64String(encrypted); var decryptedString = aes.Decrypt(Convert.FromBase64String(encryptedString), key); Assert.AreEqual(inputString, decryptedString); }
/// <summary> /// Decrypts the wallet data, verifies checksum and returns the private key secret. /// </summary> /// <param name="passphrase">Passphrase to decrypt the wallet.</param> /// <returns>The decrypted secret, or null if decryption or checksum failed.</returns> public BigInteger?TryGetSecret(string passphrase) { using var sha256 = SHA256.Create(); var ppBytes = Encoding.UTF8.GetBytes(passphrase); var ppHash = sha256.ComputeHash(ppBytes); var decryptedPayload = AesEncryptor.Decrypt(payload, ppHash); var decryptedChecksum = sha256.ComputeHash(decryptedPayload); if (equalBytes(decryptedChecksum, checksum)) { return(new BigInteger(decryptedPayload)); } else { return(null); } }
public void should_correctly_encrypt_and_decrypt_data() { var expectedBytes = Encoding.UTF8.GetBytes("This is what was requested"); Console.WriteLine("default: {0}", BitConverter.ToString(expectedBytes)); var encryptor = new AesEncryptor("some-password"); var encryptedData = encryptor.Encrypt(expectedBytes); Console.WriteLine("encrypted: {0}", BitConverter.ToString(encryptedData)); var decryptedData = encryptor.Decrypt(encryptedData); Console.WriteLine("decrypted: {0}", BitConverter.ToString(decryptedData)); Assert.Equal(expectedBytes, decryptedData); }
public static byte[] LoadBytesFromPersistentStorage(string filename) { try { string path = Path.Combine(Application.persistentDataPath, filename); byte[] buffer = null; if (ConfigApp.PersistentStorageEncryptionEnabled) { buffer = AesEncryptor.Decrypt(File.ReadAllBytes(path)); } else { buffer = File.ReadAllBytes(path); } return(buffer); } catch (Exception) { return(null); } }
public void TestAes200() { int length = 100; var rand = new RandomGenerator(); for (int i = 0; i < length; i++) { using (var password = rand.RandomSecureStringPassword(10, 50)) { var stringToEncrypt = Guid.NewGuid().ToString("N") + ":* d’une secrétairE chargée des affaires des étudiants de la section"; // base 64 var encryptedBase64 = AesEncryptor.Encrypt200(stringToEncrypt, password); var decryptedBase64 = AesEncryptor.Decrypt(encryptedBase64, password); Assert.AreEqual(stringToEncrypt, decryptedBase64); // base 36 var encryptedBase36 = AesEncryptor.Encrypt200(stringToEncrypt, password, true); var decryptedBase36 = AesEncryptor.Decrypt(encryptedBase36, password, true); Assert.AreEqual(stringToEncrypt, decryptedBase36); } } }
public string ApplyFormat(string name, object rawValue, Func <string, string, string, object> argumentsCallback) { var pass = argumentsCallback(name, "decrypt", "password"); var useRawKey = argumentsCallback(name, "decrypt", "useRawKey"); var keyIsRaw = useRawKey is bool db && db; string retVal = rawValue?.ToString(); if (!string.IsNullOrEmpty(retVal)) { bool success = false; if (pass is string pwd && !string.IsNullOrEmpty(pwd)) { try { retVal = AesEncryptor.Decrypt(retVal, Convert.FromBase64String(pwd), !keyIsRaw); success = true; } catch { } } else if (pass is byte[] pwb && pwb.Length != 0) { try { retVal = AesEncryptor.Decrypt(retVal, pwb, !keyIsRaw); success = true; } catch { } } if (!success) { retVal = retVal.Decrypt(); } return(retVal); }
public static string LoadFromEditorDataPath(string filename, bool encrypted) { try { string path = Path.Combine(Application.dataPath, filename); byte[] bytes = null; if (encrypted) { bytes = AesEncryptor.Decrypt(File.ReadAllBytes(path)); } else { bytes = File.ReadAllBytes(path); } return(Encoding.UTF8.GetString(bytes)); } catch (Exception exception) { Debug.LogError(string.Concat(new object[] { "Cannot load '", filename, "' from editor data path: ", exception })); } return(null); }
public void TestAes20000_Bytes() { int length = 100; var rand = new RandomGenerator(); //byte[] entropy = new byte[20]; var stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < length; i++) { byte[] newKey = new byte[rand.RandomNumber(75, 88)]; using (var rng = new RNGCryptoServiceProvider()) { //rng.GetBytes(entropy); rng.GetBytes(newKey); } var stringToEncrypt = Guid.NewGuid().ToString("N") + ":* d’une secrétairE chargée des affaires des étudiants de la section"; var bytes = System.Text.Encoding.UTF8.GetBytes(stringToEncrypt); // base 64 var encryptedBytes = AesEncryptor.Encrypt20000(bytes, newKey); var decryptedBytes = AesEncryptor.Decrypt(encryptedBytes, newKey); Assert.IsTrue(decryptedBytes.SequenceEqual(bytes)); var decryptedString = System.Text.Encoding.UTF8.GetString(decryptedBytes); Assert.AreEqual(stringToEncrypt, decryptedString); Console.WriteLine(string.Format("{0:N0}\t{1}", i, stopwatch.Elapsed)); stopwatch.Stop(); stopwatch.Reset(); stopwatch.Start(); } }
public static byte[] DecodePayload(Payload payload, string secret) { var keyBytes = KeyDerivedBytes.GetBytes(secret, AesEncryptor.KeySize); return(AesEncryptor.Decrypt(payload.EncryptedData, keyBytes, payload.Vector)); }
//[ReflectionProtect(typeof(byte[]))] protected override byte[] InternalDecrypt(byte[] encryptedData, byte[] entropy) { ProtectedMemory.Unprotect(encryptedData, MemoryProtectionScope.SameProcess); return(aes.Decrypt(encryptedData, entropy?.Length > 0 ? entropy : randomEntropy)); }
public void SetUp() { var subject = new AesEncryptor("SOME_KEY"); _result = subject.Decrypt("ABZnmFTW8EcZLGqcDk2aQQ=="); }
/// <summary> /// /// </summary> /// private List <string> CheckLDate() { try { Encryptor en = new AesEncryptor(); using (RegistryKey skey = Registry.LocalMachine.OpenSubKey("Software", true)) { using (RegistryKey key = skey.OpenSubKey("IdentaMaster", RegistryKeyPermissionCheck.ReadWriteSubTree)) { byte[] entropy = { 0x00, 0x01, 0x02, 0x01, 0x00, 0x04, 0xBB, 0x17, 0x8b, 0xf6, 0xa2, 0x15, 0xe2, 0x64, 0x11, 0x9a }; var appsetString = System.Text.Encoding.Default.GetString(en.Decrypt((byte[])key.GetValue("AppSet"), entropy)); int indof0 = appsetString.IndexOf("^^^"); string cleanAppsetString = appsetString.Substring(0, indof0); string[] Separ = { "..." }; appSet = new List <string>(cleanAppsetString.Split(Separ, StringSplitOptions.None)); if (appSet.Count > 0) { return(appSet); } else { throw new Exception("noDate"); } } } } catch (Exception E) { Log.Error(E.Message); return(appSet); } }
public string Deconvert(string value) { return(encryptor.Decrypt(value)); }