{"id":8180,"date":"2024-06-19T20:01:01","date_gmt":"2024-06-19T12:01:01","guid":{"rendered":""},"modified":"2024-06-19T20:01:01","modified_gmt":"2024-06-19T12:01:01","slug":"openssl aes\u52a0\u5bc6\u547d\u4ee4_java\u4f7f\u7528aes\u52a0\u5bc6\u89e3\u5bc6","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8180.html","title":{"rendered":"openssl aes\u52a0\u5bc6\u547d\u4ee4_java\u4f7f\u7528aes\u52a0\u5bc6\u89e3\u5bc6"},"content":{"rendered":"<p><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"display: none;\"> \n <path stroke-linecap=\"round\" d=\"M5,0 0,2.5 5,5z\" id=\"raphael-marker-block\" style=\"-webkit-tap-highlight-color: rgba(0, 0, 0, 0);\"><\/path> \n<\/svg> <\/p>\n<h3 id=\"\u53c2\u8003\u6587\u7ae0\">\u53c2\u8003\u6587\u7ae0<\/h3>\n<p>\u4f7f\u7528openssl\u8fdb\u884cAES 128 CTR \u52a0\u5bc6\u3001\u89e3\u5bc6<\/p>\n<h3 id=\"\u5bf9\u79f0\u52a0\u5bc6\u5982\u975e\u5bf9\u79f0\u52a0\u5bc6\">\u5bf9\u79f0\u52a0\u5bc6\u5982\u975e\u5bf9\u79f0\u52a0\u5bc6<\/h3>\n<p>AES128-CTR\u65b9\u5f0f\uff0c\u5c5e\u4e8e\u5bf9\u79f0\u52a0\u5bc6\u7684\u4e00\u79cd\u3002 <br \/> \u8fd9\u91cc\u6709\u4e00\u7bc7\u8bb2\u89e3\u5bf9\u79f0\u548c\u975e\u5bf9\u79f0\u52a0\u5bc6\u975e\u5e38\u901a\u4fd7\u6613\u61c2\u7684\u6587\u7ae0\uff1a <br \/> https:\/\/segmentfault.com\/a\/61428 <\/p>\n<h3 id=\"\u4ee3\u7801\u793a\u4f8b\">\u4ee3\u7801\u793a\u4f8b<\/h3>\n<pre class=\"prettyprint\"><code class=\" hljs cpp\"><span class=\"hljs-preprocessor\">#include &lt;stdlib.h&gt;<\/span> <span class=\"hljs-preprocessor\">#include &lt;stdio.h&gt;<\/span> <span class=\"hljs-preprocessor\">#include &lt;string.h&gt;<\/span> <span class=\"hljs-preprocessor\">#include &lt;openssl\/evp.h&gt;<\/span> <span class=\"hljs-keyword\">void<\/span> handleErrors(<span class=\"hljs-keyword\">void<\/span>) { ERR_print_errors_fp(stderr); <span class=\"hljs-built_in\">abort<\/span>(); } <span class=\"hljs-keyword\">int<\/span> encrypt(<span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> *plaintext, <span class=\"hljs-keyword\">int<\/span> plaintext_len, <span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> *key, <span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> *iv, <span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> *ciphertext) { EVP_CIPHER_CTX *ctx; <span class=\"hljs-keyword\">int<\/span> len; <span class=\"hljs-keyword\">int<\/span> ciphertext_len; <span class=\"hljs-comment\">\/* Create and initialise the context *\/<\/span> <span class=\"hljs-keyword\">if<\/span> (!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); <span class=\"hljs-comment\">\/* Initialise the encryption operation. IMPORTANT - ensure you use a key * and IV size appropriate for your cipher * In this example we are using 256 bit AES (i.e. a 256 bit key). The * IV size for *most* modes is the same as the block size. For AES this * is 128 bits *\/<\/span> <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-number\">1<\/span> != EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) handleErrors(); <span class=\"hljs-comment\">\/* Provide the message to be encrypted, and obtain the encrypted output. * EVP_EncryptUpdate can be called multiple times if necessary *\/<\/span> <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-number\">1<\/span> != EVP_EncryptUpdate(ctx, ciphertext, &amp;len, plaintext, plaintext_len)) handleErrors(); ciphertext_len = len; <span class=\"hljs-comment\">\/* Finalise the encryption. Further ciphertext bytes may be written at * this stage. *\/<\/span> <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-number\">1<\/span> != EVP_EncryptFinal_ex(ctx, ciphertext + len, &amp;len)) handleErrors(); ciphertext_len += len; <span class=\"hljs-comment\">\/* Clean up *\/<\/span> EVP_CIPHER_CTX_free(ctx); <span class=\"hljs-keyword\">return<\/span> ciphertext_len; } <span class=\"hljs-keyword\">int<\/span> decrypt(<span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> *ciphertext, <span class=\"hljs-keyword\">int<\/span> ciphertext_len, <span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> *key, <span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> *iv, <span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> *plaintext) { EVP_CIPHER_CTX *ctx; <span class=\"hljs-keyword\">int<\/span> len; <span class=\"hljs-keyword\">int<\/span> plaintext_len; <span class=\"hljs-comment\">\/* Create and initialise the context *\/<\/span> <span class=\"hljs-keyword\">if<\/span> (!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); <span class=\"hljs-comment\">\/* Initialise the decryption operation. IMPORTANT - ensure you use a key * and IV size appropriate for your cipher * In this example we are using 256 bit AES (i.e. a 256 bit key). The * IV size for *most* modes is the same as the block size. For AES this * is 128 bits *\/<\/span> <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-number\">1<\/span> != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) handleErrors(); <span class=\"hljs-comment\">\/* Provide the message to be decrypted, and obtain the plaintext output. * EVP_DecryptUpdate can be called multiple times if necessary *\/<\/span> <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-number\">1<\/span> != EVP_DecryptUpdate(ctx, plaintext, &amp;len, ciphertext, ciphertext_len)) handleErrors(); plaintext_len = len; <span class=\"hljs-comment\">\/* Finalise the decryption. Further plaintext bytes may be written at * this stage. *\/<\/span> <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-number\">1<\/span> != EVP_DecryptFinal_ex(ctx, plaintext + len, &amp;len)) handleErrors(); plaintext_len += len; <span class=\"hljs-comment\">\/* Clean up *\/<\/span> EVP_CIPHER_CTX_free(ctx); <span class=\"hljs-keyword\">return<\/span> plaintext_len; } <span class=\"hljs-keyword\">int<\/span> main(<span class=\"hljs-keyword\">int<\/span> argc, <span class=\"hljs-keyword\">char<\/span> <span class=\"hljs-keyword\">const<\/span> *argv[]) { <span class=\"hljs-comment\">\/* A 256 bit key *\/<\/span> <span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> *key = (<span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> *)<span class=\"hljs-string\">\"0\"<\/span>; <span class=\"hljs-comment\">\/* A 128 bit IV *\/<\/span> <span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> *iv = (<span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> *)<span class=\"hljs-string\">\"02345\"<\/span>; <span class=\"hljs-comment\">\/* Message to be encrypted *\/<\/span> <span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> *plaintext = <span class=\"hljs-string\">\"The quick brown fox jumps over the lazy dog\"<\/span>; <span class=\"hljs-comment\">\/* Buffer for ciphertext. Ensure the buffer is long enough for the * ciphertext which may be longer than the plaintext, dependant on the * algorithm and mode *\/<\/span> <span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> ciphertext[<span class=\"hljs-number\">128<\/span>]; <span class=\"hljs-comment\">\/* Buffer for the decrypted text *\/<\/span> <span class=\"hljs-keyword\">unsigned<\/span> <span class=\"hljs-keyword\">char<\/span> decryptedtext[<span class=\"hljs-number\">128<\/span>]; <span class=\"hljs-keyword\">int<\/span> decryptedtext_len, ciphertext_len; <span class=\"hljs-built_in\">printf<\/span>(<span class=\"hljs-string\">\"Plaintext is:\\n%s~\\n\"<\/span>, plaintext); <span class=\"hljs-comment\">\/* Encrypt the plaintext *\/<\/span> ciphertext_len = encrypt(plaintext, <span class=\"hljs-built_in\">strlen<\/span>(plaintext), key, iv, ciphertext); <span class=\"hljs-comment\">\/* Do something useful with the ciphertext here *\/<\/span> <span class=\"hljs-built_in\">printf<\/span>(<span class=\"hljs-string\">\"Ciphertext is %d bytes long:\\n\"<\/span>, ciphertext_len); BIO_dump_fp(stdout, ciphertext, ciphertext_len); <span class=\"hljs-comment\">\/* Decrypt the ciphertext *\/<\/span> decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext); <span class=\"hljs-comment\">\/* Add a NULL terminator. We are expecting printable text *\/<\/span> decryptedtext[decryptedtext_len] = <span class=\"hljs-string\">'\\0'<\/span>; <span class=\"hljs-comment\">\/* Show the decrypted text *\/<\/span> <span class=\"hljs-built_in\">printf<\/span>(<span class=\"hljs-string\">\"Decrypted text is:\\n\"<\/span>); <span class=\"hljs-built_in\">printf<\/span>(<span class=\"hljs-string\">\"%s~\\n\"<\/span>, decryptedtext); <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>; }<\/code><\/pre>\n<p>\u7f16\u8bd1<\/p>\n<pre class=\"prettyprint\"><code class=\" hljs lasso\">gcc openssl<span class=\"hljs-attribute\">-aes<\/span><span class=\"hljs-subst\">-<\/span><span class=\"hljs-number\">128<\/span><span class=\"hljs-attribute\">-ctr<\/span><span class=\"hljs-built_in\">.<\/span>c <span class=\"hljs-attribute\">-lssl<\/span> <span class=\"hljs-attribute\">-lcrypto<\/span><\/code><\/pre>\n<p>\u8fd0\u884c\u6548\u679c<\/p>\n<pre class=\"prettyprint\"><code class=\" hljs applescript\">Plaintext <span class=\"hljs-keyword\">is<\/span>: The quick brown fox jumps <span class=\"hljs-keyword\">over<\/span> <span class=\"hljs-keyword\">the<\/span> lazy dog~ Ciphertext <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-number\">43<\/span> bytes long: <span class=\"hljs-number\">0000<\/span> - <span class=\"hljs-number\">0<\/span>b <span class=\"hljs-number\">15<\/span> <span class=\"hljs-number\">95<\/span> <span class=\"hljs-number\">9<\/span>f <span class=\"hljs-number\">61<\/span> <span class=\"hljs-number\">4<\/span>f e5 <span class=\"hljs-number\">29<\/span>-<span class=\"hljs-number\">8<\/span>d da cf eb <span class=\"hljs-number\">69<\/span> db <span class=\"hljs-number\">55<\/span> <span class=\"hljs-number\">0<\/span>a ....aO.)....i.U. <span class=\"hljs-number\">0010<\/span> - <span class=\"hljs-number\">00<\/span> <span class=\"hljs-number\">58<\/span> <span class=\"hljs-number\">58<\/span> fa <span class=\"hljs-number\">7<\/span>c <span class=\"hljs-number\">6<\/span>f d0 <span class=\"hljs-number\">52<\/span>-<span class=\"hljs-number\">52<\/span> <span class=\"hljs-number\">53<\/span> <span class=\"hljs-number\">73<\/span> <span class=\"hljs-number\">61<\/span> <span class=\"hljs-number\">1<\/span>b <span class=\"hljs-number\">08<\/span> <span class=\"hljs-number\">2<\/span>e <span class=\"hljs-number\">03<\/span> .XX.|o.RRSsa.... <span class=\"hljs-number\">0020<\/span> - a2 d1 <span class=\"hljs-number\">16<\/span> <span class=\"hljs-number\">0<\/span>c <span class=\"hljs-number\">98<\/span> <span class=\"hljs-number\">4<\/span>b f5 c7-<span class=\"hljs-number\">43<\/span> <span class=\"hljs-number\">6<\/span>c ca .....K..Cl. Decrypted <span class=\"hljs-type\">text<\/span> <span class=\"hljs-keyword\">is<\/span>: The quick brown fox jumps <span class=\"hljs-keyword\">over<\/span> <span class=\"hljs-keyword\">the<\/span> lazy dog~<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"openssl aes\u52a0\u5bc6\u547d\u4ee4_java\u4f7f\u7528aes\u52a0\u5bc6\u89e3\u5bc6\u4ee3\u7801\u793a\u4f8b#include&amp;amp;lt;stdlib.h&amp;amp;gt;#include&amp;amp;lt;stdio.h&am...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8180"}],"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=8180"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8180\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}