내 작업 : 나는 모듈러스 및 지수로 데이터 (RSA)와 공개 키를 암호화했습니다. 암호 해독 코드를 작성해야합니다. 그것으로
내 문제 : 내 구현이 작동하지 않습니다는) 내가 아는 한 철학은 간단하다
"오픈 텍스트"== RSA (PUBLIC_KEY, RSA (PRIVATE_KEY, "오픈 텍스트"))
편집 : 정확히 내 가정은 틀렸다. (가정은 모든 부유 한 엄마 다.)). RSA에서는 공개 키가 암호화에 사용되고 복호화에는 개인용이기 때문에 "open text" == rsa(private_key, rsa(public_key, "open text"))
이어야합니다. 내가 명령을 사용하여 공개 키 계수와 지수를 가지고modulus와 지수를 사용하는 RSA 암호 해독
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
:
나는 내가 그런 방식으로 자신의 키를 만들어 시험 있도록 암호화시에 사용하는 개인 키에 해당하지 않는 공개 키를 가질 수 있다고 가정 : 암호화 테스트를 위해 나는
//Reads private key from file
//StringPasswordFinder is my tmp implementation of PasswordFinder
PEMReader pemReader = new PEMReader(new FileReader("/path/to/server.key"), new StringPasswordFinder());
KeyPair keyPair = (KeyPair) pemReader.readObject();
PrivateKey pk = keyPair.getPrivate();
//text for encryption
String openText = "openText";
//encryption
Cipher rsaCipher = Cipher.getInstance("RSA", "BC");
rsaCipher.init(Cipher.ENCRYPT_MODE, pk);
byte[] encrypted = rsaCipher.doFinal(openText.getBytes("utf-8"));
그리고 decrypti에 대한 코드
를 사용하고openssl x509 -in server.crt -text
암호화 된 텍스트에 나는 코드를 사용합니다.
//modulus hex got using openssl
byte[] modulus = Hex.decodeHex("very long hex".toCharArray());
//exponent hex got using openssl
byte[] exponent = Hex.decodeHex("010001".toCharArray());
//initialization of rsa decryption engine
RSAEngine rsaEngine = new RSAEngine();
rsaEngine.init(false, new RSAKeyParameters(false, new BigInteger(modulus), new BigInteger(exponent)));
//input - encrypted stream
ByteArrayInputStream bais = new ByteArrayInputStream(encrypted);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//reading blocks from the input stream and decrypting them
int bytesRead = 0;
byte[] block = new byte[rsaEngine.getInputBlockSize()];
while ((bytesRead = bais.read(block)) > -1) {
baos.write(rsaEngine.processBlock(block, 0, bytesRead));
}
//dispalying decrypted text
System.out.println(new String(baos.toByteArray(), "utf-8"));
그리고 표시된 모든 텍스트는 그렇지 않습니다. 아무도 내가 틀린 곳을 보여줄 수 있습니까?
편집 :이 문제를 요약하면 해결책이 없습니다. 개인 키를 사용하여 메시지를 암호화 할 수 없으므로 나중에 공개 키를 사용하여 암호를 해독합니다. 일반적으로 나는 암호화와 서명 메시지 및 암호 해독을 검증과 섞었다. 왜냐하면 서명하는 동안 개인 키가 사용되고 인증 과정에서 공개가 사용되기 때문입니다. Btw, MByD thx 중요한 단서. 내가 RSA에 대한 자바 라이브러리와 친숙하지 않다
감사합니다. 그것은 단지 그것에 관한 것이었다. 서명을 위해 개인 키를 사용했습니다 (공개 키가 아닌). 변경 후 작업이 시작되었습니다. – emstol