Hola, estoy tratando de descifrar una firma teniendo los parámetros y llave publica, al momento de usar la función de Cipher con DECRYPT_MODE y con el proveedor de certificado ("SHA-1/DSA", "SUN") obtengo el siguiente error
Código:
java.security.NoSuchAlgorithmException: Invalid transformation format:SHA-1/DSA
Intento cambiar el proveedor Cipher cipher = Cipher.getInstance("SHA-1/DSA", "SUN"); y simpre obtengo ese error.
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 originalimport 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 {
public static void main
(String[] args
) { // TODO Auto-generated method stub
try {
keyGen.initialize(1024, random);
KeyPair keyPair
= keyGen.
generateKeyPair(); DSAParams dsaParams
= privateKey.
getParams(); System.
out.
println("BigInteger\np: "+p
+"\nq: "+q
+"\ng: "+g
+"\nx: "+x
+"\ny: "+y
);
PublicKey publicKey
= keyFactory.
generatePublic(publicKeySpec
); System.
out.
println("publicKey: "+publicKey
);
System.
out.
println("EncodeBase64\nP: "+Base64.
encode(p.
toByteArray())+ "\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();
System.
out.
println("firma-base64: "+Base64.
encode(firmaDigitalByte
)); // 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 originalimport 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 {
public static void main
(String[] args
){ // TODO Auto-generated method stub
y
= new BigInteger(Base64.
decode("AMadmTWomoI7vER+OyuWVlM+8kEvhD2WvK2O9Pk3PClROsrqTvL/Qdou/I4Iy+0PcSMG7mM3zlqADtwmKqjYYkC37pcQ7fQdiOUQkGV7apb9Kf2Vbo21JO0oPTPq8FqJpSlfvBtu2Kmc0xst0Ue55iDnWkSIVONzIqWcJPn6GAYR")); p
= new BigInteger(Base64.
decode("AP1/U4EddRIpUt9KnC7s5Of2EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdrmVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXzrith1yrv8iIDGZ3RSAHH")); q
= new BigInteger(Base64.
decode("AJdgUI8VIwvMspK5gqLrhAvwWBz1")); g
= new BigInteger(Base64.
decode("APfhoIXWmz3ey7yrXDa4V7l5lK+7+jrqgvlXTAs9B4JnUVlXjrrUWU/mcQcQgYC0SRZxI+hMKBYTt88JMozIpuE8FnqLVHyNKOCjrh4rs6Z1kW6jfwv6ITVi8ftiegEkO8yk8b6oUZCJqIPf4VrlnwaSi2ZegHtVJWQBTDv+z0kq")); String firma
= "MCwCFDT6jZHwqD/CRWcQd5Zf514C+Mj+AhQoMjmcr4SRYdaxk7GWVOtw/y0Fgg=="; String comparar
= "CARLOS|132.20|400|ABC"; System.
out.
println("\nSignature-verify: " + valida
(y, p, q, g, firma, comparar
) ); }
boolean firmaValida = false;
byte[] firmaByte;
System.
out.
println("firma: "+firma
+"\nfirma-base64decode: "+ Base64.
decode(firma
)); firmaByte = Base64.decode(firma);
System.
out.
println("firma-byte: " + firmaByte
); System.
out.
println("y: "+y
+"\np: "+p
+"\nq: "+q
+"\ng: "+g
); 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));
// }
keyFactory
= KeyFactory.
getInstance("DSA",
"SUN"); //KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
publicKey = keyFactory.generatePublic(publicKeySpec);
//privateKey = keyFactory.generatePrivate(privateKeySpec);
signature
= Signature.
getInstance("SHA1withDSA" ,
"SUN"); signature.initVerify(publicKey);
System.
out.
println(publicKey
); signature.update(comparar.getBytes("UTF-8"));
firmaValida = signature.verify( firmaByte );
System.
out.
println("\nDesencriptando firma"); Cipher cipher = Cipher.getInstance("SHA-1/DSA", "SUN");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
System.
out.
println("valor: "+new String(cipher.
doFinal(firmaByte
)));
// 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)