Tag: rsa

InvalidKeySpecException:algid解析错误,而不是序列

我正在编写一个从控制台输入的程序 – 一个zip文件的名称,一个zip文件的名称包含从第一个zip生成的(de / en)加密文件和一个包含公钥的文件。 解密时我得到exception: Exception in thread “main” java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217) at java.security.KeyFactory.generatePrivate(KeyFactory.java:372) at com.Main.makePrivateKey(Main.java:148) at com.Main.decrypt(Main.java:40) at com.Main.main(Main.java:182) Caused by: java.security.InvalidKeyException: IOException : algid parse error, not a sequence at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:351) at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:356) at sun.security.rsa.RSAPrivateCrtKeyImpl.(RSAPrivateCrtKeyImpl.java:91) at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:75) at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:316) at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:213) … 4 […]

使用java实现RSA

我在java中实现了RSA我遇到了一个代码,下面给出的代码在解密明文后以数字forms显示明文,但我想用简单的英文输入它。 我不想使用java api。 TestRsa.Java import java.io.IOException; import java.math.BigInteger; import java.util.Random; public class TestRsa { private BigInteger p, q; private BigInteger n; private BigInteger PhiN; private BigInteger e, d; public TestRsa() { initialize(); } public void initialize() { int SIZE = 512; /* Step 1: Select two large prime numbers. Say p and q. */ p […]

使用Java的RSA加密/解密

我正在做一个简单的程序来使用Java中的RSA算法进行加密/解密。 我创建一个密码对象如下: //Create a Cipher object Cipher rsaCipher = Cipher.getInstance(“RSA/ECB/NoPadding”); 我通过调用加密函数来进行加密: String cipher=encrypt(textByte, pair, rsaCipher); System.out.println(“The Encryption using RSA Algorithm : “+cipher); 并解密为: //Decryption String plain=decrypt(Base64.decodeBase64(cipher),pair, rsaCipher); System.out.println(“The Decryption using RSA Algorithm : “+plain); 当我显示输出时,解密输出在原始文本之前返回一个长空格: 但是,当我编辑用于创建Cipher对象的代码时://创建一个密码对象Cipher rsaCipher = Cipher.getInstance(“RSA”); 即,删除操作模式和填充参数,问题得到解决,输出变为: 问题出在哪儿。 在第一种情况下(当空间出现时),我指定了NoPadding? 为什么空格出现在解密的消息中? 即使我使用填充,我预计这不应该发生。 编辑:这是加密和解密方法: public static String encrypt(byte[] textBytes, KeyPair pair, Cipher rsaCipher) throws […]

Android中的RSA加密

我正在编写一个在Android中使用RSA的程序。 我有以下问题:我收到RSA密钥: KeyPair kp = kpg.genKeyPair(); publicKey = kp.getPublic(); privateKey = kp.getPrivate(); 使用加密函数加密测试字符串: String test =”test”; byte[] testbytes = test.getBytes(); Cipher cipher = Cipher.getInstance(“RSA”); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] cipherData = cipher.doFinal(testbytes); String s = new String(cipherData); Log.d(“testbytes after encryption”,s); 在解密function中,我正在解密数据以获得原始字符串 Cipher cipher2 = Cipher.getInstance(“RSA”); cipher2.init(Cipher.DECRYPT_MODE, privateKey); byte[] plainData = cipher.doFinal(cipherData); String p = new String(plainData); Log.d(“decrypted […]

使用公钥进行RSA解密

我的Android项目中有一些解密问题。 我收到一个用私钥签名的字符串,我必须用公钥validation(解密)它。 我希望得到与使用PHP函数完全相同的结果 – openssl_public_decrypt( http://php.net/manual/pl/function.openssl-public-decrypt.php ) 我必须在我的Java项目中这样做,所以我可以使用Java库(例如BouncyCastle,或其他什么,任何建议?) 任何想法如何解决这个问题? 好的,这是我的代码。 我正在收到这样的公钥 PEMReader reader = new PEMReader(new InputStreamReader(ctx .getAssets().open(“pubkey.pem”))); Object obj; while ((obj = reader.readObject()) != null) { if (obj instanceof RSAPublicKey) { pubKey = (RSAPublicKey) obj; return pubKey; } } 我总是毫无问题地得到公钥。 Cipher c = Cipher.getInstance(“RSA/NONE/NoPadding”, “SC”); c.init(Cipher.DECRYPT_MODE, pubKey); byte[] result = c.doFinal(data_to_decrypt.getBytes()); 结果(将字节转换为字符串后)我得到022c06571c6a263b389fcd93159cb311abb880bddf51b7c916dd1ae… 其中php函数返回sd8dsa348acvcx87|00454|OK|15000|CDE ,这是一个正确的输出。

Java中的RSA加密,PHP中的解密

我正在尝试使用RSA公钥和Android应用程序加密AES密钥,然后使用带有phpseclib的PHP在服务器上解密AES密钥。 我已经测试过确保RSA加密/解密在两个平台上都可以通过加密静态文本并在之后解密来检查我是否仍然获得原始文本。 根据测试,RSA代码在每个平台上单独工作,但平台之间似乎存在差异。 在Java中,我使用的是Bouncy Castle库并具有以下代码: RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec( new BigInteger(“00c897f9e401819e223ffbecc6f715a8d84dce9022762e0e2d54fa434787fcaf230d28bd0c3b6b39b5211f74ffc4871c421362ccfc07ae98b88fa9728f1e26b8210ebbf4981e45867fe810938294d0095d341b646b86dcbd4c246676c203cb1584d01eef0635299714d94fa12933ecd35e6c412573156d9e6e549b7804eb6e165660507d8748bcc8c60da10099bacb94d3f7b50b1883ee108489e0dd97ed7d28e564edd4ee5d6b4225f5c23cdaaf495c3fa08c3b82e1674946e4fa1e79b2493204d6953c261105ba5d0f8dcf3fcd39a51fbc18a5f58ffff169b1bed7ceeded2ae0e8e8e2238e8b77b324d1a482593b1a642e688c860e90d5a3de8515caf384133b”, 16), new BigInteger(“11”, 16)); keyFactory = KeyFactory.getInstance(“RSA”, “BC”); //RSAPublicKeySpec rsaKeySpec = new RSAPublicKeySpec(rsaKey.MODULUS, new BigInteger(“11”, 16)); RSAPublicKey pubKey = (RSAPublicKey)keyFactory.generatePublic(pubKeySpec); //Set up the cipher to RSA encryption Cipher cipher = Cipher.getInstance(“RSA/None/PKCS1Padding”, “BC”); cipher.init(Cipher.ENCRYPT_MODE, pubKey); // make sure the Aes Key is less than […]

如何使“MessageDigest SHA-1和Signature NONEwithRSA”等同于“Signature SHA1withRSA”

我有兴趣将带有RSA签名的SHA-1哈希应用于某些数据,但我需要分两步完成 – 首先应用哈希然后对数据进行签名。 Signature.sign()函数似乎创建了一个最终签名的更复杂(ASN.1?)的数据结构(请参阅此问题 )。 如何在不使用任何外部库(如BouncyCastle)的情况下制作两个等效文件? 使用签名应用哈希并签名: PrivateKey privatekey = (PrivateKey) keyStore.getKey(alias, null); … sig = Signature.getInstance(“SHA1withRSA”, “SunMSCAPI”); sig.initSign(privatekey); sig.update(data_to_sign); byte[] bSignedData_CAPISHA1_CAPIRSA = sig.sign(); 通过MessageDigest应用哈希,然后使用签名进行签名: PrivateKey privatekey = (PrivateKey) keyStore.getKey(alias, null); … MessageDigest sha1 = MessageDigest.getInstance(“SHA-1”); byte[] data_to_sign = sha1.digest(bdataToSign); Signature sig = Signature.getInstance(“NONEwithRSA”, “SunMSCAPI”); sig.initSign(privatekey); sig.update(data_to_sign); byte[] bSignedData_JAVASHA1_CAPIRSA = sig.sign(); … 我正在寻找以下等价物: bSignedData_JAVASHA1_CAPIRSA == […]

无法解析证书:java.io.IOException:空输入X509Certificate

解析签名时,我收到下面给出的错误。 任何人都知道错误显示的原因? 注意: 使用相同的证书,我签署了自己的XML并validation哪个工作正常。 这意味着证书没有问题。 客户提供的签名文档无法validation。 错误: Exception in thread “main” javax.xml.crypto.MarshalException: Cannot create X509Certificate at org.jcp.xml.dsig.internal.dom.DOMX509Data.unmarshalX509Certificate(DOMX509Data.java:225) at org.jcp.xml.dsig.internal.dom.DOMX509Data.(DOMX509Data.java:116) at org.jcp.xml.dsig.internal.dom.DOMKeyInfo.(DOMKeyInfo.java:116) at org.jcp.xml.dsig.internal.dom.DOMXMLSignature.(DOMXMLSignature.java:150) at org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshal(DOMXMLSignatureFactory.java:173) at org.jcp.xml.dsig.internal.dom.DOMXMLSignatureFactory.unmarshalXMLSignature(DOMXMLSignatureFactory.java:137) at com.signing.ValidateSignedXML.main(ValidateSignedXML.java:126) Caused by: java.security.cert.CertificateException: Could not parse certificate: java.io.IOException: Empty input at sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:104) at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:339) at org.jcp.xml.dsig.internal.dom.DOMX509Data.unmarshalX509Certificate(DOMX509Data.java:223) … 6 more Caused by: java.io.IOException: Empty input at sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:101) […]

RSA密钥对使用充气城堡生成。 使代码可以从java程序运行

我使用的Java代码通过充气城堡库生成公钥和私钥。 我的问题是将它实现到我的Android设备可运行的代码中。 我的代码没有像我编写的那样显示RSA密钥,并且在我的大部分故障排除中,我仍然无法按照我的要求进行代码,尽管我没有错误。 我怀疑的是我将所有代码放入try / catch块的方式,但我不太确定。 编辑:代码较低 这是生成RSA公钥和私钥的Java类。 (有用) public class ClassMain { public static void main(String[]args) throws Exception { String ST = “Ebenezersawesome”; byte[] plainText = “ST”.getBytes(“UTF8”); // Generating RSA Key System.out.println(“\nStart generating RSA key”); KeyPairGenerator keyGen = KeyPairGenerator.getInstance(“RSA”); keyGen.initialize(4096); KeyPair key = keyGen.generateKeyPair(); System.out.println(“Finish generating RSA key”); // // Creates an RSA Cipher […]

HSM错误| 私钥必须是RSAPrivate(Crt)密钥的实例或具有PKCS#8

从HSM检索私钥时解密数据时收到错误。 我在java.security中添加了sunpkcs11提供程序。 因此,不要通过代码添加提供程序。 文本已成功加密。 但是,在解密加密文本时,我在下面的行中得到以下错误: cipher.init(Cipher.DECRYPT_MODE, privateKey); 我在这里失踪的是什么? 错误: Caused by: java.security.InvalidKeyException: Private key must be instance of RSAPrivate(Crt)Key or have PKCS#8 encoding at sun.security.pkcs11.P11RSAKeyFactory.implTranslatePrivateKey(P11RSAKeyFactory.java:101) [sunpkcs11.jar:1.7.0_85] at sun.security.pkcs11.P11KeyFactory.engineTranslateKey(P11KeyFactory.java:132) [sunpkcs11.jar:1.7.0_85] at sun.security.pkcs11.P11KeyFactory.convertKey(P11KeyFactory.java:65) [sunpkcs11.jar:1.7.0_85] at sun.security.pkcs11.P11RSACipher.implInit(P11RSACipher.java:199) [sunpkcs11.jar:1.7.0_85] at sun.security.pkcs11.P11RSACipher.engineInit(P11RSACipher.java:168) [sunpkcs11.jar:1.7.0_85] at javax.crypto.Cipher.init(Cipher.java:1068) [jce.jar:1.7.0_85] at javax.crypto.Cipher.init(Cipher.java:1012) [jce.jar:1.7.0_85]enter code here 以下是代码: import java.io.ByteArrayOutputStream; import java.security.KeyStore; import java.security.PrivateKey; import java.security.PublicKey; […]