First, we can see: plaintext ---> Public Key ---> ciphertext --> key --> plaintext
RSA greatly improves file security because of the differences between public key <Public Key> and private key <key> <also called asymmetric algorithm encryption and decryption>. Symmetric algorithms are of the same key, which can be easily cracked.
RSA algorithm:
- ImportJava. Security. Key;
- ImportJava. Security. keyfactory;
- ImportJava. Security. keypair;
- ImportJava. Security. keypairgenerator;
- ImportJava. Security. privatekey;
- ImportJava. Security. publickey;
- ImportJava. Security. Interfaces. rsw.vatekey;
- ImportJava. Security. Interfaces. rsapublickey;
- ImportJava. Security. spec. pkcs8encodedkeyspec;
- ImportJava. Security. spec. x509encodedkeyspec;
- ImportJavax. crypto. cipher;
- ImportSun. Misc. base64decoder;
- ImportSun. Misc. base64encoder;
- Public ClassRsahelper {
- Public StaticPublickey getpublickey (string key)ThrowsException {
- Byte[] Keybytes;
- Keybytes = (NewBase64decoder (). decodebuffer (key );
- X509encodedkeyspec keyspec =NewX509encodedkeyspec (keybytes );
- Keyfactory = keyfactory. getinstance ("RSA ");
- Publickey = keyfactory. generatepublic (keyspec );
- ReturnPublickey;
- }
- Public StaticPrivatekey getprivatekey (string key)ThrowsException {
- Byte[] Keybytes;
- Keybytes = (NewBase64decoder (). decodebuffer (key );
- Pkcs8encodedkeyspec keyspec =NewPkcs8encodedkeyspec (keybytes );
- Keyfactory = keyfactory. getinstance ("RSA ");
- Privatekey = keyfactory. generateprivate (keyspec );
- ReturnPrivatekey;
- }
- Public StaticString getkeystring (Key key)ThrowsException {
- Byte[] Keybytes = key. getencoded ();
- String S = (NewBase64encoder (). encode (keybytes );
- ReturnS;
- }
- Public Static VoidMain (string [] ARGs)ThrowsException {
- Keypairgenerator keypairgen = keypairgenerator. getinstance ("RSA ");
- // Number of keys
- Keypairgen. initialize (1024 );
- // Key pair
- Keypair = keypairgen. generatekeypair ();
- // Public Key
- Publickey = (rsapublickey) keypair. getpublic ();
- // Private Key
- Privatekey = (rsw.vatekey) keypair. getprivate ();
- String publickeystring = getkeystring (publickey );
- System. Out. println ("public:/N" + publickeystring );
- String privatekeystring = getkeystring (privatekey );
- System. Out. println ("Private:/N" + privatekeystring );
- // Encryption/Decryption class
- Cipher cipher = cipher. getinstance ("RSA"); // cipher. getinstance ("RSA/ECB/pkcs1padding ");
- // Plaintext
- Byte[] Plaintext = "we are all good! Email: @ Sina.com ". getbytes ();
- // Encryption
- Cipher. INIT (Cipher. encrypt_mode, publickey );
- Byte[] Enbytes = cipher. dofinal (plaintext );
- // Obtain the key using the 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 =NewString (debytes );
- System. Out. println (s );
- }
- }
In actual development! We use our own modulus and publicexponent to encrypt and generate public keys, while privateexponet is used to generate private keys for development.
- ImportJava. Math. biginteger;
- ImportJava. Security. keyfactory;
- ImportJava. Security. privatekey;
- ImportJava. Security. publickey;
- ImportJava. Security. spec. rsw.vatekeyspec;
- ImportJava. Security. spec. rsapublickeyspec;
- ImportJavax. crypto. cipher;
- Public ClassRsakey {
- PublicPublickey getpublickey (string modulus, string publicexponent)ThrowsException {
- Biginteger M =NewBiginteger (modulus );
- Biginteger E =NewBiginteger (publicexponent );
- Rsapublickeyspec keyspec =NewRsapublickeyspec (M, e );
- Keyfactory = keyfactory. getinstance ("RSA ");
- Publickey = keyfactory. generatepublic (keyspec );
- ReturnPublickey;
- }
- PublicPrivatekey getprivatekey (string modulus, string privateexponent)ThrowsException {
- Biginteger M =NewBiginteger (modulus );
- Biginteger E =NewBiginteger (privateexponent );
- Rsw.vatekeyspec keyspec =NewRsw.vatekeyspec (M, e );
- Keyfactory = keyfactory. getinstance ("RSA ");
- Privatekey = keyfactory. generateprivate (keyspec );
- ReturnPrivatekey;
- }
- Public Static VoidMain (string [] ARGs)ThrowsException {
- String modulus = "10103166745709600780215616551837697832816413714471062522342538060943596036859967333870827790358555455232243383580565187280643159050869924436081447583051139 ";
- String publicexponent = "65537 ";
- String privateexponet = "367979294475011322800474185715497882523349856362702385535371444397399388741997039894583483410120364529325888461124714276674612930833020362278754665756193 ";
- Rsakey key =NewRsakey ();
- Publickey = key. getpublickey (modulus, publicexponent );
- Privatekey = key. getprivatekey (modulus, privateexponet );
- // Encryption/Decryption class
- Cipher cipher = cipher. getinstance ("RSA"); // "RSA/ECB/pkcs1padding" indicates "algorithm/working mode/filling mode"
- // Plaintext
- Byte[] Plaintext = "Hello world! ". Getbytes ();
- // Encryption
- Cipher. INIT (Cipher. encrypt_mode, publickey );
- Byte[] Enbytes = cipher. dofinal (plaintext );
- Cipher. INIT (Cipher. decrypt_mode, privatekey );
- Byte[] Debytes = cipher. dofinal (enbytes );
- String S =NewString (debytes );
- System. Out. println (s );
- }
- }