Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.29 KB | None | 0 0
  1. import java.security.MessageDigest;
  2. import java.security.NoSuchAlgorithmException;
  3.  
  4.  
  5. public class CriptografiaLogix {
  6. private String usuario;
  7. private String senha;
  8.  
  9. public CriptografiaLogix() {
  10. }
  11.  
  12. public CriptografiaLogix(String usuario, String senha) {
  13. this.criptografaLogin(usuario, senha);
  14. }
  15.  
  16. public String criptografaLogin(String codUsuario, String senha) {
  17.  
  18. String senhaResult;
  19. String arrumaResult = new String("");
  20.  
  21. StringBuffer codUsuarioAsciiAux;
  22. StringBuffer senhaAsciiAux;
  23. StringBuffer senhaResultAux;
  24. StringBuffer valorAscii;
  25. StringBuffer shortBuffer;
  26.  
  27. Short codUsuarioShort;
  28. Short senhaShort;
  29. Short shortAux;
  30.  
  31. short somaSenha = 0;
  32.  
  33. codUsuarioAsciiAux = new StringBuffer("000000000000000000000000");
  34. senhaAsciiAux = new StringBuffer("000000000000000000000000");
  35. valorAscii = new StringBuffer("000000000000000000000000");
  36.  
  37. int ind2 = 1;
  38. for (int ind1 = codUsuario.length(); ind1 >= 1; ind1--) {
  39. codUsuarioAsciiAux.replace(
  40. ind2 - 1,
  41. ind2 + 1,
  42. obtemValorAscii(codUsuario.substring(ind1 - 1)));
  43. ind2 = ind2 + 3;
  44.  
  45. }
  46.  
  47. codUsuarioAsciiAux.setLength(24);
  48. // Truncando p/ os 24 caracteres da criptografia
  49. this.usuario = (String) codUsuarioAsciiAux.toString();
  50. // Obtendo usuario criptografado
  51.  
  52. ind2 = 1;
  53. for (int ind1 = 0; ind1 < senha.length(); ind1++) {
  54. senhaAsciiAux.replace(
  55. ind2 - 1,
  56. ind2 + 1,
  57. obtemValorAscii(senha.substring(ind1)));
  58. ind2 = ind2 + 3;
  59. }
  60.  
  61. senhaAsciiAux.setLength(24);
  62. // Truncando p/ os 24 caracteres da criptografia
  63.  
  64. for (int ind1 = 0; ind1 < 22; ind1 += 3) {
  65. codUsuarioShort =
  66. new Short(
  67. codUsuarioAsciiAux.substring(ind1, ind1 + 3).toString());
  68. senhaShort =
  69. new Short(senhaAsciiAux.substring(ind1, ind1 + 3).toString());
  70. somaSenha =
  71. (short) (codUsuarioShort.shortValue()
  72. + senhaShort.shortValue());
  73. shortAux = new Short(somaSenha);
  74. arrumaResult = Short.toString(shortAux.shortValue());
  75. if ((arrumaResult != null) && (arrumaResult.length() == 2)) {
  76. arrumaResult = "0" + arrumaResult;
  77. }
  78. valorAscii.replace(ind1, ind1 + 2, arrumaResult);
  79. // System.out.println("1 : "+ shortAux.shortValue());//teste2
  80. // System.out.println("2 : " + valorAscii.toString()); //teste3
  81. }
  82.  
  83. valorAscii.setLength(24);
  84. //Truncando p/ os 24 caracteres da criptografia do Logix
  85.  
  86. senhaResult = (String) valorAscii.toString();
  87. this.senha = senhaResult;
  88. // Obtendo senha criptografada
  89. return senhaResult;
  90. }
  91.  
  92. public String decriptografarSenha(String usuario, String senhaCripto) throws java.lang.NumberFormatException {
  93. String senhaResult;
  94. String arrumaResult = new String("");
  95.  
  96. StringBuffer codUsuarioAsciiAux;
  97. String usuarioAscii;
  98. StringBuffer senhaAsciiAux;
  99. StringBuffer senhaResultAux;
  100. StringBuffer valorAscii;
  101.  
  102. Short codUsuarioShort;
  103. Short shortAux;
  104. Short senhaCriptoShort;
  105.  
  106. short diminuiSenha = 0;
  107.  
  108. codUsuarioAsciiAux = new StringBuffer("000000000000000000000000");
  109. senhaAsciiAux = new StringBuffer("000000000000000000000000");
  110. senhaResultAux = new StringBuffer("000000000000000000000000");
  111. valorAscii = new StringBuffer(senhaCripto);
  112.  
  113. int ind2 = 1;
  114. for (int ind1 = usuario.length(); ind1 >= 1; ind1--) {
  115. codUsuarioAsciiAux.replace(
  116. ind2 - 1,
  117. ind2 + 1,
  118. obtemValorAscii(usuario.substring(ind1 - 1)));
  119. ind2 = ind2 + 3;
  120.  
  121. }
  122.  
  123. codUsuarioAsciiAux.setLength(24);
  124. // Truncando p/ os 24 caracteres da criptografia
  125. usuarioAscii = (String) codUsuarioAsciiAux.toString();
  126. // Obtendo usuario criptografado
  127.  
  128. for (int ind1 = 0; ind1 < 22; ind1 += 3) {
  129. codUsuarioShort = new Short(codUsuarioAsciiAux.substring(ind1, ind1 + 3).toString());
  130. senhaCriptoShort = new Short(valorAscii.substring(ind1,ind1+3).toString());
  131. diminuiSenha = (short) (senhaCriptoShort.shortValue() - codUsuarioShort.shortValue());
  132. shortAux = new Short(diminuiSenha);
  133. arrumaResult = Short.toString(shortAux.shortValue());
  134. if ((arrumaResult != null) && (arrumaResult.length() == 2)) {
  135. arrumaResult = "0" + arrumaResult;
  136. }
  137. senhaResultAux.replace(ind1, ind1 + 2, arrumaResult);
  138. // System.out.println("1 : "+ shortAux.shortValue());//teste2
  139. // System.out.println("2 : " + valorAscii.toString()); //teste3
  140. }
  141.  
  142. senhaResultAux.setLength(24);
  143. //Truncando p/ os 24 caracteres da criptografia
  144. senhaResult="";
  145. try {
  146.  
  147. for(int ind1=0;ind1<22;ind1 +=3){
  148. String aux = String.valueOf((char) new Short(senhaResultAux.substring(ind1, ind1 + 3).toString()).intValue());
  149. if((Character.isLetterOrDigit(aux.charAt(0)))){
  150. senhaResult += aux;
  151. }
  152. }
  153. } catch(Exception ex) {
  154. return "000000000";
  155. }
  156.  
  157. return senhaResult;
  158. }
  159.  
  160. public String obtemValorAscii(String caracterString) {
  161. Character caracter;
  162. Character caracter2;
  163. Character caracter3;
  164. Character caracter4;
  165.  
  166. String caracterAux;
  167. String resultCharacter;
  168.  
  169. StringBuffer valorAscii;
  170.  
  171. int compara = 0;
  172.  
  173. short posIni = 0;
  174. short posFim = 0;
  175. short comparador = 0;
  176.  
  177. byte valorAsciiAux;
  178.  
  179. posIni = (short) 32;
  180. posFim = (short) 126;
  181. caracter = new Character(caracterString.charAt(0));
  182. caracter2 = new Character('0');
  183. caracter3 = new Character('A');
  184. caracter4 = new Character('a');
  185.  
  186. valorAscii = new StringBuffer();
  187.  
  188. compara = caracter.compareTo(caracter2);
  189. // Retorna 0 (zero) se for igual numericamente
  190. // Retorna um numero menor que zero se for menor numericamente
  191. // Retorna um numero maior que zero se for maior numericamente
  192.  
  193. if (compara == 0 || compara > 0) {
  194. posIni = (short) 48;
  195. compara = caracter.compareTo(caracter3);
  196. if (compara == 0 || compara > 0) {
  197. posIni = (short) 65;
  198. compara = caracter.compareTo(caracter4);
  199. if (compara == 0 || compara > 0) {
  200. posIni = (short) 97;
  201. } else {
  202. posFim = (short) 90;
  203. }
  204. } else {
  205. posFim = (short) 90;
  206. }
  207. } else {
  208. posFim = (short) 47;
  209. }
  210.  
  211. for (int i = posIni; i <= posFim; i++) {
  212. caracterAux = caracter.toString();
  213. valorAsciiAux = (byte) caracterAux.charAt(0);
  214. comparador = valorAsciiAux;
  215. if (comparador == i) {
  216. if (comparador < 100) {
  217. valorAscii.append("0").append(i);
  218. // apenas para fazer igual ao 4GL USING &&&
  219. } else {
  220. valorAscii.append(i);
  221. }
  222. }
  223. }
  224. resultCharacter = new String(valorAscii);
  225. return resultCharacter;
  226. }
  227.  
  228. public String descriptografaUsuario(String usuarioCriptografado) {
  229.  
  230. Byte byteAux = null;
  231. StringBuffer retornoAux = null;
  232. String stringAux;
  233. String retorno = "";
  234.  
  235. char[] charAux = new char[1];
  236.  
  237. retornoAux = new StringBuffer();
  238.  
  239. for (int i = 0; i < usuarioCriptografado.length(); i = i + 3) {
  240. stringAux = usuarioCriptografado.substring(i, i + 3);
  241. byteAux = new Byte(stringAux);
  242. charAux[0] = (char) byteAux.byteValue();
  243. stringAux = new String(charAux);
  244. retornoAux.append(stringAux);
  245. }
  246.  
  247. retornoAux.<b style="color:black;background-color:#ff9999">reverse</b>();
  248. retorno = new String(retornoAux.toString());
  249.  
  250. return retorno;
  251. }
  252.  
  253. public String getUsuarioCriptografado() {
  254. return this.usuario.toString();
  255. }
  256.  
  257. public String getSenhaCriptografada() {
  258. return this.senha.toString();
  259. }
  260.  
  261. public static String criptografaSenha(String senha) {
  262. MessageDigest md = null;
  263. String password = new String(senha);
  264. try {
  265. md = MessageDigest.getInstance("SHA-1");
  266. } catch (NoSuchAlgorithmException _exNSAE) {
  267. System.out.println(
  268. "Algoritmo de criptografia n�o encontrado. \n Execu��o abortada.");
  269. System.exit(0);
  270. }
  271. md.reset();
  272. md.update(password.getBytes());
  273. byte[] digest1 = md.digest();
  274.  
  275. StringBuffer encryptedPassword = new StringBuffer();
  276. for (int i = 0; i < digest1.length; i++) {
  277. encryptedPassword.append(Integer.toHexString(0xFF & digest1[i]));
  278. }
  279. return encryptedPassword.toString();
  280. }
  281.  
  282. static void escreve(String str) {
  283. System.out.println(str);
  284. }
  285.  
  286. public static void main(String[] args) {
  287. String user = "Laranjinha";
  288. String pass = "uHjWdg5";
  289. CriptografiaLogix cripto = new CriptografiaLogix(user, pass);
  290. escreve("Testes de senha");
  291. escreve("Usuario : " + user);
  292. escreve("Senha Plana: " + pass);
  293. escreve("Senha Banco: " + cripto.getSenhaCriptografada());
  294. escreve("Senha : " + cripto.decriptografarSenha(user,cripto.getSenhaCriptografada()));
  295. escreve("Usuário : " + cripto.descriptografaUsuario(cripto.getUsuarioCriptografado()));
  296. escreve("#####################################");
  297. escreve("");
  298. escreve("FIM");
  299. }
  300.  
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement