Java에서 RSA 알고리즘을 사용하여 암호화/해독하는 간단한 프로그램을 작성하고 있습니다.Java를 사용한 RSA 암호화/암호 해독
//Create a Cipher object
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/NoPadding");
I 암호화를 수행하여 암호화 함수 호출 : 같이
String cipher=encrypt(textByte, pair, rsaCipher);
System.out.println("The Encryption using RSA Algorithm : "+cipher);
복호를 : I 출력을 표시 할 때
//Decryption
String plain=decrypt(Base64.decodeBase64(cipher),pair, rsaCipher);
System.out.println("The Decryption using RSA Algorithm : "+plain);
, 다음 I는 암호 객체를 만들 복호화 출력은 원본 텍스트보다 긴 공간을 반환합니다.
그러나 Cipher 객체를 생성하기위한 코드를 다음과 같이 편집합니다. // 암호 객체 만들기 암호 rsaCipher = Cipher.getInstance ("RSA");
즉은, 동작 모드 및 패딩 인수를 제거하여 문제가 해결 얻을 출력이된다 :
가 어디에 문제입니다. 첫 번째 경우 (공백이 나타날 때), NoPadding을 지정 했습니까? 해독 된 메시지에 공백이 나타나는 이유는 무엇입니까? 패딩을 사용해도 이런 일이 일어나지 않아야합니다.
편집 : 이것은 암호화이고 암호 해독 방법 :
public static String encrypt(byte[] textBytes, KeyPair pair, Cipher rsaCipher) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
//get the public key
PublicKey pk=pair.getPublic();
//Initialize the cipher for encryption. Use the public key.
rsaCipher.init(Cipher.ENCRYPT_MODE, pk);
//Perform the encryption using doFinal
byte[] encByte = rsaCipher.doFinal(textBytes);
// converts to base64 for easier display.
byte[] base64Cipher = Base64.encodeBase64(encByte);
return new String(base64Cipher);
}//end encrypt
public static String decrypt(byte[] cipherBytes, KeyPair pair, Cipher rsaCipher) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException
{
//get the public key
PrivateKey pvk=pair.getPrivate();
//Create a Cipher object
//Cipher rsaCipher = Cipher.getInstance("RSA/ECB/NoPadding");
//Initialize the cipher for encryption. Use the public key.
rsaCipher.init(Cipher.DECRYPT_MODE, pvk);
//Perform the encryption using doFinal
byte[] decByte = rsaCipher.doFinal(cipherBytes);
return new String(decByte);
}//end decrypt
'encrypt' 및'decrypt' 메소드를 보여줄 수 있습니까? – Craigy