GUJ
Notícias, artigos e o maior fórum brasileiro sobre Java
home
fórum
notícias
tópicos recentes
empregos
artigos
Bem-vindo ao GUJ.
Crie seu login
, ou digite-o para logar no site.
Usuário:
Senha:
Criptografia DES
Índice dos Fóruns
»
Java Avançado
◄
1
2
Ir
Ir para a página...
Autor
Mensagem
09/12/2008 12:01:00
Assunto:
Re:Criptografia DES
tap_pedroso
JavaBaby
Membro desde: 11/06/2008 12:38:26
Mensagens: 90
Offline
Aos colegas q necessitam trabalhar com Criptografia DES...segue um exemplo funcional 100% DES com chave hexadecimal:
package testedesjce; import java.security.spec.*; import javax.crypto.*; import javax.crypto.spec.*; public class Main { public static byte[] hexToBytes(String str) { if (str==null) { return null; } else if (str.length() < 2) { return null; } else { int len = str.length() / 2; byte[] buffer = new byte[len]; for (int i=0; i<len; i++) { buffer[i] = (byte) Integer.parseInt( str.substring(i*2,i*2+2),16); } return buffer; } } public static String bytesToHex(byte[] data) { if (data==null) { return null; } else { int len = data.length; String str = ""; for (int i=0; i<len; i++) { if ((data[i]&0xFF)<16) str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF); else str = str + java.lang.Integer.toHexString(data[i]&0xFF); } return str.toUpperCase(); } } public static void main(String[] a) { if (a.length<1) { System.out.println("Usage:"); System.out.println("java JceSunDesTest 1/2"); return; } String test = a[0]; try { byte[] theKey = null; byte[] theMsg = null; byte[] theExp = null; if (test.equals("1")) { theKey = hexToBytes("5446615264454F49"); theMsg = hexToBytes("9626000715612011"); theExp = hexToBytes("85E813540F0AB405"); } else if (test.equals("2")) { theKey = hexToBytes("5446615264454F49"); // "8bytekey" theMsg = hexToBytes("9626000715582011"); // "message." theExp = hexToBytes("7CF45E129445D451"); } else { System.out.println("Usage:"); System.out.println("java JceSunDesTest 1/2"); return; } KeySpec ks = new DESKeySpec(theKey); SecretKeyFactory kf = SecretKeyFactory.getInstance("DES"); SecretKey ky = kf.generateSecret(ks); Cipher cf = Cipher.getInstance("DES/ECB/NoPadding"); cf.init(Cipher.ENCRYPT_MODE,ky); byte[] theCph = cf.doFinal(theMsg); System.out.println("Key : "+bytesToHex(theKey)); System.out.println("Message : "+bytesToHex(theMsg)); System.out.println("Cipher : "+bytesToHex(theCph)); System.out.println("Expected: "+bytesToHex(theExp)); } catch (Exception e) { e.printStackTrace(); return; } } }
26/05/2009 17:40:02
Assunto:
Re:Criptografia DES
gnewuch
Smalltalk
Membro desde: 23/08/2007 23:03:11
Mensagens: 1
Offline
Na minha classe o problema é essa linha:
sun.misc.BASE64Encoder
ta dando erro, não sei o que fazer, já atualizei o Java-JRE
Índice dos Fóruns
»
Java Avançado
◄
1
2
Ir
Ir para a página...
Ir para:
Selecione um Fórum
Notícias
Assuntos gerais (Off-topic)
MundoJ - Artigos, Notícias e Debates
Artigos e Tutoriais
Java Básico
Java Avançado
Ferramentas, Frameworks e Utilitários
Desenvolvimento Web
Interface Gráfica
Google Android e Java Micro Edition (ME)
Certificação Java
Persistência: Hibernate, JPA, JDBC e outros
Java Enterprise Edition (Java EE)
Frameworks e Bibliotecas brasileiros
RIA - Flex, JavaFX e outros
Arquitetura de Sistemas
Metodologias de Desenvolvimento e Testes de Software
JavaScript
Ruby & Ruby on Rails
Outras Linguagens
Powered by
JForum 2.1.8
©
JForum Team