{"id":725,"date":"2023-10-20T09:01:01","date_gmt":"2023-10-20T01:01:01","guid":{"rendered":""},"modified":"2023-10-20T09:01:01","modified_gmt":"2023-10-20T01:01:01","slug":"java\u5b9e\u73b0ecc\u52a0\u5bc6\uff1a\u901a\u8fc7AES\u751f\u6210\u516c\u94a5\u52a0\u5bc6\u6570\u636e\uff0cECC\u52a0\u5bc6\u516c\u94a5","status":"publish","type":"post","link":"https:\/\/mushiming.com\/725.html","title":{"rendered":"java\u5b9e\u73b0ecc\u52a0\u5bc6\uff1a\u901a\u8fc7AES\u751f\u6210\u516c\u94a5\u52a0\u5bc6\u6570\u636e\uff0cECC\u52a0\u5bc6\u516c\u94a5"},"content":{"rendered":"

\u5f00\u542f\u6398\u91d1\u6210\u957f\u4e4b\u65c5\uff01\u8fd9\u662f\u6211\u53c2\u4e0e\u300c\u6398\u91d1\u65e5\u65b0\u8ba1\u5212 \u00b7 12 \u6708\u66f4\u6587\u6311\u6218\u300d\u7684\u7b2c25\u5929\uff0c\u70b9\u51fb\u67e5\u770b\u6d3b\u52a8\u8be6\u60c5\u200b\u200b\u200b<\/p>\n

\u200b<\/p>\n

 \u6210\u529f\uff1a\"java\u5b9e\u73b0ecc\u52a0\u5bc6\uff1a\u901a\u8fc7AES\u751f\u6210\u516c\u94a5\u52a0\u5bc6\u6570\u636e\uff0cECC\u52a0\u5bc6\u516c\u94a5\"java\u5b9e\u73b0ecc\u52a0\u5bc6\uff1a\u901a\u8fc7AES\u751f\u6210\u516c\u94a5\u52a0\u5bc6\u6570\u636e\uff0cECC\u52a0\u5bc6\u516c\u94a5\u200b<\/p>\n

\u672c\u6587\u901a\u8fc7\u3002java\u8bed\u8a00\u5b9e\u73b0ECC+AES\u6df7\u5408\u52a0\u5bc6\u3002ECC\u52a0\u5bc6\u7b97\u6cd5\u5177\u6709\u5bc6\u94a5\u5206\u914d\u4e0e\u7ba1\u7406\u7b80\u5355,\u5b89\u5168\u5f3a\u5ea6\u9ad8\u7b49\u4f18\u70b9,AES\u7684\u52a0\u5bc6\u7b97\u6cd5\u5177\u6709\u901f\u5ea6\u5feb,\u5f3a\u5ea6\u9ad8,\u4fbf\u4e8e\u5b9e\u73b0\u7b49\u4f18\u70b9\u3002Ecc\u692d\u5706\u66f2\u7ebf\u7b97\u6cd5\u5bf9AES\u516c\u94a5\u8fdb\u884c\u52a0\u5bc6\u7ba1\u7406\uff0cAES\u4e3b\u8981\u4e3a\u6211\u4eec\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\u3002\u901a\u8fc7\u96c6\u6210AES\u52a0\u5bc6\u7b97\u6cd5\u548cECC\u52a0\u5bc6\u7b97\u6cd5\u7684\u4f18\u70b9,\u5b9e\u73b0\u4e86\u52a0\u5bc6\u901f\u5ea6\u5feb\u548c\u5b89\u5168\u65b9\u4fbf\u7ba1\u7406\u5bc6\u94a5\u7684\u4f18\u70b9,\u6709\u6548\u5730\u89e3\u51b3\u4e86\u5bc6\u7801\u4f53\u5236\u4e2d\u901f\u5ea6\u548c\u5b89\u5168\u6027\u4e0d\u80fd\u517c\u987e\u7684\u95ee\u9898\u3002JDK\u4e2d\u81ea\u5e26\u4e86\u692d\u5706\u66f2\u7ebf\u7684\u7b7e\u540d\uff0c\u4f46\u662f\u6ca1\u6709\u5b9e\u73b0\u692d\u5706\u66f2\u7ebf\u7684<\/strong>\u52a0\u5bc6\u89e3\u5bc6\u3002\u901a\u8fc7\u5f15\u5165bouncycastle\u5e93\u5b9e\u73b0\u5b9e\u73b0\u692d\u5706\u66f2\u7ebf\u7684<\/strong>\u52a0\u5bc6\u89e3\u5bc6\u3002<\/strong><\/p>\n

    \n
  1. \u53bbJDK\u7684\u4e0b\u8f7d\u9875\u9762\uff0c\u4e0b\u8f7d<\/li>\n<\/ol>\n

    www.oracle.com\/technetwork\u2026 
    \u8fd9\u4e2a\u4e1c\u897f\u3002\u8fd9\u4e2a\u662f\u4e3a\u4e86\u89e3\u9664\u9ed8\u8ba4JDK\u4e2d\u7684\u52a0\u5bc6\u5f3a\u5ea6\u7684\u9650\u5236\u3002\u4e0d\u4f7f\u7528\u8fd9\u4e2a\u53ef\u80fd\u4f1a\u62a5\u9519\u3002
    \u4e0b\u8f7d\u4e0b\u6765\u4ee5\u540e\uff0c\u9700\u8981\u5c06local_policy.jar \u548c US_export_policy.jar\u66ff\u6362\u6389D:\\Program Files\\Java\\jdk1.8.0_91\\jre\\lib\\security\u4e0b\u9762\u7684\u76f8\u540c\u7684\u4e24\u4e2ajar\u5305\u3002<\/p>\n

    \u76ee\u5f55<\/strong><\/p>\n

    \u901a\u8fc7AES\u83b7\u53d6\u516c\u94a5\u548c\u79c1\u94a5<\/p>\n

    \u8fdb\u884cECC\u52a0\u5bc6<\/p>\n


    \n

    \u57fa\u4e8eAES\u7684\u52a0\u5bc6\u7b97\u6cd5\u5177\u6709\u901f\u5ea6\u5feb,\u5f3a\u5ea6\u9ad8,\u4fbf\u4e8e\u5b9e\u73b0\u7b49\u4f18\u70b9<\/p>\n

    ECC\u52a0\u5bc6\u7b97\u6cd5\u5177\u6709\u5bc6\u94a5\u5206\u914d\u4e0e\u7ba1\u7406\u7b80\u5355,\u5b89\u5168\u5f3a\u5ea6\u9ad8\u7b49\u4f18\u70b9<\/p>\n

    \u91c7\u7528AES\u52a0\u5bc6\u7b97\u6cd5\u52a0\u5bc6\u5927\u6570\u636e\u5757,<\/strong> \u800c\u7528ECC\u52a0\u5bc6\u7b97\u6cd5\u7ba1\u7406AES\u5bc6\u94a5<\/strong><\/p>\n

    \u901a\u8fc7\u96c6\u6210AES\u52a0\u5bc6\u7b97\u6cd5\u548cECC\u52a0\u5bc6\u7b97\u6cd5\u7684\u4f18\u70b9,\u5b9e\u73b0\u4e86\u52a0\u5bc6\u901f\u5ea6\u5feb\u548c\u5b89\u5168\u65b9\u4fbf\u7ba1\u7406\u5bc6\u94a5\u7684\u4f18\u70b9,\u6709\u6548\u5730\u89e3\u51b3\u4e86\u5bc6\u7801\u4f53\u5236\u4e2d\u901f\u5ea6\u548c\u5b89\u5168\u6027\u4e0d\u80fd\u517c\u987e\u7684\u95ee\u9898.<\/p>\n

    AES\u52a0\u5bc6\u6807\u51c6\u53c8\u79f0\u4e3a\u9ad8\u7ea7\u52a0\u5bc6\u6807\u51c6Rijndael\u52a0\u5bc6\u6cd5\uff0c\u662f\u7f8e\u56fd\u56fd\u5bb6\u6807\u51c6\u6280\u672f\u7814\u7a76\u6240NIST\u65e8\u5728\u53d6\u4ee3DES\u768421\u4e16\u7eaa\u7684\u52a0\u5bc6\u6807\u51c6\u3002AES\u7684\u57fa\u672c\u8981\u6c42\u662f\uff0c\u91c7\u7528\u5bf9\u79f0\u5206\u7ec4\u5bc6\u7801\u4f53\u5236\uff0c\u5bc6\u94a5\u957f\u5ea6\u53ef\u4ee5\u4e3a128\u3001192\u6216256\u4f4d\uff0c\u5206\u7ec4\u957f\u5ea6128\u4f4d\uff0c\u7b97\u6cd5\u5e94\u6613\u5728\u5404\u79cd\u786c\u4ef6\u548c\u8f6f\u4ef6\u4e0a\u5b9e\u73b0\u3002<\/p>\n

    AES\u5c5e\u4e8e\u5bf9\u79f0\u52a0\u5bc6\u7b97\u6cd5\uff1b\u52a0\u5bc6\u3001\u89e3\u5bc6\u4f7f\u7528\u76f8\u540c\u7684\u5bc6\u94a5\uff0cAES\u52a0\u89e3\u5bc6\u8fc7\u7a0b\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n

    \"java\u5b9e\u73b0ecc\u52a0\u5bc6\uff1a\u901a\u8fc7AES\u751f\u6210\u516c\u94a5\u52a0\u5bc6\u6570\u636e\uff0cECC\u52a0\u5bc6\u516c\u94a5\"java\u5b9e\u73b0ecc\u52a0\u5bc6\uff1a\u901a\u8fc7AES\u751f\u6210\u516c\u94a5\u52a0\u5bc6\u6570\u636e\uff0cECC\u52a0\u5bc6\u516c\u94a5\u200b\u7f16\u8f91<\/p>\n

    \u901a\u8fc7AES\u83b7\u53d6\u516c\u94a5\u548c\u79c1\u94a5<\/h1>\n
    package<\/span> utils;\n\nimport<\/span> sun.misc.BASE64Decoder;\nimport<\/span> sun.misc.BASE64Encoder;\n\nimport<\/span> javax.crypto.Cipher;\nimport<\/span> javax.crypto.KeyGenerator;\nimport<\/span> javax.crypto.SecretKey;\nimport<\/span> javax.crypto.spec.SecretKeySpec;\nimport<\/span> java.io.IOException;\n\npublic<\/span> class<\/span> AESUtil<\/span> {\n    \/\/\u751f\u6210AES\u79d8\u94a5\uff0c\u7136\u540eBase64\u7f16\u7801<\/span>\n    public<\/span> static<\/span> String genKeyAES<\/span>()<\/span> throws<\/span> Exception{\n        KeyGenerator<\/span> keyGen<\/span> =<\/span> KeyGenerator.getInstance(\"AES\"<\/span>);\n        keyGen.init(128<\/span>);\n        SecretKey<\/span> key<\/span> =<\/span> keyGen.generateKey();\n        String<\/span> base64Str<\/span> =<\/span> byte2Base64(key.getEncoded());\n        return<\/span> base64Str;\n    }\n\n    \/\/\u5c06Base64\u7f16\u7801\u540e\u7684AES\u79d8\u94a5\u8f6c\u6362\u6210SecretKey\u5bf9\u8c61<\/span>\n    public<\/span> static<\/span> SecretKey loadKeyAES<\/span>(String base64Key)<\/span> throws<\/span> Exception{\n        byte<\/span>[] bytes = base642Byte(base64Key);\n        SecretKeySpec<\/span> key<\/span> =<\/span> new<\/span> SecretKeySpec<\/span>(bytes, \"AES\"<\/span>);\n        return<\/span> key;\n    }\n\n    \/\/\u52a0\u5bc6<\/span>\n    public<\/span> static<\/span> byte<\/span>[] encryptAES(byte<\/span>[] source, SecretKey key) throws<\/span> Exception{\n        Cipher<\/span> cipher<\/span> =<\/span> Cipher.getInstance(\"AES\"<\/span>);\n        cipher.init(Cipher.ENCRYPT_MODE, key);\n        return<\/span> cipher.doFinal(source);\n    }\n\n    \/\/\u89e3\u5bc6<\/span>\n    public<\/span> static<\/span> byte<\/span>[] decryptAES(byte<\/span>[] source, SecretKey key) throws<\/span> Exception{\n        Cipher<\/span> cipher<\/span> =<\/span> Cipher.getInstance(\"AES\"<\/span>);\n        cipher.init(Cipher.DECRYPT_MODE, key);\n        return<\/span> cipher.doFinal(source);\n    }\n\n    \/\/\u5b57\u8282\u6570\u7ec4\u8f6cBase64\u7f16\u7801<\/span>\n    public<\/span> static<\/span> String byte2Base64<\/span>(byte<\/span>[] bytes)<\/span>{\n        BASE64Encoder<\/span> encoder<\/span> =<\/span> new<\/span> BASE64Encoder<\/span>();\n        return<\/span> encoder.encode(bytes);\n    }\n\n    \/\/Base64\u7f16\u7801\u8f6c\u5b57\u8282\u6570\u7ec4<\/span>\n    public<\/span> static<\/span> byte<\/span>[] base642Byte(String base64Key) throws<\/span> IOException {\n        BASE64Decoder<\/span> decoder<\/span> =<\/span> new<\/span> BASE64Decoder<\/span>();\n        return<\/span> decoder.decodeBuffer(base64Key);\n    }\n}\n<\/code><\/pre>\n

    \"java\u5b9e\u73b0ecc\u52a0\u5bc6\uff1a\u901a\u8fc7AES\u751f\u6210\u516c\u94a5\u52a0\u5bc6\u6570\u636e\uff0cECC\u52a0\u5bc6\u516c\u94a5<\/p>\n

    \u8fdb\u884cECC\u52a0\u5bc6<\/h1>\n
    package utils;<\/span>\n\nimport java.security.KeyFactory;<\/span>\nimport java.security.KeyPair;<\/span>\nimport java.security.KeyPairGenerator;<\/span>\nimport java.security.PrivateKey;<\/span>\nimport java.security.PublicKey;<\/span>\nimport java.security.SecureRandom;<\/span>\nimport java.security.Security;<\/span>\nimport java.security.spec.PKCS8EncodedKeySpec;<\/span>\nimport java.security.spec.X509EncodedKeySpec;<\/span>\n\nimport javax.crypto.Cipher;<\/span>\n\nimport org.bouncycastle.jce.interfaces.ECPrivateKey;<\/span>\nimport org.bouncycastle.jce.interfaces.ECPublicKey;<\/span>\n\n\npublic class ECCUtil {\n    static {\n        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());<\/span>\n    }\n\n    \/\/\u751f\u6210\u79d8\u94a5\u5bf9\n    public static KeyPair getKeyPair() throws Exception {\n        KeyPairGenerator keyPairGenerator<\/span> = KeyPairGenerator.getInstance(\"EC\"<\/span>, \"BC\"<\/span>);<\/span>\n        keyPairGenerator.initialize(256, new SecureRandom());<\/span>\n        KeyPair keyPair<\/span> = keyPairGenerator.generateKeyPair();<\/span>\n        return keyPair;<\/span>\n    }\n\n    \/\/\u83b7\u53d6\u516c\u94a5(Base64\u7f16\u7801)\n    public static String getPublicKey(KeyPair keyPair){\n        ECPublicKey publicKey<\/span> = (ECPublicKey) keyPair.getPublic();<\/span>\n        byte[]<\/span> bytes<\/span> = publicKey.getEncoded();<\/span>\n        return AESUtil.byte2Base64(bytes);<\/span>\n    }\n\n    \/\/\u83b7\u53d6\u79c1\u94a5(Base64\u7f16\u7801)\n    public static String getPrivateKey(KeyPair keyPair){\n        ECPrivateKey privateKey<\/span> = (ECPrivateKey) keyPair.getPrivate();<\/span>\n        byte[]<\/span> bytes<\/span> = privateKey.getEncoded();<\/span>\n        return AESUtil.byte2Base64(bytes);<\/span>\n    }\n\n    \/\/\u5c06Base64\u7f16\u7801\u540e\u7684\u516c\u94a5\u8f6c\u6362\u6210PublicKey\u5bf9\u8c61\n    public static ECPublicKey string2PublicKey(String pubStr) throws Exception{\n        byte[]<\/span> keyBytes<\/span> = AESUtil.base642Byte(pubStr);<\/span>\n        X509EncodedKeySpec keySpec<\/span> = new X509EncodedKeySpec(keyBytes);<\/span>\n        KeyFactory keyFactory<\/span> = KeyFactory.getInstance(\"EC\"<\/span>, \"BC\"<\/span>);<\/span>\n        ECPublicKey publicKey<\/span> = (ECPublicKey) keyFactory.generatePublic(keySpec);<\/span>\n        return publicKey;<\/span>\n    }\n\n    \/\/\u5c06Base64\u7f16\u7801\u540e\u7684\u79c1\u94a5\u8f6c\u6362\u6210PrivateKey\u5bf9\u8c61\n    public static ECPrivateKey string2PrivateKey(String priStr) throws Exception{\n        byte[]<\/span> keyBytes<\/span> = AESUtil.base642Byte(priStr);<\/span>\n        PKCS8EncodedKeySpec keySpec<\/span> = new PKCS8EncodedKeySpec(keyBytes);<\/span>\n        KeyFactory keyFactory<\/span> = KeyFactory.getInstance(\"EC\"<\/span>, \"BC\"<\/span>);<\/span>\n        ECPrivateKey privateKey<\/span> = (ECPrivateKey) keyFactory.generatePrivate(keySpec);<\/span>\n        return privateKey;<\/span>\n    }\n\n    \/\/\u516c\u94a5\u52a0\u5bc6\n    public static byte[]<\/span> publicEncrypt(byte[]<\/span> content, PublicKey publicKey) throws Exception{\n        Cipher cipher<\/span> = Cipher.getInstance(\"ECIES\"<\/span>, \"BC\"<\/span>);<\/span>\n        cipher.init(Cipher.ENCRYPT_MODE, publicKey);<\/span>\n        byte[]<\/span> bytes<\/span> = cipher.doFinal(content);<\/span>\n        return bytes;<\/span>\n    }\n\n    \/\/\u79c1\u94a5\u89e3\u5bc6\n    public static byte[]<\/span> privateDecrypt(byte[]<\/span> content, PrivateKey privateKey) throws Exception{\n        Cipher cipher<\/span> = Cipher.getInstance(\"ECIES\"<\/span>, \"BC\"<\/span>);<\/span>\n        cipher.init(Cipher.DECRYPT_MODE, privateKey);<\/span>\n        byte[]<\/span> bytes<\/span> = cipher.doFinal(content);<\/span>\n        return bytes;<\/span>\n    }\n\n    public static void main(String[]<\/span> args) throws Exception {\n        KeyPair keyPair<\/span> = ECCUtil.getKeyPair();<\/span>\n        String publicKeyStr<\/span> = ECCUtil.getPublicKey(keyPair);<\/span>\n        String privateKeyStr<\/span> = ECCUtil.getPrivateKey(keyPair);<\/span>\n        System.out.println(\"ECC\u516c\u94a5Base64\u7f16\u7801:\" + publicKeyStr);<\/span>\n        System.out.println(\"ECC\u79c1\u94a5Base64\u7f16\u7801:\" + privateKeyStr);<\/span>\n\n        ECPublicKey publicKey<\/span> = string2PublicKey(publicKeyStr);<\/span>\n        ECPrivateKey privateKey<\/span> = string2PrivateKey(privateKeyStr);<\/span>\n\n        byte[]<\/span> publicEncrypt<\/span> = publicEncrypt(\"hello world\"<\/span>.getBytes(), publicKey);<\/span>\n        byte[]<\/span> privateDecrypt<\/span> = privateDecrypt(publicEncrypt, privateKey);<\/span>\n        System.out.println(new String(privateDecrypt));<\/span>\n    }\n}\n<\/code><\/pre>\n

    \"java\u5b9e\u73b0ecc\u52a0\u5bc6\uff1a\u901a\u8fc7AES\u751f\u6210\u516c\u94a5\u52a0\u5bc6\u6570\u636e\uff0cECC\u52a0\u5bc6\u516c\u94a5<\/p>\n

    \u200b<\/p>\n","protected":false},"excerpt":{"rendered":"java\u5b9e\u73b0ecc\u52a0\u5bc6\uff1a\u901a\u8fc7AES\u751f\u6210\u516c\u94a5\u52a0\u5bc6\u6570\u636e\uff0cECC\u52a0\u5bc6\u516c\u94a5\u5f00\u542f\u6398\u91d1\u6210\u957f\u4e4b\u65c5\uff01\u8fd9\u662f\u6211\u53c2\u4e0e\u300c\u6398\u91d1\u65e5\u65b0\u8ba1\u5212 \u00b7 12 \u6708\u66f4\u6587\u6311\u6218\u300d\u7684\u7b2c...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/725"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=725"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/725\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}