Tag: rsa

在java中将byte 转换为PrivateKey以进行数字签名

我需要先使用SHA-1摘要算法对String进行数字签名,然后应用RSA算法,使用PrivateKey对其进行签名。 我已经将PrivateKey存储在我的数据库中,作为base64中的数据类型char(250)。 我的问题是我不知道如何将其转换为PrivateKey以使用它进行登录: Cipher cipher = Cipher.getInstance(“RSA”); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] cipherText = cipher.doFinal(digest); Digest是我应用SHA-1摘要算法的字节数组: MessageDigest md = MessageDigest.getInstance(“SHA-1”); byte [] ba = cadena.getBytes(); byte [] digest = md.digest(ba); 这是我想到的解决方案,但如果有人有更好的解决方案我会很感激。

javax.crypto.BadPaddingException:未知块类型

我试图模拟非对称密钥系统。 我使用以下代码生成密钥对,加密,解密密码。 我有一个分布式环境,目前我保存在文件系统中生成的密钥。 我知道这不安全,但仅用于测试目的。 private static SecureRandom random = new SecureRandom(); static { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); } protected synchronized void generateKeys() throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException { KeyPairGenerator generator = KeyPairGenerator.getInstance(“RSA”, “BC”); generator.initialize(256, random); KeyPair pair = generator.generateKeyPair(); Key pubKey = pair.getPublic(); Key privKey = pair.getPrivate(); //store public key try { storeKey(pubKey, Constants.KEY_PATH.concat(Constants.SERVER_PREFIX.concat(“-publickey”))); } […]

如何从未格式化的String获取公共RSA密钥

我在属性文件中有一个未格式化的公钥,这意味着它只包含公钥hexa值: K_PUB_CCE = 如你所见,它的长度为538六进制。 我想要实现的是获取具有此值的java.security.PublicKey 。 但是当使用这种方法时: private PublicKey createPublicKey(String stringPublicKey) throws NoSuchAlgorithmException, InvalidKeySpecException { byte[] bytesKey= Hex.decodeHex(stringPublicKey.toCharArray()); X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(bytesKey); KeyFactory keyFactory = KeyFactory.getInstance(“RSA”); return keyFactory.generatePublic(encodedKeySpec); } 我得到以下exception: java.security.InvalidKeyException: IOException: algid parse error, not a sequence 我究竟做错了什么? 我应该使用其他任何关键规格类吗?

Java到Objective-C RSA的实现

我在Objective-C中实现RSA encryption和解密时遇到了麻烦,我用Java非常简单地编写了它,现在我尝试在objc翻译这个java代码。 这是我的java代码: public static byte[] encryptRSA(byte[] text, PublicKey key) throws Exception { byte[] cipherText = null; // get an RSA cipher object and print the provider Cipher cipher = Cipher.getInstance(“RSA”); // encrypt the plaintext using the public key cipher.init(Cipher.ENCRYPT_MODE, key); cipherText = cipher.doFinal(text); return cipherText; } public static byte[] decryptRSA(byte[] text, PrivateKey key) throws […]

使用“RSA / ECB / PKCS7Padding”与Bouncy Castle

我尝试使用“RSA / ECB / PKCS7Padding”进行加密。 JCE不支持它。 所以我下载了Bouncy Castle,但似乎Bouncy Castle也不支持这种转变。 以下代码: Security.insertProviderAt(new BouncyCastleProvider(), 1); Cipher cipher = Cipher.getInstance(“RSA/ECB/PKCS7Padding”); 投 Caused by: java.security.NoSuchAlgorithmException: Cannot find any provider supporting RSA/ECB/PKCS7Padding at javax.crypto.Cipher.getInstance(Cipher.java:524) …. Caused by: javax.crypto.NoSuchPaddingException: Unsupported padding PKCS7Padding at sun.security.pkcs11.P11RSACipher.engineSetPadding(P11RSACipher.java:129) at javax.crypto.Cipher$Transform.setModePadding(Cipher.java:360) at javax.crypto.Cipher.getInstance(Cipher.java:517) … 4 more 我做得对吗? TIA。

validation在Java中的golang中生成的rsa.SignPKCS1v15签名

我试图让Javavalidation签名的SHA-1哈希,但它一直返回false。 我在Go中有以下代码,它生成一个RSA密钥对并签名并返回任何命中/符号端点以及hex编码哈希和公钥模数和指数的消息: package main import ( “crypto” “crypto/rand” “crypto/rsa” “encoding/hex” “encoding/json” “fmt” “io” “net/http” “strconv” ) var PrivKey *rsa.PrivateKey type Message struct { Message string `json:”message”` } func (msg *Message) Decode(r io.Reader) error { return json.NewDecoder(r).Decode(&msg) } type Signature struct { Hash string `json:”hash”` Signature string `json:”signature”` N string `json:”N”` E string `json:”E”` } func […]

RSA / NONE / PKCS1Padding给出java.security.NoSuchAlgorithmException错误

我使用“RSA / None / PKCS1Padding”作为: Cipher RSACipher = Cipher.getInstance(“RSA/None/PKCS1Padding”); 这给了我例外: java.security.NoSuchAlgorithmException: Cannot find any provider supporting RSA/None/PKCS1Padding 感谢帮助。

RSA加密Android和Java环境之间的差异

首先,为我即将发布的代码量道歉。 我正在尝试使用我的Java应用程序中的RSA公钥来加密Android应用程序中的消息,然后将密文发送回Java环境进行解密,但在尝试解密时,我总是会收到此错误: javax.crypto.BadPaddingException: Decryption error at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380) at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291) at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356) at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382) at javax.crypto.Cipher.doFinal(Cipher.java:2087) … 密文确实包含正确的字节数(512),因此看到“填充错误”exception会让人感到困惑。 SO上的其他类似post建议使用“RSA / ECB / PKCS1Padding”作为算法,但这不起作用。 令人讨厌的是,Android环境中的加密和解密(使用Base64.URL_SAFE作为’base64Type’)工作正常,我似乎无法使用通过Java生成的公钥进行初始加密。 我已经将最小的代码提取到示例中,如下所示: Android代码 private void exampleMethod(){ String messageString = “Why does this not work in Android?”; String serverPubKey = “MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoApIBIna77xq4j+M2RmyIhsB++41NHcY4KIPfX4VP4ADnkO+7ejbs4le/twrPtYGESVPF9czSMB5bzmTBZtq0jC8oT/6wiDIBlSuzo4fBrGociBIuaOjyG/j3ZhpcWpWPXuzER+ehuQ+8hZkMuJdK9IodqPR+5jmCef4rXoKObwS02LYQ1co5dEmtZVQRmmeYaVnWibd/s1d4KKGvSzXap3YBTf8peH5UGIQrLOTqvX0bo34xFxmj5U0H3xudnnwuVAlQlj9KiHPPABuwNtm1buRKJb5HZhSCveyT/2YAOmQqGrVN/nALtlZyTDZNs//Vp1zb9exSuG0t5xFc+pn4QIDAQAB”; String encryptedMessageString = getUrlEncodedCipherText(messageString, serverPubKey, Base64.NO_WRAP); /** * CipherText is ALWAYS the same […]

使用KeyGenParameterSpec.Builder等效替换KeyPairGeneratorSpec – 密钥库操作失败

不推荐使用以下方法 KeyPairGenerator generator = KeyPairGenerator.getInstance(“RSA”, “AndroidKeyStore”); KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(this) .setAlias(alias) .setSubject(new X500Principal(“CN=Sample Name, O=Android Authority”)) .setSerialNumber(BigInteger.ONE) .setStartDate(start.getTime()) .setEndDate(end.getTime()) .build(); generator.initialize(spec); 我遇到的替代品看起来像这样 KeyPairGenerator generator = KeyPairGenerator.getInstance(“RSA”, “AndroidKeyStore”); generator.initialize(new KeyGenParameterSpec.Builder (alias, KeyProperties.PURPOSE_SIGN) .setDigests(KeyProperties.DIGEST_SHA256) .setSignaturePaddings(KeyProperties.SIGNATURE_PADDING_RSA_PKCS1) .build()); 虽然我能够使用它来生成密钥对条目并加密该值,但我无法解密它 public void encryptString(String alias) { try { KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry)keyStore.getEntry(alias, null); RSAPublicKey publicKey = (RSAPublicKey) privateKeyEntry.getCertificate().getPublicKey(); String initialText […]

生成给定模数和指数的RSA密钥

我被要求使用给定的modulus和exponent值生成RSA密钥。 但我只想知道生成密钥而不指定模数和指数。 无论给出什么价值,似乎都是大整数值。 我在网上搜索了这个并找到了一些东西,但它无法通过成功。 所以,如果有人以前这样做了,他们可以给我一些提示吗? 这是我们尝试使用给定值的示例程序。 import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.RSAKeyGenParameterSpec; import java.security.spec.RSAPrivateKeySpec; import java.security.spec.RSAPublicKeySpec; public class Sample { public static void main( String args[] ) { BigInteger modulus = new BigInteger(“350871044328208704010580786055405681”); BigInteger exponent = new BigInteger(“545161406957801571”); try { RSAPublicKeySpec […]