android Java语言非对称加密的实现
2013-01-26 17:12
417 查看
最近做一个android项目要用到非对称加密,下面给出代码:
package com.example.encryption; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.security.Key; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import javax.crypto.Cipher; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class EntryptionActivity extends Activity { private static final String TAG="EntryptionActivity"; private PrivateKey privateKey; private String inputString; private EditText encryptionEditText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_entryption); encryptionEditText = (EditText)findViewById(R.id.encryptionedit); encryptionEditText.clearFocus(); //((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(encryptionEditText,R.id.encryptionedit); Button decrypitionButton = (Button)findViewById(R.id.decryptionbutton); Button encrypiontButton = (Button)findViewById(R.id.encryptionbutton); encrypiontButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { inputString = encryptionEditText.getText().toString(); PublicEnrypt(inputString); } catch (Exception e) { e.printStackTrace(); } } }); decrypitionButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { privateDecrypt(); } catch (Exception e) { e.printStackTrace(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_entryption, menu); return true; } /* * 公钥加密 */ private void PublicEnrypt(String inputString)throws Exception { Log.i(TAG, "----------------->加密"); Cipher cipher =Cipher.getInstance("RSA"); //实例化Key KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA"); //设置密钥长度,自己加入 keyPairGenerator.initialize(1024); //获取一对钥匙 KeyPair keyPair=keyPairGenerator.generateKeyPair(); //获得公钥 Key publicKey=keyPair.getPublic(); //获得私钥 privateKey=keyPair.getPrivate(); //用公钥加密 cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte [] result=cipher.doFinal(inputString.getBytes("UTF-8")); saveData(result,"public_encryt.dat"); //将Key写入到文件 //saveKey(privateKey,"zxx_private.key"); //加密后的数据写入到文件 } /* * 私钥解密 */ private void privateDecrypt() throws Exception { Cipher cipher=Cipher.getInstance("RSA"); //得到Key //Key privateKey=readKey("zxx_private.key"); //Log.i(TAG, "私钥:"+ privateKey.toString()); //用私钥去解密 //cipher.init(Cipher.DECRYPT_MODE, privateKey); cipher.init(Cipher.DECRYPT_MODE, privateKey); //读数据源 byte [] src =readData("public_encryt.dat"); Log.i(TAG, "----------------->解密"); //得到解密后的结果 byte[] result=cipher.doFinal(src); //二进制数据要变成字符串需解码 //System.out.println(new String(result,"UTF-8")); String outputString = new String(result, "UTF-8"); Log.i(TAG, outputString); Toast.makeText(getApplicationContext(), "您输入的明文是:" + outputString, Toast.LENGTH_LONG).show(); } /** * 存储加密后的数据 * @param result 加密数据 * @param fileName 存储文件名 * @throws Exception */ private void saveData(byte[] result, String fileName) throws Exception { FileOutputStream fosData = openFileOutput(fileName, MODE_PRIVATE); fosData.write(result); fosData.close(); } public static void saveKey(Key key,String fileName)throws Exception{ FileOutputStream fosKey=new FileOutputStream(fileName); ObjectOutputStream oosSecretKey =new ObjectOutputStream(fosKey); oosSecretKey.writeObject(key); oosSecretKey.close(); fosKey.close(); } /*//将私钥存储,可以减少内存的占用,但存储和读取降低了程序的执行效率。 private static Key readKey(String fileName) throws Exception { FileInputStream fisKey=new FileInputStream(fileName); ObjectInputStream oisKey =new ObjectInputStream(fisKey); Key key=(Key)oisKey.readObject(); Log.i(TAG, "私钥" + key.toString()); oisKey.close(); fisKey.close(); Log.i(TAG, "-----------读取密钥"); return key; }*/ private byte[] readData(String filename) throws Exception { FileInputStream fisDat=openFileInput(filename); byte [] src=new byte [fisDat.available()]; int len =fisDat.read(src); int total =0; while(total<src.length){ total +=len; len=fisDat.read(src,total,src.length-total); } fisDat.close(); return src; } }
相关文章推荐
- C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现
- Java 实现 RSA 非对称加密
- Windows 平台上的使用 Java 语言实现 appium 自动化程序 for Android(完整版)
- Java 实现 RSA 非对称加密
- Java实现非对称加密
- GO语言JAVA语言实现的AES/CFB/256位的加密解密例子
- java实现DH非对称加密工具包
- 数据传输加密:非对称加密算法RSA+对称算法AES(适用于java,android和Web)
- C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现
- RSA非对称加密Java实现
- java实现RSA非对称加密算法 对数据进行加密【附代码】
- java实现对称加密AES与非对称加密RSA算法
- Java 实现非对称加密
- Go语言解密上篇中用java aes实现的加密
- 基于Android系统的AES加密、解密的JAVA实现
- Appium :Windows 平台上的使用 Java 语言实现 appium 自动化程序 for Android
- Java实现RSA非对称加密
- Java实现的RSA非对称加密工具
- [置顶] 数据传输加密——非对称加密算法RSA+对称算法AES(适用于java,android和Web)
- 非对称加密算法之java实现