In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today Xiaobian to share with you about the cryptographic system AES private key RSA public key encryption and decryption of the relevant knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.
Preface
Keys exist in pairs. encryption and decryption use different keys (public keys), that is, asymmetric key cryptosystems. Each communicating party needs two keys, namely public key and private key, and uses public key for encryption operation and private key for decryption operation. The public key is public and does not need to be kept secret, while the private key is held by the individual and must be properly kept and kept secret. Cryptography is extensive and profound. The following examples are for reference only.
The interpretation of Encyclopedia
Public key (Public Key) and private key (Private Key) are a key pair obtained by an algorithm (that is, a public key and a private key). The public key is the public part of the key pair, and the private key is the non-public part. The public key is typically used to encrypt session keys, verify digital signatures, or encrypt data that can be decrypted with the corresponding private key. The key pair obtained by this algorithm can be guaranteed to be unique in the world. When using this key pair, if you encrypt a piece of data with one key, you must decrypt it with the other key. For example, encrypting data with a public key must be decrypted with a private key, and if encrypted with a private key, it must also be decrypted with a public key, otherwise the decryption will not succeed.
An example of java using public and private keys to encrypt and decrypt
For reference only
/ * * data encryption string to be encrypted by plainTextData * @ param plainTextData * @ return * @ throws Exception * / public static Map encrypt (String plainTextData) throws Exception {HashMap result = new HashMap (); / / keySpec generates symmetric key KeyGenerator keyGenerator = KeyGenerator.getInstance ("AES"); keyGenerator.init (128); SecretKey secretKey = keyGenerator.generateKey () SecretKeySpec keySpec = new SecretKeySpec (secretKey.getEncoded (), "AES"); / / RSA encrypts the 'symmetric key' with the other party's public key Cipher cipher = Cipher.getInstance ("RSA"); String keyFilePathName = pertery.getProperty ("bsbank_Key_path") + "PublicKey.keystore"; cipher.init (Cipher.WRAP_MODE, loadPublicKeyByStr (loadKeyByFile (keyFilePathName) Byte [] wrappedKey = cipher.wrap (keySpec); result.put ("wrappedKey", Base64.encodeBase64String (wrappedKey)); / / encrypted data cipher = Cipher.getInstance ("AES"); cipher.init (Cipher.ENCRYPT_MODE, keySpec); byte [] encryptedData = cipher.doFinal (plainTextData.getBytes ("UTF-8")); result.put ("encryptedData", Base64.encodeBase64String (encryptedData); return result } / * data decryption encryptedData * @ param encryptedData * @ return * @ throws Exception * / public static Map decrypt (Map encryptedData) throws Exception {/ / get the key byte [] wrappedKey = Base64.decodeBase64 (encryptedData.get ("wrappedKey"). ToString ()); HashMap result = new HashMap () / / RSA decryption key Cipher cipher = Cipher.getInstance ("RSA"); String keyFilePathName = pertery.getProperty ("bsbank_Key_path") + "privateKey.keystore"; / / decrypt cipher.init (Cipher.UNWRAP_MODE, loadPrivateKeyByStr (loadKeyByFile (keyFilePathName)) using the other party's private key; Key key = cipher.unwrap (wrappedKey, "AES", Cipher.SECRET_KEY) / / decrypt data cipher = Cipher.getInstance ("AES"); cipher.init (Cipher.DECRYPT_MODE, key); byte [] decryptedData = cipher.doFinal (Base64.decodeBase64 (encryptedData. Get ("encryptedData"). ToString ()); result.put ("decryptedData", new String (decryptedData, "UTF-8")); result.put ("wrappedKey", Base64.encodeBase64String (wrappedKey)) Return result;} private static String loadKeyByFile (String filePathName) throws Exception {BufferedReader br = null; StringBuilder sb = new StringBuilder (); try {br = new BufferedReader (new FileReader (filePathName)); String readLine = null; while ((readLine = br.readLine ())! = null) {sb.append (readLine) }} catch (Exception e) {throw e;} finally {if (null! = br) {br.close ();}} return sb.toString ();} private static RSAPublicKey loadPublicKeyByStr (String publicKeyStr) throws Exception {RSAPublicKey publicKey = null Try {byte [] buffer = Base64.decodeBase64 (publicKeyStr); KeyFactory keyFactory = KeyFactory.getInstance ("RSA"); X509EncodedKeySpec keySpec = new X509EncodedKeySpec (buffer); publicKey = (RSAPublicKey) keyFactory.generatePublic (keySpec);} catch (Exception e) {logger.error ("failed to load pubKey", e); throw e;} return publicKey } private static RSAPrivateKey loadPrivateKeyByStr (String privateKeyStr) throws Exception {RSAPrivateKey privateKey = null; try {byte [] buffer = Base64.decodeBase64 (privateKeyStr); KeyFactory keyFactory = KeyFactory.getInstance ("RSA"); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec (buffer); privateKey = (RSAPrivateKey) keyFactory.generatePrivate (keySpec) } catch (Exception e) {logger.error ("failed to loadPrivateKeyByStr", e); throw e;} return privateKey;} / * output public and private key pair * @ param filePath * @ throws Exception * / private static void genKeyPair (String filePath) throws Exception {KeyPairGenerator keyPairGen = null Try {keyPairGen = KeyPairGenerator.getInstance ("RSA");} catch (NoSuchAlgorithmException e) {logger.error ("failed to do key gen", e); throw e;} keyPairGen.initialize (1024, new SecureRandom ()); KeyPair keyPair = keyPairGen.generateKeyPair (); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate (); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic () Try {String publicKeyString = Base64.encodeBase64String (publicKey. GetEncoded ()); String privateKeyString = Base64.encodeBase64String (privateKey. GetEncoded ()); FileWriter pubfw = new FileWriter (filePath + "/ PublicKey.keystore"); FileWriter prifw = new FileWriter (filePath + "/ PrivateKey.keystore"); BufferedWriter pubbw = new BufferedWriter (pubfw) BufferedWriter pribw = new BufferedWriter (prifw); pubbw.write (publicKeyString); pribw.write (privateKeyString); pubbw.flush (); pubbw.close (); pubfw.close (); pribw.flush (); pribw.close (); prifw.close () } catch (IOException e) {logger.error ("failed to genKeypair", e);}} above are all the contents of the article "cryptosystem AES private key RSA public key encryption and decryption". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.