Tag: public key encryption

从Base64编码的字符串中检索ECC公钥

我一直在尝试使用Base64编码的ECC公钥创建java.security.PublicKey的实例。 MainActivity.java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { byte[] data = decodePublicKey(“AsIAEFjzIcX+Kvhe8AmLoGUc8aYAEAwf5ecREGZ2u4RLxQuav/A=”); PublicKey publicKey = loadPublicKey(“secp128r1”, data); Log.d(TAG, publicKey.toString()); } catch (SQLException | IOException | GeneralSecurityException e) { Log.e(TAG, e.getMessage(), e); } } private byte[] decodePublicKey(String s) throws UnsupportedEncodingException { return Base64.decode(s, Base64.DEFAULT); } public PublicKey loadPublicKey(String curve, byte[] data) throws […]

让BouncyCastle解密GPG加密的消息

如何让BouncyCastle解密GPG加密的邮件? 我使用gpg –gen-key在CentOS 7命令行创建了一个GPG密钥对。 我选择RSA RSA作为加密类型,我使用gpg –export-secret-key -a “User Name” > /home/username/username_private.key导出密钥gpg –export-secret-key -a “User Name” > /home/username/username_private.key和gpg –armor –export 66677FC6 > /home/username/username_pubkey.asc 我能够将username_pubkey.asc导入另一个电子邮件帐户的远程Thunderbird客户端,并成功将加密的电子邮件发送到username@mydomain.com。 但是当我在mydomain.com上运行的Java / BouncyCastle代码尝试解密GPG编码的数据时,它会出现以下错误: org.bouncycastle.openpgp.PGPException: Encrypted message contains a signed message – not literal data. 如果你查看下面的代码,你会看到这与PGPUtils.decryptFile()的行相对应,其中说明了else if (message instanceof PGPOnePassSignatureList) {throw new PGPException(“Encrypted message contains a signed message – not literal data.”); […]

如何做Diffie Hellman密钥生成并在Java中检索原始密钥字节

我正在java中为现有程序编写测试工具。 作为这个的一部分,我需要生成一个Diffie Hellman密钥对,并以其原始(即未编码的字节)forms将公钥传递给另一个程序。 我可以使用以下代码成功使用密钥对: KeyPairGenerator kpg = KeyPairGenerator.getInstance(“DiffieHellman”); kpg.initialize(512); KeyPair dkp = kpg.generateKeyPair(); 但是,我似乎无法检索键的原始字节值:-(调用dkp.getPublic().getEncoded()以x509编码格式返回一个字节数组,但它的Key。 我有三种可能的前进方式: 找到一些以原始forms从上面获取关键数据的方法。 将密钥的x509编码解码为其原始格式 以允许访问原始密钥的不同方式生成密钥 但我不是如何去做任何一个(哪个会变得最好)? 任何帮助或建议将不胜感激!

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 […]

加密javascript中的字符串和java中的解密

我想知道是否有人知道任何库在javascript和java解密加密。 我已经尝试了很多API,但是在java中获得的不是相同的值。 我想要公钥 – 私钥加密,因此尝试使用RSA。 我很少使用的是: http://www-cs-students.stanford.edu/~tjw/jsbn/ http://ats.oka.nu/titaniumcore/js/crypto/readme.txt http://www.ohdave.com/rsa/ 我检查的东西很少,javascript将字符串分成小块然后加密它们,这使得密码文本在java和javascript中不同。 我编辑javascript代码使用字符串作为一个整体但没有工作。 我也尝试将html页面的charset设置为utf-8,但它也没有用。 我成功加密像’K’这样的单字符串正确加密和解​​密,这让我觉得加密javascript中的字符串是有问题的,把它分成小块(我检查过,但它失败了加密它作为一个整个)。 我的java实现是: BigInteger d = new BigInteger(“1f3fac65c4ae222e3a3074dd4c38fbb72c0705c4bbac0385b867c12c25a44e01”, 16); BigInteger e = new BigInteger(“65537”); BigInteger N = new BigInteger(“b42e91fbca364cf2a125aec67ffbdab624fd401100c40ea05189ba34d1028b0d”, 16); String messageToEncrypt = “kishor”; byte [] messageByte = messageToEncrypt.getBytes(); BigInteger message = new BigInteger(messageByte); //Encrypting and Decrypting messages //Encrypt a message using N and […]

Java – Diffie-Hellman加密 – 输出错误

我正在尝试实现Diffie-Hellman密钥交换。 我对如何使用密钥生成时有点困惑。 如输出中所示,在密钥交换中使用与正常相同的prime和base生成2个密钥,并在生成密钥时交换公钥,但是它们不输出与我预期相同的值。 我对如何实现这种加密方法感到非常困惑,并且非常感谢某些方向。 我的总体目标是实现加密的SMS Android应用程序。 (截图无法捕捉a&b的基础和素数的整个长度) AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator.getInstance(“DH”); paramGen.init(512); // number of bits AlgorithmParameters params = paramGen.generateParameters(); DHParameterSpec dhSpec = (DHParameterSpec)params.getParameterSpec(DHParameterSpec.class); BigInteger p512 = dhSpec.getP(); BigInteger g512 = dhSpec.getG(); //A KeyPairGenerator akpg = KeyPairGenerator.getInstance(“DiffieHellman”); DHParameterSpec param = new DHParameterSpec(p512, g512); System.out.println(“Prime: ” + p512); System.out.println(“Base: ” + g512); akpg.initialize(param); KeyPair kp = […]

PKCS#7登录并validation标志

我正在尝试使用PKCS#7签名并validation标志。 我正在跟着用Java开始加密的书。 我已经编写了示例代码来签名和validation。 当我试图附加签名并将其写入文件然后尝试validation我得到exception时(下面给出例外) 我想知道如何将这个签名数据写入文件? 我是否还需要将密钥库分享给将validation该标志的第二人? org.bouncycastle.cms.CMSException:message-digest属性值与计算值不匹配 在org.bouncycastle.cms.SignerInformation.doVerify(未知来源) at org.bouncycastle.cms.SignerInformation.verify(Unknown Source) at org.bouncycastle.cms.SignerInformation.verify(Unknown Source) 在com.inc.cms.test.bc.Test.isValidSignature(Test.java:150) 在com.inc.cms.test.bc.Test.verifyData(Test.java:120) 在com.inc.cms.test.bc.Test.main(Test.java:78) 非常感谢 我的代码如下 请帮我解决这个问题。 非常感谢 `package com.inc.cms.test.bc; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.math.BigInteger; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.KeyStore; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.security.cert.CertPathBuilder; import java.security.cert.CertStore; import java.security.cert.Certificate; import java.security.cert.CollectionCertStoreParameters; import java.security.cert.PKIXBuilderParameters; import java.security.cert.PKIXCertPathBuilderResult; […]

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; […]

使用PKCS#7加密

我正在使用Bouncy Castle提供的库来加密,解密,签名和validation签名。 我这样做 1.加密数据 2.签署数据 3.将带符号的字节写入文件 4.从文件中读取带符号的字节 5.validation签名 6.解密数据 我参考了Beginning Cryptography with Java 当我validation数据时,我的问题出在第5步 org.bouncycastle.cms.CMSException:message-digest属性值与计算值不匹配 我的代码如下 import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.math.BigInteger; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.KeyStore; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.security.cert.CertPathBuilder; import java.security.cert.CertStore; import java.security.cert.Certificate; import java.security.cert.CollectionCertStoreParameters; import java.security.cert.PKIXBuilderParameters; import java.security.cert.PKIXCertPathBuilderResult; import java.security.cert.TrustAnchor; import java.security.cert.X509CertSelector; import […]

Java BC SicBlockCipher直接输出等效于c#

我正在用C#实现一些东西,为此我有一个单独的规范和相当清楚的理解我需要做什么,但同时作为参考我有一个Java实现,并希望在这种情况下遵循Java实现尽我所能。 代码涉及加密流,Java源代码在这里相关的行在这里: private final StreamCipher enc; … BlockCipher cipher; enc = new SICBlockCipher(cipher = new AESEngine()); enc.init(true, new ParametersWithIV(new KeyParameter(secrets.aes), new byte[cipher.getBlockSize()])); … … byte[] ptype = RLP.encodeInt((int) frame.type); //Result can be a single byte long … … enc.processBytes(ptype, 0, ptype.length, buff, 0); out.write(buff, 0, ptype.length); //encrypt and write a single byte from the SICBlockCipher […]