Código:
Intento cambiar el proveedor Cipher cipher = Cipher.getInstance("SHA-1/DSA", "SUN"); y simpre obtengo ese error.java.security.NoSuchAlgorithmException: Invalid transformation format:SHA-1/DSA
Lo que si me funciona es la validación de la firma, pero no puedo hacer la inversa de obtener la información que hay dentro de esa firma.
Este es el código para cifrar la información
Código Java:
Ver original
import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.PublicKey; import java.security.SecureRandom; import java.security.Signature; import java.security.SignatureException; import java.security.interfaces.DSAParams; import java.security.interfaces.DSAPrivateKey; import java.security.interfaces.DSAPublicKey; import java.security.spec.DSAPublicKeySpec; import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import org.apache.axis.encoding.Base64; public class Prb07 { // TODO Auto-generated method stub try { keyGen.initialize(1024, random); "\nQ: "+Base64.encode(q.toByteArray())+ "\nG: "+Base64.encode(g.toByteArray())+ "\nY: "+Base64.encode(y.toByteArray())+ "\nX: "+Base64.encode(x.toByteArray())); signature.initSign(privateKey); signature.update("CARLOS|132.20|400|ABC".getBytes()); //Valor que va a contener la firma byte[] firmaDigitalByte = signature.sign(); // TODO Auto-generated catch block e.printStackTrace(); // TODO Auto-generated catch block e.printStackTrace(); // TODO Auto-generated catch block e.printStackTrace(); // TODO Auto-generated catch block e.printStackTrace(); // TODO Auto-generated catch block e.printStackTrace(); } } }
Y este código para validar y descifrar la información
Código Java:
Ver original
import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.security.InvalidKeyException; import java.security.Key; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.PrivateKey; import java.security.Provider; import java.security.PublicKey; import java.security.Security; import java.security.Signature; import java.security.SignatureException; import java.security.spec.DSAPrivateKeySpec; import java.security.spec.DSAPublicKeySpec; import java.security.spec.InvalidKeySpecException; import java.security.spec.KeySpec; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import org.apache.axis.encoding.Base64; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Hex; public class Prb06 { // TODO Auto-generated method stub BigInteger x, y, p, q, g; y = new BigInteger(Base64.decode("AMadmTWomoI7vER+OyuWVlM+8kEvhD2WvK2O9Pk3PClROsrqTvL/Qdou/I4Iy+0PcSMG7mM3zlqADtwmKqjYYkC37pcQ7fQdiOUQkGV7apb9Kf2Vbo21JO0oPTPq8FqJpSlfvBtu2Kmc0xst0Ue55iDnWkSIVONzIqWcJPn6GAYR")); p = new BigInteger(Base64.decode("AP1/U4EddRIpUt9KnC7s5Of2EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdrmVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXzrith1yrv8iIDGZ3RSAHH")); g = new BigInteger(Base64.decode("APfhoIXWmz3ey7yrXDa4V7l5lK+7+jrqgvlXTAs9B4JnUVlXjrrUWU/mcQcQgYC0SRZxI+hMKBYTt88JMozIpuE8FnqLVHyNKOCjrh4rs6Z1kW6jfwv6ITVi8ftiegEkO8yk8b6oUZCJqIPf4VrlnwaSi2ZegHtVJWQBTDv+z0kq")); } boolean firmaValida = false; byte[] firmaByte; PublicKey publicKey; PrivateKey privateKey; Signature signature; KeyFactory keyFactory; firmaByte = Base64.decode(firma); try { // for (Provider provider: Security.getProviders()) { // System.out.println(provider.getName()); // for (String key2: provider.stringPropertyNames()) // System.out.println("\t" + key2 + "\t" + provider.getProperty(key2)); // } //KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g); publicKey = keyFactory.generatePublic(publicKeySpec); //privateKey = keyFactory.generatePrivate(privateKeySpec); signature.initVerify(publicKey); signature.update(comparar.getBytes("UTF-8")); firmaValida = signature.verify( firmaByte ); Cipher cipher = Cipher.getInstance("SHA-1/DSA", "SUN"); cipher.init(Cipher.DECRYPT_MODE, publicKey); // TODO Auto-generated catch block e.printStackTrace(); // TODO Auto-generated catch block e1.printStackTrace(); // TODO Auto-generated catch block e.printStackTrace(); // TODO Auto-generated catch block e.printStackTrace(); // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); // TODO Auto-generated catch block e.printStackTrace(); } return firmaValida; } }
En signature verify me devuelve true, pero con cipher decrypt mode me devuelve el error
Código Java:
Ver original
at javax.crypto.Cipher.tokenizeTransformation(Cipher.java:310) at javax.crypto.Cipher.getTransforms(Cipher.java:417) at javax.crypto.Cipher.getInstance(Cipher.java:629) at javax.crypto.Cipher.getInstance(Cipher.java:585) at Prb06.valida(Prb06.java:76) at Prb06.main(Prb06.java:40)