public bool GenerarCsr(ref authTokenType token, string privateKeyalias, string publicKeyAlias, string subject, string fileName, string challenge) { LunaXml.xmCryptoService client = new xmCryptoService(); try { byte[] modulus = new byte[] {}; byte [] exponent = new byte[] {}; this.Extraer(ref token, publicKeyAlias, ref modulus, ref exponent); RsaKeyParameters param = new RsaKeyParameters(false, new BigInteger(modulus), new BigInteger(exponent)); DerSet derset = null; if (challenge != null) { ChallengePassword chpass = new ChallengePassword(challenge); derset = new DerSet(chpass); //IList oid = new ArrayList(); //IList values = new ArrayList(); //oid.Add(PkcsObjectIdentifiers.Pkcs9AtChallengePassword); //var pass = new DerPrintableString(challenge); ////Asn1OctetString oct = pass.ToAsn1Object(); //new DerOctetString(pass);//Encoding.ASCII.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes("AABBccc22")))); //X509Extension ext = new X509Extension(false,new DerOctetString(pass.GetEncoded())); //values.Add(pass); //X509Extensions extensions = new X509Extensions(oid, values); //derset = new DerSet(extensions.ToAsn1Object()); } else { derset = new DerSet(); } //string sub = //"2.5.4.45=SAT970701NN3 / GATF730321GG5, SERIALNUMBER= / GATF730321HJCRRR01, O=SERVICIO DE ADMINISTRACION TRIBUTARIA, OU=PACNLC091211KC657202"; //+ ", 1.2.840.113549.1.9.7= NtLink2012" X509Name sub = new X509Name(subject, new ConverterSidetec()); Pkcs10CertificationRequestDelaySigned ds = new Pkcs10CertificationRequestDelaySigned("SHA1WITHRSA", sub, param, derset); string pafirmar = Convert.ToBase64String(ds.GetDataToSign()); string firmados = Firmar(ref token, pafirmar, privateKeyalias, SignatureModeType.SHA1withRSA); byte[] bytes = Convert.FromBase64String(firmados); ds.SignRequest(bytes); File.WriteAllBytes(fileName, ds.GetDerEncoded()); return(true); } catch (Exception ex) { Log.Error(ex); return(false); } finally { client.Dispose(); } }
public bool GenerarLLavesSoftware(string subject, string challenge, string fileName) { try { RsaKeyPairGenerator r = new RsaKeyPairGenerator(); var param = new RsaKeyGenerationParameters(new BigInteger("10001", 16), new SecureRandom(), 1024, 80); r.Init(param); AsymmetricCipherKeyPair k = r.GenerateKeyPair(); var privada = PrivateKeyInfoFactory.CreatePrivateKeyInfo(k.Private); SubjectPublicKeyInfo pubInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(k.Public); string priv = Convert.ToBase64String(privada.GetDerEncoded()); string pub = Convert.ToBase64String(pubInfo.GetDerEncoded()); File.WriteAllText("Privada.pem", priv); File.WriteAllText("Publica.pem", pub); RsaPrivateCrtKeyParameters privateKey = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(priv)); RsaKeyParameters publicKey = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(pub)); DerSet derset = null; if (challenge != null) { ChallengePassword chpass = new ChallengePassword(challenge); derset = new DerSet(chpass); } else { derset = new DerSet(); } X509Name sub = new X509Name(subject, new ConverterSidetec()); Pkcs10CertificationRequest ds = new Pkcs10CertificationRequest("SHA1WITHRSA", sub, publicKey, derset, privateKey); File.WriteAllBytes(fileName, ds.GetDerEncoded()); return(true); } catch (Exception ee) { Log.Error(ee.Message); return(false); } }