누구나 RSA 공개 키와 비공개 키를 사용하여 문자열 객체를 암호화하고 암호 해독하는 방법을 알고 있습니까?자바 키 쌍을 사용하여 암호화하고 암호 해독 하시겠습니까?
KeyPair 생성기를 사용하여 개인 및 공개 키를 만들었지 만 공개 키를 사용하여 데이터를 암호화하고 개인 키를 사용하여 암호를 해독하려고합니다.
public class Keys {
private static KeyPairGenerator generator;
private static KeyPair keyPair;
private static PrivateKey mPrivateKey;
private static PublicKey mPublicKey;
private static SecureRandom secureRandom;
private static final String SHA1PRNG = "SHA1PRNG";
public static final String RSA = "RSA";
private Keys() throws NoSuchAlgorithmException {
generator = KeyPairGenerator.getInstance("RSA");
}
/**
* Generate private and public key pairs
*
* @throws NoSuchAlgorithmException
*/
private static void generateKeyPair() throws NoSuchAlgorithmException {
// create SecureRandom object used to generate key pairs
secureRandom = SecureRandom.getInstance(SHA1PRNG);
// initialise generator
generator = KeyPairGenerator.getInstance(RSA);
generator.initialize(1024, secureRandom);
// generate keypair using generator
keyPair = generator.generateKeyPair();
// asssign private and public keys
setPrivateKey(keyPair.getPrivate());
setPublicKey(keyPair.getPublic());
}
/**
* Get private key from key generated
* @return
* @throws NoSuchAlgorithmException
*/
public static PrivateKey getPrivateKey() throws NoSuchAlgorithmException {
if (mPrivateKey == null) {
generateKeyPair();
}
return mPrivateKey;
}
private static void setPrivateKey(PrivateKey privateKey) {
mPrivateKey = privateKey;
}
/**
* Get public key from key pair generated
*
* @return
* @throws NoSuchAlgorithmException
*/
public PublicKey getPublicKey() throws NoSuchAlgorithmException {
if (mPublicKey == null) {
generateKeyPair();
}
return mPublicKey;
}
private static void setPublicKey(PublicKey publicKey) {
mPublicKey = publicKey;
}
이 가능합니까? 아니면 암호화가 동일한 키를 공유하고 사용해야합니까?
주요 목표는 다음과 같습니다.
암호화 된 데이터를주고받을 수있는 두 개의 클라이언트가 있습니다. 클라이언트 A의 공개 키에 대한
클라이언트 B 요청 : 클라이언트 A에 대한
는 암호화 된 데이터를받을 수 있습니다. 클라이언트 B는 문자열을 암호화하여 클라이언트 A로 보냅니다. 클라이언트 A는이 암호화 된 문자열을 수신 한 다음 자체 개인 키를 사용하여 암호를 해독합니다.
클라이언트 B가 암호화 된 데이터를 수신하려는 경우에도 마찬가지입니다.
키 생성 코드에 문제가 있습니까? 그렇지 않은 경우 질문에서 제거 될 수 있습니다. –