Java 如何用公钥加密字符串,用私钥解密? 我想用来自服务器的密钥加密密码,并在服务器端解密加密的密码。这是我在应用程序中使用的代码
我想用来自服务器的密钥加密密码,并在服务器端解密加密的密码。这是我在应用程序中使用的代码。如果您正在寻找一个java程序,用公钥加密数据,用私钥解密数据,那么下面是代码(使用RSA算法)Java 如何用公钥加密字符串,用私钥解密? 我想用来自服务器的密钥加密密码,并在服务器端解密加密的密码。这是我在应用程序中使用的代码,java,public-key-encryption,password-encryption,Java,Public Key Encryption,Password Encryption,我想用来自服务器的密钥加密密码,并在服务器端解密加密的密码。这是我在应用程序中使用的代码。如果您正在寻找一个java程序,用公钥加密数据,用私钥解密数据,那么下面是代码(使用RSA算法) 欢迎来到SO。请阅读:到底是什么问题?你没有明确说明你的问题是什么,但你根本不应该加密密码。请参阅TagWiki了解原因。您不应该在纯文本上使用公钥和私钥,因为我们有对称密钥加密。非对称密钥加密用于秘密传输对称密钥。我认为init函数的值不正确。根据Java API文档,cipher.init()方法的第一个参
欢迎来到SO。请阅读:到底是什么问题?你没有明确说明你的问题是什么,但你根本不应该加密密码。请参阅TagWiki了解原因。您不应该在纯文本上使用公钥和私钥,因为我们有对称密钥加密。非对称密钥加密用于秘密传输
对称密钥
。我认为init函数的值不正确。根据Java API文档,cipher.init()方法的第一个参数应该是:ENCRYPT_模式、DECRYPT_模式、,WRAP\u模式或UNWRAP\u模式。@opeongoPUBLIC\u-KEY
和ENCRYPT\u-MODE
的值为1,PRIVATE\u-KEY
和DECRYPT\u-MODE
的值为2,因此它将按预期工作,但我用最合适的常量名称对其进行了修改。谢谢你的通知。你能建议我如何生成像……….这样的公钥和私钥吗<代码>----开始PGP公钥块----MI0EXEMQFWEEANS1O8WI2KW1BIOHBEYGDBKUPHLO4EE98S2ZFMM2FS4M8SHKD=arHQ----结束PGP公钥块---
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package publicprivatekey;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import javax.crypto.*;
/**
*
* @author Rajorshi
*/
public class PublicPrivateKey {
public static String getEncrypted(String data, String Key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(Key.getBytes())));
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedbytes = cipher.doFinal(data.getBytes());
return new String(Base64.getEncoder().encode(encryptedbytes));
}
public static String getDecrypted(String data, String Key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
PrivateKey pk = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(Key.getBytes())));
cipher.init(Cipher.DECRYPT_MODE, pk);
byte[] encryptedbytes = cipher.doFinal(Base64.getDecoder().decode(data.getBytes()));
return new String(encryptedbytes);
}
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// TODO code application logic here
KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
keyGenerator.init(448);
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.genKeyPair();
String pubKey = new String(Base64.getEncoder().encode(keyPair.getPublic().getEncoded()));
String priKey = new String(Base64.getEncoder().encode(keyPair.getPrivate().getEncoded()));
System.out.println("Public Key:" + pubKey);
System.out.println("Private Key:" + priKey);
String cipherText = getEncrypted("hi this is a string", pubKey);
System.out.println("CHIPHER:" + cipherText);
String decryptedText = getDecrypted(cipherText, priKey);
System.out.println("DECRYPTED STRING:" + decryptedText);
}
}
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
/**
* @author visruthcv
*
*/
public class CryptographyUtil {
private static final String ALGORITHM = "RSA";
public static byte[] encrypt(byte[] publicKey, byte[] inputData)
throws Exception {
PublicKey key = KeyFactory.getInstance(ALGORITHM)
.generatePublic(new X509EncodedKeySpec(publicKey));
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(inputData);
return encryptedBytes;
}
public static byte[] decrypt(byte[] privateKey, byte[] inputData)
throws Exception {
PrivateKey key = KeyFactory.getInstance(ALGORITHM)
.generatePrivate(new PKCS8EncodedKeySpec(privateKey));
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedBytes = cipher.doFinal(inputData);
return decryptedBytes;
}
public static KeyPair generateKeyPair()
throws NoSuchAlgorithmException, NoSuchProviderException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
// 512 is keysize
keyGen.initialize(512, random);
KeyPair generateKeyPair = keyGen.generateKeyPair();
return generateKeyPair;
}
public static void main(String[] args) throws Exception {
KeyPair generateKeyPair = generateKeyPair();
byte[] publicKey = generateKeyPair.getPublic().getEncoded();
byte[] privateKey = generateKeyPair.getPrivate().getEncoded();
byte[] encryptedData = encrypt(publicKey,
"hi this is Visruth here".getBytes());
byte[] decryptedData = decrypt(privateKey, encryptedData);
System.out.println(new String(decryptedData));
}
}