나는 탄력 성 라이브러리를 통해 공개 키와 개인 키를 생성하는 Java 코드를 사용하고 있습니다. 내 문제는 내 안드로이드 장치에 의해 실행 가능한 코드에 그것을 구현합니다. 내 코드는 내가 프로그래밍 한 것처럼 RSA 키를 표시하지 않으며 대부분의 문제 해결을 통해 오류가 발생하지 않지만 요청할 때 코드를 수행 할 수 없습니다. 내 의심은 모든 코드를 try
/catch
블록에 넣는 방법이지만 실제로는 확실하지 않습니다. 편집 : 코드 하단탄력성을 사용하여 RSA 키 쌍을 생성합니다. Java 프로그램에서 코드 실행 가능
이것은 RSA 공개 및 개인 키를 생성하는 Java 클래스입니다. (작동)
public class ClassMain {
public static void main(String[]args) throws Exception {
String ST = "Ebenezersawesome";
byte[] plainText = "ST".getBytes("UTF8");
// Generating RSA Key
System.out.println("\nStart generating RSA key");
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(4096);
KeyPair key = keyGen.generateKeyPair();
System.out.println("Finish generating RSA key");
//
// Creates an RSA Cipher object (specifying the algorithm, mode, and
// padding).
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
//
// Print the provider information
System.out.println("\n" + cipher.getProvider().getInfo());
System.out.println("\nStart encryption");
//
// Initializes the Cipher object.
cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
//
// Encrypt the plaintext using the public key
byte[] cipherText = cipher.doFinal(plainText);
System.out.println("Finish encryption: ");
System.out.println(new String(cipherText, "UTF8"));
System.out.println("\nStart decryption");
//
// Initializes the Cipher object.
cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
//
// Decrypt the ciphertext using the private key
byte[] newPlainText = cipher.doFinal(cipherText);
System.out.println("Finish decryption: ");
System.out.println(new String(newPlainText, "UTF8"));
}
}
이것은 안드로이드 응용 프로그램에서 코드를 표시하려고하는 나의 시도입니다. 편집 : 코드가 작동하지만 내 try/catch 중지하고 tv3 코드를 생성하지 않습니다.
TextView tv1;
TextView tv2;
TextView tv3;
Button convert;
String publicKeyFilename = null;
String privateKeyFilename = null;
String ST = "Ebenezersawesome";
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("Ebz", "Made it to onCreate");
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
convert = (Button) findViewById(R.id.button1);
// tv2.setText(ST);
convert.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("Ebz", "Made it to onCreate");
try {
byte[] plainText = "ST".getBytes("UTF8");
Log.d("Ebz", "made it to Try Block");
KeyPairGenerator keyGen = KeyPairGenerator
.getInstance("RSA");
keyGen.initialize(2048);
KeyPair key = keyGen.generateKeyPair();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
tv1.setText(cipher.getProvider().getInfo().toString());
Log.d("Ebz", "Made it passed tv1");
//tv1.setText(ST);
cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
byte[] cipherText = cipher.doFinal(plainText);
tv2.setText(new String(cipherText, "UTF8").toString());
Log.d("Ebz", "Made it passed tv2");
// tv2.setText(ST);
byte[] newPlainText = cipher.doFinal(cipherText);
//tv3.setText(new String(newPlainText, "UTF8").toString());
// tv3.setText(ST);
Log.d("Ebz", "Made it passed tv3");
} catch (Exception e) {
System.out.println("error");
}
}
});
}
아마도 Android에서 SpongyCastle을 사용할 수 있습니다. 참고 : 4096 비트 ... 편집증 클럽에 오신 것을 환영합니다 ;-) – Vincent
@ Obliviator, 훨씬 더 .... 사람들이 이미 반응을 보았습니다 + ve'ly – Mayank
RSA 키를 생성하는 코드가 작동한다고 했으므로 작동하지 않습니다 ??? – Mayank