Advertisement
panxop

Untitled

May 11th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.47 KB | None | 0 0
  1. //LLamado
  2.  
  3. Dim pass As String = Encript.GeneraPassword(TxtNIT.Text.ToString().Replace(",", "").Replace(".", "").Replace("_", "").Trim())
  4.             Dim pass_encrip As String = eas256.EncryptText("AES256", pass)
  5.  
  6. //Genera Clave Aleatoria
  7.  
  8. Public Function GeneraPassword(ByVal rut As String) As String
  9.         Dim passwordletras As String = ""
  10.         Dim Clave As String, x As Integer, Pass2 As String
  11.         Dim CAR As String, Codigo As String, valor As String, Temp As String
  12.  
  13.         ' semilla entragada por bbva
  14.         Dim semilla As String = ConfigurationManager.AppSettings("Semilla").ToString
  15.  
  16.         Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  17.         Dim n As String = "1234567890"
  18.  
  19.         semilla = RandomNum(semilla)
  20.         Dim r As New Random(semilla)
  21.  
  22.         For i As Integer = 1 To 6
  23.             Dim idx As Integer = r.Next(0, 52)
  24.             passwordletras = passwordletras & s.Substring(idx, 1)
  25.         Next
  26.  
  27.  
  28.         For i As Integer = 1 To 4
  29.             Dim idx As Integer = r.Next(0, 10)
  30.             passwordletras = passwordletras & n.Substring(idx, 1)
  31.         Next
  32.  
  33.         Clave = rut
  34.         Pass2 = ""
  35.         For x = 1 To Len(passwordletras)
  36.             CAR = Mid(passwordletras, x, 1)
  37.             Codigo = Mid(Clave, ((x - 1) Mod Len(Clave)) + 1, 1)
  38.             valor = Right("2" & Hex(Asc(Codigo) Xor Asc(CAR)), 2)
  39.             Temp = Valida_HEX(valor)
  40.             Pass2 = Pass2 & Temp
  41.         Next
  42.  
  43.         Dim Final_Pass As String = ""
  44.         For x = 0 To Pass2.Length - 1 Step 2
  45.             Final_Pass &= ChrW(CInt("&H" & Pass2.Substring(x, 2)))
  46.         Next
  47.  
  48.         Return Final_Pass
  49.  
  50.  
  51.  
  52.     End Function
  53.  
  54.  
  55.  
  56. //Clase que encripta la contraseña
  57.  
  58.  
  59. using System;
  60. using System.Collections.Generic;
  61. using System.Text;
  62. using System.Security.Cryptography;
  63. using System.IO;  
  64.  
  65. namespace LibAES256
  66. {
  67.     public class AES256
  68.     {
  69.         public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
  70.         {
  71.             byte[] encryptedBytes = null;
  72.  
  73.             // Set your salt here, change it to meet your flavor:
  74.             // The salt bytes must be at least 8 bytes.
  75.             byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  76.  
  77.             using (MemoryStream ms = new MemoryStream())
  78.             {
  79.                 using (RijndaelManaged AES = new RijndaelManaged())
  80.                 {
  81.                     AES.KeySize = 256;
  82.                     AES.BlockSize = 128;
  83.  
  84.                     var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
  85.                     AES.Key = key.GetBytes(AES.KeySize / 8);
  86.                     AES.IV = key.GetBytes(AES.BlockSize / 8);
  87.  
  88.                     AES.Mode = CipherMode.CBC;
  89.  
  90.                     using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
  91.                     {
  92.                         cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
  93.                         cs.Close();
  94.                     }
  95.                     encryptedBytes = ms.ToArray();
  96.                 }
  97.             }
  98.  
  99.             return encryptedBytes;
  100.         }
  101.  
  102.         public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
  103.         {
  104.             byte[] decryptedBytes = null;
  105.  
  106.             // Set your salt here, change it to meet your flavor:
  107.             // The salt bytes must be at least 8 bytes.
  108.             byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  109.  
  110.             using (MemoryStream ms = new MemoryStream())
  111.             {
  112.                 using (RijndaelManaged AES = new RijndaelManaged())
  113.                 {
  114.                     AES.KeySize = 256;
  115.                     AES.BlockSize = 128;
  116.  
  117.                     var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
  118.                     AES.Key = key.GetBytes(AES.KeySize / 8);
  119.                     AES.IV = key.GetBytes(AES.BlockSize / 8);
  120.  
  121.                     AES.Mode = CipherMode.CBC;
  122.  
  123.                     using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
  124.                     {
  125.                         cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
  126.                         cs.Close();
  127.                     }
  128.                     decryptedBytes = ms.ToArray();
  129.                 }
  130.             }
  131.  
  132.             return decryptedBytes;
  133.         }
  134.  
  135.         public string EncryptText(string input, string password)
  136.         {
  137.             // Get the bytes of the string
  138.             byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);
  139.             byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
  140.  
  141.             // Hash the password with SHA256
  142.             passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
  143.  
  144.             byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
  145.  
  146.             string result = Convert.ToBase64String(bytesEncrypted);
  147.  
  148.             return result;
  149.         }
  150.  
  151.         public string DecryptText(string input, string password)
  152.         {
  153.             // Get the bytes of the string
  154.             byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
  155.             byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
  156.             passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
  157.  
  158.             byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
  159.  
  160.             string result = Encoding.UTF8.GetString(bytesDecrypted);
  161.  
  162.             return result;
  163.         }
  164.  
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement