{"id":956,"date":"2023-03-11T19:11:41","date_gmt":"2023-03-11T11:11:41","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=956"},"modified":"2023-04-29T13:30:42","modified_gmt":"2023-04-29T05:30:42","slug":"android-aes-encryption-and-decryption-compatible-with-android-7","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-aes-encryption-and-decryption-compatible-with-android-7\/","title":{"rendered":"Android AES\u52a0\u89e3\u5bc6\uff08\u517c\u5bb9Android7.0\uff09"},"content":{"rendered":"<p>Android 7.0\u53ca\u4ee5\u4e0a <code>AES<\/code>\u52a0\u5bc6\u62a5\u9519\uff1a<\/p>\n<pre><code>New versions of the Android SDK no longer support the Crypto provider.<\/code><\/pre>\n<p>\u539f\u56e0\u5728\u4e8e\u751f\u6210 <code>AES<\/code>\u52a0\u5bc6\u5bc6\u94a5\u65f6\uff0c\u5728 <code>Axndroid N<\/code> \u4e0a\u5df2\u7ecf\u5c06\u76f8\u5173\u7684 <code>Crypto provider<\/code> \u548c <code>SHA1PRNG<\/code> \u7b97\u6cd5\u540c\u65f6\u5e9f\u5f03\u6389\u4e86\uff0c\u5e76\u8ba1\u5212\u5728\u540e\u7eed\u7684 SDK \u4e2d\u5b8c\u5168\u79fb\u9664\u76f8\u5173\u7684\u5e93\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>\u589e\u52a0 CryptoProvider \u7c7b<\/h2>\n<pre><code class=\"language-java\">\/**\n * Aes\u5bc6\u94a5\u5904\u7406\n * @param seed\n * @return\n * @throws Exception\n *\/\nprivate static byte[] getRawKey(byte[] seed) throws Exception {\n    KeyGenerator kgen = KeyGenerator.getInstance(AES);\n    \/\/for android\n    SecureRandom sr = null;\n    \/\/ \u57284.2\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0cSecureRandom\u83b7\u53d6\u65b9\u5f0f\u53d1\u751f\u4e86\u6539\u53d8\n    if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN_MR1) {\n        sr = SecureRandom.getInstance(SHA1PRNG, &quot;Crypto&quot;);\n    } else if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.N) {\n        sr = SecureRandom.getInstance(SHA1PRNG, new CryptoProvider());\n    } else {\n        sr = SecureRandom.getInstance(SHA1PRNG);\n    }\n    \/\/ for Java\n    \/\/ secureRandom = SecureRandom.getInstance(SHA1PRNG);\n    sr.setSeed(seed);\n    kgen.init(128, sr); \/\/256 bits or 128 bits,192bits\n    \/\/AES\u4e2d128\u4f4d\u5bc6\u94a5\u7248\u672c\u670910\u4e2a\u52a0\u5bc6\u5faa\u73af\uff0c192\u6bd4\u7279\u5bc6\u94a5\u7248\u672c\u670912\u4e2a\u52a0\u5bc6\u5faa\u73af\uff0c256\u6bd4\u7279\u5bc6\u94a5\u7248\u672c\u5219\u670914\u4e2a\u52a0\u5bc6\u5faa\u73af\u3002\n    SecretKey skey = kgen.generateKey();\n    byte[] raw = skey.getEncoded();\n    return raw;\n}\n\n\/\/ \u589e\u52a0 CryptoProvider \u7c7b\npublic static  class CryptoProvider extends Provider {\n    \/**\n     * Creates a Provider and puts parameters\n     *\/\n    public CryptoProvider() {\n        super(&quot;Crypto&quot;, 1.0, &quot;HARMONY (SHA1 digest; SecureRandom; SHA1withDSA signature)&quot;);\n        put(&quot;SecureRandom.SHA1PRNG&quot;, &quot;org.apache.harmony.security.provider.crypto.SHA1PRNG_SecureRandomImpl&quot;);\n        put(&quot;SecureRandom.SHA1PRNG ImplementedIn&quot;, &quot;Software&quot;);\n    }\n}<\/code><\/pre>\n<h2>Google \u7ec8\u6781\u63a8\u8350<\/h2>\n<p>\u53c2\u8003\uff1a[Android 7.0\u540e\u52a0\u5bc6\u5e93Crypto\u88ab\u5e9f\u5f03\u540e\u586b\u5751](<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/www.appblog.cn\/2018\/08\/09\/Android\">http:\/\/www.appblog.cn\/2018\/08\/09\/Android<\/a> 7.0\u540e\u52a0\u5bc6\u5e93Crypto\u88ab\u5e9f\u5f03\u540e\u586b\u5751\/ &quot;Android 7.0\u540e\u52a0\u5bc6\u5e93Crypto\u88ab\u5e9f\u5f03\u540e\u586b\u5751&quot;)<\/p>\n<h2>\u5f00\u6e90 AES\u52a0\u89e3\u5bc6\u5e93<\/h2>\n<ul>\n<li>java-aes-crypto\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/tozny\/java-aes-crypto\">https:\/\/github.com\/tozny\/java-aes-crypto<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Android 7.0\u53ca\u4ee5\u4e0a AES\u52a0\u5bc6\u62a5\u9519\uff1a New versions of the Android SDK [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[260],"class_list":["post-956","post","type-post","status-publish","format-standard","hentry","category-android-basic","tag-aes"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=956"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/956\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}