Tag: rsa

如何使用RSA密钥签署通用文本并使用Java中的Base64进行编码?

我在bash中有以下代码: signed_request = $(printf “PLAIN TEXT REQUEST” | openssl rsautl -sign -inkey “keyfile.pem” | openssl enc -base64 | _chomp ) 基本上,此代码采用纯文本,使用私钥对其进行签名并使用Base64进行编码 我怎样才能在Java中使用完全相同的function执行代码?

为什么RSA加密可以使用C#和Java返回不同的结果?

我用的是: c#:RSACryptoServiceProvider JAVA:KeyFactory.getInstance(“RSA”)+ Cipher 我将公钥(指数+模数)作为字节数组从java发送到c#。 没关系,有相同的字节。 但是当我尝试使用Java和c#中的一个密钥加密某些数据时 – 会有不同的结果。 Java密钥生成: KeyPairGenerator keyGen = KeyPairGenerator.getInstance(“RSA”); keyGen.initialize( Config.CRYPTO_KEY_NUM_BITS ); m_KeyPair = keyGen.genKeyPair(); m_PublicKey = KeyFactory.getInstance(“RSA”).generatePublic( newX509EncodedKeySpec(m_KeyPair.getPublic().getEncoded())); byte[] exponent = m_PublicKey.getPublicExponent().toByteArray(); byte[] modulus = m_PublicKey.getModulus().toByteArray(); // then sending… C#Key Recieve: // Recieved… m_ExternKey = new RSAParameters(); m_ExternKey.Exponent = exponent; m_ExternKey.Modulus = modulus; m_RsaExtern = new RSACryptoServiceProvider(); m_RsaExtern.ImportParameters(m_ExternKey); byte[] […]

使用phpseclibvalidation在Java(Android)中生成的SHA1withRSA签名

这就是我想要做的: 在Java / Android中生成512位RSA密钥对 为Java中的某些消息生成SHA1withRSA签名 向PHP发送消息,签名和公钥(用于测试这将同时完成) 使用phpseclibvalidationPHP中的消息 到目前为止我得到了什么: 在Java方面: String msg = “Test message”; // generate keypair KeyPairGenerator keyGen = KeyPairGenerator.getInstance(“RSA”); keyGen.initialize(512); KeyPair keyPair = keyGen.generateKeyPair(); // generate signature Signature signature = Signature.getInstance(“SHA1withRSA”); signature.initSign(keyPair.getPrivate(), SecureRandom.getInstance(“SHA1PRNG”)); signature.update(msg.getBytes()); byte[] sigBytes = signature.sign(); // send message, signature and public key to php script List nameValuePairs = new ArrayList(uploadNum […]

如何从Java中的私钥对象获取RSA公钥

如何从RSA密码系统中的私钥Object java.security.PrivateKey中获取相关的公钥对象java.security.PublicKey 。

如何在python中制作PKCS8 RSA签名

我有pkcs8_rsa_private_key文件,它由openssl从rsa_private_key.pem文件生成。 我需要通过python中的私钥进行签名,使用下面的java代码创建相同的签名。 public static final String SIGN_ALGORITHMS = “SHA1WithRSA”; public static String sign(String content, String privateKey) { String charset = “utf-8”; try { PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec( Base64.decode(privateKey)); KeyFactory keyf = KeyFactory.getInstance(“RSA”); PrivateKey priKey = keyf.generatePrivate(priPKCS8); java.security.Signature signature = java.security.Signature .getInstance(SIGN_ALGORITHMS); signature.initSign(priKey); signature.update(content.getBytes(charset)); byte[] signed = signature.sign(); return Base64.encode(signed); } catch (Exception e) { […]

从String创建RSA公钥

我使用1024 RSA生成了此测试公钥,然后在另一个编码平台中将其编码为DER和Base64。 我将密钥复制到Android / Eclipse中的字符串中,我试图使用KeyFactory将其转换为公钥。 无论我尝试什么,它都会不断给我一个InvalidKeySpecException。 任何建议都将不胜感激。 private void prepKeys() { String AppKeyPub = “MIGHAoGBAOX+TFdFVIKYyCVxWlnbGYbmgkkmHmEv2qStZzAFt6NVqKPLK989Ow0RcqcDTZaZBfO5” + “5JSVHNIKoqULELruACfqtGoATfgwBp4Owfww8M891gKNSlI/M0yzDQHns5CKwPE01jD6qGZ8/2IZ” + “OjLJNH6qC9At8iMCbPe9GeXIPFWRAgER”; // create the key factory try { KeyFactory kFactory = KeyFactory.getInstance(“RSA”); // decode base64 of your key byte yourKey[] = Base64.decode(AppKeyPub,0); // generate the public key X509EncodedKeySpec spec = new X509EncodedKeySpec(yourKey); PublicKey publicKey = (PublicKey) kFactory.generatePublic(spec); […]

RSA BadPaddingException:数据必须以零开头

我尝试在Java程序中实现RSA算法。 我面临“BadPaddingException:数据必须从零开始”。 以下是用于加密和解密数据的方法: public byte[] encrypt(byte[] input) throws Exception { Cipher cipher = Cipher.getInstance(“RSA/ECB/PKCS1Padding”);// cipher.init(Cipher.ENCRYPT_MODE, this.publicKey); return cipher.doFinal(input); } public byte[] decrypt(byte[] input) throws Exception { Cipher cipher = Cipher.getInstance(“RSA/ECB/PKCS1Padding”);/// cipher.init(Cipher.DECRYPT_MODE, this.privateKey); return cipher.doFinal(input); } 以这种方式从文件中读取privateKey和publicKey属性: public PrivateKey readPrivKeyFromFile(String keyFileName) throws IOException { PrivateKey key = null; try { FileInputStream fin = new FileInputStream(keyFileName); ObjectInputStream […]

使用RSA进行模乘会导致Java Card出错

你好我正在研究一个关于Java Card的项目,它暗示了很多模乘法。 我设法使用RSA密码系统在这个平台上实现模乘,但它似乎适用于某些数字。 public byte[] modMultiply(byte[] x, short xOffset, short xLength, byte[] y, short yOffset, short yLength, short tempOutoffset) { //copy x value to temporary rambuffer Util.arrayCopy(x, xOffset, tempBuffer, tempOutoffset, xLength); // copy the y value to match th size of rsa_object Util.arrayFillNonAtomic(eempromTempBuffer, (short)0, (byte) (Configuration.LENGTH_RSAOBJECT_MODULUS-1),(byte)0x00); Util.arrayCopy(y,yOffset,eempromTempBuffer,(short)(Configuration.LENGTH_RSAOBJECT_MODULUS – yLength),yLength); // x+y if (JBigInteger.add(x,xOffset,xLength, eempromTempBuffer, (short)0,Configuration.LENGTH_MODULUS)) […]

在javascript中使用RSA加密一个小字符串,然后在服务器上的java中解密

我想使用带有公钥的RSA在javascript中加密一个小字符串,然后使用私钥在java服务器端代码中解密该字符串。 我在javascript中使用此代码: http : //www-cs-students.stanford.edu/~tjw/jsbn/示例: http : //www-cs-students.stanford.edu/~tjw/jsbn/rsa2 html的 这个代码在java方面: 加密javascript中的字符串和java中的解密 这两个代码都可以独立工作,但它们彼此不了解。 今天需要解决这个问题,或者我愿意接受任何其他以这种方式工作的非对称算法。

将SubjectPublicKeyInfo格式的公钥转换为RSAPublicKey格式java

PublicKey.getEncoded()返回一个包含SubjectPublicKeyInfo(x.509)格式的公钥的字节数组,如何将其转换为RSA公钥编码?