Hola 🙂 en esta post se deja a disposición de la comunidad de programadores que quiera aprender un poquito sobre Facturación Virtual, el generador de Código de Control versión 7.0 construido según la documentación provista por Impuestos Nacionales de Bolivia (SIN).
¿Que es C#?
C# es un lenguaje de programación que se ha diseñado para compilar diversas aplicaciones que se ejecutan en .NET Framework. C# es simple, eficaz, con seguridad de tipos y orientado a objetos. Las numerosas innovaciones de C# permiten desarrollar aplicaciones rápidamente y mantener la expresividad y elegancia de los lenguajes de estilo de C.
Recordemos los algoritmos utilizados por el generador de Código de Control son:
Nuestro proyecto es el siguiente:
El código del generador se encuentra en el proyecto ControlCodeV7, las clases son:
AllegedRC4.cs
public static class AllegedRC4 { /// <summary> /// Criptografia simetrica /// <see cref="http://jc-mouse.net/"/> /// <param name="message">texto a encriptar</param> /// <param name="key">llave para encriptacion</param> /// <param name="unscripted">con guion o sin guion</param> /// <returns>mensaje encriptado</returns> /// </summary> public static String encryptMessageRC4(String message, String key, Boolean unscripted) { int[] state = new int[256]; int x = 0; int y = 0; int index1 = 0; int index2 = 0; int nmen; String messageEncryption = ""; for (int i = 0; i <= 255; i++) { state[i] = i; } for (int i = 0; i <= 255; i++) { index2 = (((int)key[index1]) + state[i] + index2) % 256; int aux = state[i]; state[i] = state[index2]; state[index2] = aux; index1 = (index1 + 1) % key.Length; } for (int i = 0; i < message.Length; i++) { x = (x + 1) % 256; y = (state[x] + y) % 256; int aux = state[x]; state[x] = state[y]; state[y] = aux; nmen = ((int)message[i]) ^ state[(state[x] + state[y]) % 256]; String nmenHex = nmen.ToString("X").ToUpper(); messageEncryption = messageEncryption + ((unscripted)?"":"-") + ((nmenHex.Length == 1) ? ("0" + nmenHex) : nmenHex); } return (unscripted)?messageEncryption:messageEncryption.Substring(1, messageEncryption.Length-1); } }
Base64SIN.cs
public class Base64SIN { /// <summary> /// Codificador base64 /// <see cref="http://jc-mouse.net/"/> /// <param name="value">Numero a codificar</param> /// <returns>Cadena codificada</returns> /// </summary> public static String convertBase64(int value) { String[] dictionary = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "+", "/" }; int quotient = 1; int remainder; String word = ""; while (quotient > 0) { quotient = value / 64; remainder = value % 64; word = dictionary[remainder] + word; value = quotient; } return word; } }
Verhoeff.cs
/// <summary> /// For more information cf. http://en.wikipedia.org/wiki/Verhoeff_algorithm /// Dihedral Group stuff: http://en.wikipedia.org/wiki/Dihedral_group /// Dihedral Group order 10: http://mathworld.wolfram.com/DihedralGroupD5.html /// </summary> public static class Verhoeff { // The multiplication table static int[,] d = new int[,] { {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 0, 6, 7, 8, 9, 5}, {2, 3, 4, 0, 1, 7, 8, 9, 5, 6}, {3, 4, 0, 1, 2, 8, 9, 5, 6, 7}, {4, 0, 1, 2, 3, 9, 5, 6, 7, 8}, {5, 9, 8, 7, 6, 0, 4, 3, 2, 1}, {6, 5, 9, 8, 7, 1, 0, 4, 3, 2}, {7, 6, 5, 9, 8, 2, 1, 0, 4, 3}, {8, 7, 6, 5, 9, 3, 2, 1, 0, 4}, {9, 8, 7, 6, 5, 4, 3, 2, 1, 0} }; // The permutation table static int[,] p = new int[,] { {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 5, 7, 6, 2, 8, 3, 0, 9, 4}, {5, 8, 0, 3, 7, 9, 6, 1, 4, 2}, {8, 9, 1, 6, 0, 4, 3, 5, 2, 7}, {9, 4, 5, 3, 1, 2, 6, 8, 7, 0}, {4, 2, 8, 6, 5, 7, 3, 9, 0, 1}, {2, 7, 9, 3, 8, 0, 6, 4, 1, 5}, {7, 0, 4, 6, 9, 1, 3, 2, 5, 8} }; // The inverse table static int[] inv = { 0, 4, 3, 2, 1, 5, 6, 7, 8, 9 }; /// <summary> /// Validates that an entered number is Verhoeff compliant. /// NB: Make sure the check digit is the last one! /// </summary> /// <param name="num"></param> /// <returns>True if Verhoeff compliant, otherwise false</returns> public static bool validateVerhoeff(string num) { int c = 0; int[] myArray = StringToReversedIntArray(num); for (int i = 0; i < myArray.Length; i++) { c = d[c, p[(i % 8), myArray[i]]]; } return c == 0; } /// <summary> /// For a given number generates a Verhoeff digit /// Append this check digit to num /// </summary> /// <param name="num"></param> /// <returns>Verhoeff check digit as string</returns> public static string generateVerhoeff(string num) { int c = 0; int[] myArray = StringToReversedIntArray(num); for (int i = 0; i < myArray.Length; i++) { c = d[c, p[((i + 1) % 8), myArray[i]]]; } return inv[c].ToString(); } /// <summary> /// Converts a string to a reversed integer array. /// </summary> /// <param name="num"></param> /// <returns>Reversed integer array</returns> private static int[] StringToReversedIntArray(string num) { int[] myArray = new int[num.Length]; for (int i = 0; i < num.Length; i++) { try { myArray[i] = int.Parse(num.Substring(i, 1)); } catch (FormatException e) { if (e.Source != null) Console.WriteLine("FormatException source: {0}", e.Source); throw; } } Array.Reverse(myArray); return myArray; } }
ControlCode.cs
public static class ControlCode { /// <summary> /// Genera codigo de control /// <see cref="http://jc-mouse.net/"/> /// <param name="authorizationNumber">Numero de autorizacion</param> /// <param name="invoiceNumber">Numero de factura</param> /// <param name="nitci">Número de Identificación Tributaria o Carnet de Identidad</param> /// <param name="dateOfTransaction">fecha de transaccion de la forma AAAAMMDD</param> /// <param name="transactionAmount">Monto de la transacción</param> /// <param name="dosageKey">Llave de dosificación</param> /// <returns>Codigo de Control generado de la forma 6A-DC-53-05-14</returns> /// </summary> public static String generateControlCode(String authorizationNumber, String invoiceNumber, String nitci, String dateOfTransaction, String transactionAmount, String dosageKey) { //redondea monto de transaccion transactionAmount = roundUp(transactionAmount); /* ========== PASO 1 ============= */ invoiceNumber = addVerhoeffDigit(invoiceNumber, 2); nitci = addVerhoeffDigit(nitci, 2); dateOfTransaction = addVerhoeffDigit(dateOfTransaction, 2); transactionAmount = addVerhoeffDigit(transactionAmount, 2); //se suman todos los valores obtenidos Int64 sumOfVariables=0; try { sumOfVariables = Int64.Parse(invoiceNumber) + Int64.Parse(nitci) + Int64.Parse(dateOfTransaction) + Int64.Parse(transactionAmount); } catch (FormatException e) { if (e.Source != null) Console.WriteLine("FormatException source: {0}", e.Source); throw; } //A la suma total se añade 5 digitos Verhoeff String sumOfVariables5Verhoeff = addVerhoeffDigit(sumOfVariables.ToString(), 5); /* ========== PASO 2 ============= */ String fiveDigitsVerhoeff = sumOfVariables5Verhoeff.Substring(sumOfVariables5Verhoeff.Length - 5); int[] numbers = new int[5]; for (int i = 0; i < 5; i++) { numbers[i] = Int32.Parse(fiveDigitsVerhoeff[i].ToString()) + 1; } String string1 = dosageKey.Substring(0, numbers[0]); String string2 = dosageKey.Substring(numbers[0], numbers[1]); String string3 = dosageKey.Substring(numbers[0] + numbers[1], numbers[2]); String string4 = dosageKey.Substring(numbers[0] + numbers[1] + numbers[2], numbers[3]); String string5 = dosageKey.Substring(numbers[0] + numbers[1] + numbers[2] + numbers[3], numbers[4]); String authorizationNumberDKey = authorizationNumber + string1; String invoiceNumberdKey = invoiceNumber + string2; String NITCIDKey = nitci + string3; String dateOfTransactionDKey = dateOfTransaction + string4; String transactionAmountDKey = transactionAmount + string5; /* ========== PASO 3 ============= */ //se concatena cadenas de paso 2 String stringDKey = authorizationNumberDKey + invoiceNumberdKey + NITCIDKey + dateOfTransactionDKey + transactionAmountDKey; //Llave para cifrado + 5 digitos Verhoeff generado en paso 2 String keyForEncryption = dosageKey + fiveDigitsVerhoeff; //se aplica AllegedRC4 String allegedRC4String = AllegedRC4.encryptMessageRC4(stringDKey, keyForEncryption,true); /* ========== PASO 4 ============= */ //se suman valores ascii int totalAmount = 0; int sp1 = 0; int sp2 = 0; int sp3 = 0; int sp4 = 0; int sp5 = 0; byte[] asciiBytes = Encoding.ASCII.GetBytes(allegedRC4String); int tmp = 1; for (int i=0; i< asciiBytes.Length;i++) { totalAmount += asciiBytes[i]; switch (tmp) { case 1: sp1 += asciiBytes[i]; break; case 2: sp2 += asciiBytes[i]; break; case 3: sp3 += asciiBytes[i]; break; case 4: sp4 += asciiBytes[i]; break; case 5: sp5 += asciiBytes[i]; break; } tmp = (tmp < 5) ? tmp + 1 : 1; } /* ========== PASO 5 ============= */ //suma total * sumas parciales dividido entre resultados obtenidos //entre el dígito Verhoeff correspondiente más 1 (paso 2) int tmp1 = totalAmount * sp1 / numbers[0]; int tmp2 = totalAmount * sp2 / numbers[1]; int tmp3 = totalAmount * sp3 / numbers[2]; int tmp4 = totalAmount * sp4 / numbers[3]; int tmp5 = totalAmount * sp5 / numbers[4]; //se suman todos los resultados int sumProduct = tmp1 + tmp2 + tmp3 + tmp4 + tmp5; //se obtiene base64 String base64SIN = Base64SIN.convertBase64(sumProduct); /* ========== PASO 6 ============= */ //Aplicar el AllegedRC4 a la anterior expresión obtenida return AllegedRC4.encryptMessageRC4(base64SIN, String.Concat(dosageKey,fiveDigitsVerhoeff),false); } /// <summary> /// Añade N digitos Verhoeff a una cadena de texto /// <param name="value">cadena donde se añadiran digitos Verhoeff</param> /// <param name="max">cantidad de digitos a agregar</param> /// <returns>cadena original + N digitos Verhoeff</returns> /// </summary> private static String addVerhoeffDigit(String value, int max) { for (int i = 1; i <= max; i++) { value= String.Concat(value, Verhoeff.generateVerhoeff(value)); } return value; } /// <summary> /// Transforma string a numero y redondea hacia arriba /// <param name="value">String numero de la forma 123 | 123.45 | 123,45 </param> /// <returns>String con el numero redondeado sin decimales</returns> /// </summary> private static String roundUp(String value) { char a = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); value = (a == ',') ? value.Replace(".", ",") : (a=='.')? value.Replace(",", ".") : value; decimal decimalVal = System.Convert.ToDecimal(value); //redondea a 0 decimales return Math.Round(decimalVal, MidpointRounding.AwayFromZero).ToString(); } }
En el proyecto TestControlCode, implementamos el código necesario para testeat los 5000 casos que nos brinda Impuestos Nacionales. Por ejemplo para 1 caso podemos hacer lo siguiente:
String code = ControlCode.generateControlCode("29040011007", "1503", "4189179011", "20070702", "2500", "9rCB7Sv4X29d)5k7N%3ab89p-3(5[A" ); MessageBox.Show( code );
Eso es todo 🙂
Detalles de proyecto
enjoy!!!!
Yo soy yo :) JC Mouse, Soy orgullosamente boliviano soy fundador y CEO de la web jc-Mouse.net uno de las pocas web en emprendimiento y tecnología en Bolivia.
Toda la información que encuentres en este sitio es y sera completamente gratis siempre, puedes copiar, descargar y re-publicar si así lo deseas en otros blogs o sitios web, solo te pido a cambio que dejes una referencia a esta web. Esto nos ayuda a crecer y seguir aportando. Bye
Enjoy! :)
Material Design introduce una nueva forma de mostrar notificaciones al usuario, estos son los snackbar, similares a los...
Esta aplicacion permite escalar una imagen desde java sin perder las proporciones de la misma, utiliza SCALE_AREA_AVERAG...
En este post mostramos un ejemplo de uso de la interface ResultSetMetaData el cual se extiende Wrapper que es una interf...
Sinopsis del juego Bolivia, en unos años. Este hermoso país sudamericano se ha convertido en el mayor productor de cocaí...
En este tutorial conoceremos lo que es el uso de BitmapFont en LibGDX y algún uso que le podemos dar como crear un menú,...
Nivel: Intermedio-Avanzado IDE: Netbeans 6.9 o Sup. Tiempo: 30 minutos En este tutorial crearemos una aplicación que nos...
Los comentarios estan cerrados
Si trabajas con redes sociales (RRSS) a continuación te muestro tres herramintas gratuitas que te ayudaran a la hora de...
Por lo general se usan transacciones a nivel base de datos y posteriormente se llaman estos a través de procedimientos a...
En este post, aprenderemos como conectar Visual Basic 6 con SQL Server, abrir una tabla, leer su contenido y mostrar est...
Lo que veremos en este post es la configuración del driver para PHP de SQL Server que ha creado Microsoft el cual permit...
Hola.
Me gustó mucho el ejemplo.
Sólo una observación. El archivo no se puede descargar (me sale que tiene malware).
Por lo que te podría pedir si puedes subir este código a Github? así podrías tener forks y pull requests para mejorar el código (si aplica)
Buena onda de todas formas
saludos
No tiene malware 🙂 debe ser tu antivirus. Tomare en cuenta tu sugerencia 🙂 y en cuanto pueda lo subire a GitHub
Consulta permite que ingresen NIT con letras ?? ej: 654616-1B, ya que son ci/nit validos, que el segip otorga a los ci duplicados.