Android's RAS encryption algorithm test example _android

Source: Internet
Author: User
Tags key string
Copy Code code as follows:

Import Java.security.Key;
Import Java.security.KeyFactory;
Import Java.security.KeyPair;
Import Java.security.KeyPairGenerator;
Import Java.security.PrivateKey;
Import Java.security.PublicKey;
Import Java.security.interfaces.RSAPrivateKey;
Import Java.security.interfaces.RSAPublicKey;
Import Java.security.spec.PKCS8EncodedKeySpec;
Import Java.security.spec.X509EncodedKeySpec;

Import Javax.crypto.Cipher;

Import Sun.misc.BASE64Decoder;
Import Sun.misc.BASE64Encoder;


public class Rsahelper {


public static PublicKey Getpublickey (String key) throws Exception {
Byte[] keybytes;
Keybytes = (new Base64decoder ()). Decodebuffer (key);

X509encodedkeyspec Keyspec = new X509encodedkeyspec (keybytes);
Keyfactory keyfactory = keyfactory.getinstance ("RSA");
PublicKey PublicKey = Keyfactory.generatepublic (Keyspec);
return publickey;
}

public static Privatekey Getprivatekey (String key) throws Exception {
Byte[] keybytes;
Keybytes = (new Base64decoder ()). Decodebuffer (key);

Pkcs8encodedkeyspec Keyspec = new Pkcs8encodedkeyspec (keybytes);
Keyfactory keyfactory = keyfactory.getinstance ("RSA");
Privatekey Privatekey = keyfactory.generateprivate (Keyspec);
return privatekey;
}


public static String getkeystring (key key) throws Exception {
byte[] keybytes = key.getencoded ();
String s = (new Base64encoder ()). Encode (keybytes);
return s;
}


public static void Main (string[] args) throws Exception {

Keypairgenerator Keypairgen = keypairgenerator.getinstance ("RSA");
Number of key digits
Keypairgen.initialize (1024);
Key pair
KeyPair KeyPair = Keypairgen.generatekeypair ();

Public
PublicKey PublicKey = (rsapublickey) keypair.getpublic ();

Private
Privatekey Privatekey = (rsaprivatekey) keypair.getprivate ();

String publickeystring = getkeystring (PublicKey);
System.out.println ("public:\n" + publickeystring);

String privatekeystring = getkeystring (Privatekey);
System.out.println ("private:\n" + privatekeystring);

Encryption and Decryption class
Cipher Cipher = cipher.getinstance ("RSA");//cipher.getinstance ("rsa/ecb/pkcs1padding");

Plaintext
Byte[] PlainText = "We are all very well!" Mail: @sina. com ". GetBytes ();

Encryption
Cipher.init (Cipher.encrypt_mode, PublicKey);
byte[] Enbytes = cipher.dofinal (plaintext);

Get key through key string
PublicKey = Getpublickey (publickeystring);
Privatekey = Getprivatekey (privatekeystring);

Decrypt
Cipher.init (Cipher.decrypt_mode, Privatekey);
Byte[]debytes = Cipher.dofinal (enbytes);

publickeystring = getkeystring (PublicKey);
System.out.println ("public:\n" +publickeystring);

privatekeystring = getkeystring (Privatekey);
System.out.println ("private:\n" + privatekeystring);

string s = new string (debytes);
System.out.println (s);


}

}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.