我在我的应用程序中使用RSA加密.要存储生成的公钥,我将其转换为String,然后将其保存在数据库中.
Key publicKey=null;
Key privateKey=null;
KeyPair keyPair=RsaCrypto.getKeyPairRSA(1024);
publicKey=keyPair.getPublic();
privateKey=keyPair.getPrivate();
String publicK=Base64.encodeToString(publicKey.getEncoded(), Base64.DEFAULT);
String privateK=Base64.encodeToString(privateKey.getEncoded(), Base64.DEFAULT);
Run Code Online (Sandbox Code Playgroud)
我保存字符串publicK和privateK.我的问题是,当我想用RSA加密/解密文本并使用我保存的Key in String格式时,我不知道如何将其转换为Key.
public static String encrypt(Key publicKey, String inputText){
byte[]encodedBytes=null;
String encryptedText="";
try {
Cipher cipher=Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encodedBytes=cipher.doFinal(inputText.getBytes());
} catch (Exception e) {Log.e("Error", "RSA encryption error"); }
encryptedText=Base64.encodeToString(encodedBytes, Base64.DEFAULT);
return encryptedText;
}
Run Code Online (Sandbox Code Playgroud)
你有什么主意吗?非常感谢
您好,我想从联系人中选择一个电话号码并将其插入到 EditText 中
但它在片段中不起作用我在活动中使用它并且它有效。请你能帮我我应该如何改变它<谢谢
public class Encrypt extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.encrypt, null);
phoneNumberDisplay = (EditText) v.findViewById(R.id.editTextPhoneNumber);
contactsButton=(Button)v.findViewById(R.id.buttonContacts);
contactsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(v==contactsButton){
Intent intent=new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
}
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri ur = data.getData();
Cursor c = managedQuery(ur, null, null, null, null);
if (c.moveToFirst()) {
String s …Run Code Online (Sandbox Code Playgroud)