• 【Java/RSA】非对称加密方法RSA加解密例程


    例程下载:https://files.cnblogs.com/files/heyang78/ufo_RSA_210930.rar

    程序:

    package ufo;
    
    import java.security.KeyFactory;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.interfaces.RSAPrivateKey;
    import java.security.interfaces.RSAPublicKey;
    
    import javax.crypto.Cipher;
    
    import org.apache.commons.codec.binary.Base64;
    
    public class RSA_TEST {
        public static void main(String... args) {
            String original="Let’s Build a Giant Airship 青海长云暗雪山";
            System.out.println("原文=	"+original);
            
            try {
                KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
                keyPairGen.initialize(1024);//生成大小 1024
                KeyPair keyPair = keyPairGen.generateKeyPair();
                RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();//获取公钥
                System.out.println("RSA公钥:"+Base64.encodeBase64String(publicKey.getEncoded()));
                
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                Cipher cipherEncode  = Cipher.getInstance(keyFactory.getAlgorithm());
                cipherEncode .init(Cipher.ENCRYPT_MODE, publicKey);
                byte[] EncodeResults =cipherEncode .doFinal(original.getBytes());
                System.out.println("密文=	"+Base64.encodeBase64String(EncodeResults));
                
                RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();//获取私钥
                System.out.println("RSA私钥:"+Base64.encodeBase64String(privateKey.getEncoded()));
                
                Cipher cipherDecode = Cipher.getInstance("RSA");
                cipherDecode.init(Cipher.DECRYPT_MODE, privateKey);
                byte[] DecodeResults = cipherDecode.doFinal(EncodeResults);
                System.out.println("解密后=	"+new String(DecodeResults));
                
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    输出:

    原文=    Let’s Build a Giant Airship 青海长云暗雪山
    RSA公钥:MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNZc6JPRecYxLpPj/JRfs2erjQgP2HEUW6CZS76FJV+x4vk9N31qP9R2lW2pJJoPnS+8LOcA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C4qkwacWu01ferxJ0DC6pLCqDaU1ygJL584NW1nlMnC91B99E2obT99MXfHwaS02RO3GVLHQ=
    解密后=    Let’s Build a Giant Airship 青海长云暗雪山

    END

  • 相关阅读:
    [LC] 71. Simplify Path
    [LC] 225. Implement Stack using Queues
    [Coding Made Simple / LeetCode 1235] Maximum Profit in Job Scheduling
    [Coding Made Simple] Cutting Rod for max profit
    [Coding Made Simple] Longest Common Substring
    [GeeksForGeeks] Convert an array to reduced form
    [GeeksForGeeks] Find if there is a pair with a given sum in a sorted and rotated array.
    [Coding Made Simple] Optimal Binary Search Tree
    [GeeksForGeeks] Write a program to delete a tree
    [GeeksForGeeks] Check if two trees are Isomorphic
  • 原文地址:https://www.cnblogs.com/heyang78/p/15355124.html
Copyright © 2020-2023  润新知