我想创建一个简单的RSA加密程序,我找到了这个教程https://sites.google.com/site/androidstem/computinglabware/module-9/2-lav-activity/lab1。我想知道是否可以预先定义公钥和私钥的值
公钥:Hello
私钥:再见
MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
try {
generateKey();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button enButton=(Button)findViewById(R.id.enbutton);
final Button deButton=(Button)findViewById(R.id.debutton);
final EditText input=(EditText)findViewById(R.id.input);
final EditText Raw=(EditText)findViewById(R.id.raw);
final EditText output=(EditText)findViewById(R.id.originText);
enButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
try {
Raw.setText(encrypt(input.getText().toString()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
});
deButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
try {
output.setText(String.valueOf(decrypt(Raw.getText().toString())));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private final static String RSA = "RSA";
public static PublicKey uk;
public static PrivateKey rk;
public static void generateKey() throws Exception
{
KeyPairGenerator gen = KeyPairGenerator.getInstance(RSA);
gen.initialize(512, new SecureRandom());
KeyPair keyPair = gen.generateKeyPair();
uk = keyPair.getPublic();
rk = keyPair.getPrivate();
}
private static byte[] encrypt(String text, PublicKey pubRSA) throws Exception
{
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, pubRSA);
return cipher.doFinal(text.getBytes());
}
public final static String encrypt(String text)
{
try {
return byte2hex(encrypt(text, uk));
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
public final static String decrypt(String data)
{
try{
return new String(decrypt(hex2byte(data.getBytes())));
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
private static byte[] decrypt(byte[] src) throws Exception
{
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE, rk);
return cipher.doFinal(src);
}
public static String byte2hex(byte[] b)
{
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n ++)
{
stmp = Integer.toHexString(b[n] & 0xFF);
if (stmp.length() == 1)
hs += ("0" + stmp);
else
hs += stmp;
}
return hs.toUpperCase();
}
public static byte[] hex2byte(byte[] b)
{
if ((b.length % 2) != 0)
throw new IllegalArgumentException("hello");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2)
{
String item = new String(b, n, 2);
b2[n/2] = (byte)Integer.parseInt(item, 16);
}
return b2;
}
}