Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //LLamado
- Dim pass As String = Encript.GeneraPassword(TxtNIT.Text.ToString().Replace(",", "").Replace(".", "").Replace("_", "").Trim())
- Dim pass_encrip As String = eas256.EncryptText("AES256", pass)
- //Genera Clave Aleatoria
- Public Function GeneraPassword(ByVal rut As String) As String
- Dim passwordletras As String = ""
- Dim Clave As String, x As Integer, Pass2 As String
- Dim CAR As String, Codigo As String, valor As String, Temp As String
- ' semilla entragada por bbva
- Dim semilla As String = ConfigurationManager.AppSettings("Semilla").ToString
- Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
- Dim n As String = "1234567890"
- semilla = RandomNum(semilla)
- Dim r As New Random(semilla)
- For i As Integer = 1 To 6
- Dim idx As Integer = r.Next(0, 52)
- passwordletras = passwordletras & s.Substring(idx, 1)
- Next
- For i As Integer = 1 To 4
- Dim idx As Integer = r.Next(0, 10)
- passwordletras = passwordletras & n.Substring(idx, 1)
- Next
- Clave = rut
- Pass2 = ""
- For x = 1 To Len(passwordletras)
- CAR = Mid(passwordletras, x, 1)
- Codigo = Mid(Clave, ((x - 1) Mod Len(Clave)) + 1, 1)
- valor = Right("2" & Hex(Asc(Codigo) Xor Asc(CAR)), 2)
- Temp = Valida_HEX(valor)
- Pass2 = Pass2 & Temp
- Next
- Dim Final_Pass As String = ""
- For x = 0 To Pass2.Length - 1 Step 2
- Final_Pass &= ChrW(CInt("&H" & Pass2.Substring(x, 2)))
- Next
- Return Final_Pass
- End Function
- //Clase que encripta la contraseña
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Security.Cryptography;
- using System.IO;
- namespace LibAES256
- {
- public class AES256
- {
- public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
- {
- byte[] encryptedBytes = null;
- // Set your salt here, change it to meet your flavor:
- // The salt bytes must be at least 8 bytes.
- byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
- using (MemoryStream ms = new MemoryStream())
- {
- using (RijndaelManaged AES = new RijndaelManaged())
- {
- AES.KeySize = 256;
- AES.BlockSize = 128;
- var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
- AES.Key = key.GetBytes(AES.KeySize / 8);
- AES.IV = key.GetBytes(AES.BlockSize / 8);
- AES.Mode = CipherMode.CBC;
- using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
- {
- cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
- cs.Close();
- }
- encryptedBytes = ms.ToArray();
- }
- }
- return encryptedBytes;
- }
- public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
- {
- byte[] decryptedBytes = null;
- // Set your salt here, change it to meet your flavor:
- // The salt bytes must be at least 8 bytes.
- byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
- using (MemoryStream ms = new MemoryStream())
- {
- using (RijndaelManaged AES = new RijndaelManaged())
- {
- AES.KeySize = 256;
- AES.BlockSize = 128;
- var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
- AES.Key = key.GetBytes(AES.KeySize / 8);
- AES.IV = key.GetBytes(AES.BlockSize / 8);
- AES.Mode = CipherMode.CBC;
- using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
- {
- cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
- cs.Close();
- }
- decryptedBytes = ms.ToArray();
- }
- }
- return decryptedBytes;
- }
- public string EncryptText(string input, string password)
- {
- // Get the bytes of the string
- byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);
- byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
- // Hash the password with SHA256
- passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
- byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
- string result = Convert.ToBase64String(bytesEncrypted);
- return result;
- }
- public string DecryptText(string input, string password)
- {
- // Get the bytes of the string
- byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
- byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
- passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
- byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
- string result = Encoding.UTF8.GetString(bytesDecrypted);
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement