{"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":"

\n <\/path> \n<\/svg> <\/p>\n

\u53c2\u8003\u6587\u7ae0<\/h3>\n

\u4f7f\u7528openssl\u8fdb\u884cAES 128 CTR \u52a0\u5bc6\u3001\u89e3\u5bc6<\/p>\n

\u5bf9\u79f0\u52a0\u5bc6\u5982\u975e\u5bf9\u79f0\u52a0\u5bc6<\/h3>\n

AES128-CTR\u65b9\u5f0f\uff0c\u5c5e\u4e8e\u5bf9\u79f0\u52a0\u5bc6\u7684\u4e00\u79cd\u3002
\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
https:\/\/segmentfault.com\/a\/61428 <\/p>\n

\u4ee3\u7801\u793a\u4f8b<\/h3>\n
#include <stdlib.h><\/span> #include <stdio.h><\/span> #include <string.h><\/span> #include <openssl\/evp.h><\/span> void<\/span> handleErrors(void<\/span>) { ERR_print_errors_fp(stderr); abort<\/span>(); } int<\/span> encrypt(unsigned<\/span> char<\/span> *plaintext, int<\/span> plaintext_len, unsigned<\/span> char<\/span> *key, unsigned<\/span> char<\/span> *iv, unsigned<\/span> char<\/span> *ciphertext) { EVP_CIPHER_CTX *ctx; int<\/span> len; int<\/span> ciphertext_len; \/* Create and initialise the context *\/<\/span> if<\/span> (!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); \/* 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> if<\/span> (1<\/span> != EVP_EncryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) handleErrors(); \/* Provide the message to be encrypted, and obtain the encrypted output. * EVP_EncryptUpdate can be called multiple times if necessary *\/<\/span> if<\/span> (1<\/span> != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) handleErrors(); ciphertext_len = len; \/* Finalise the encryption. Further ciphertext bytes may be written at * this stage. *\/<\/span> if<\/span> (1<\/span> != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors(); ciphertext_len += len; \/* Clean up *\/<\/span> EVP_CIPHER_CTX_free(ctx); return<\/span> ciphertext_len; } int<\/span> decrypt(unsigned<\/span> char<\/span> *ciphertext, int<\/span> ciphertext_len, unsigned<\/span> char<\/span> *key, unsigned<\/span> char<\/span> *iv, unsigned<\/span> char<\/span> *plaintext) { EVP_CIPHER_CTX *ctx; int<\/span> len; int<\/span> plaintext_len; \/* Create and initialise the context *\/<\/span> if<\/span> (!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); \/* 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> if<\/span> (1<\/span> != EVP_DecryptInit_ex(ctx, EVP_aes_128_ctr(), NULL, key, iv)) handleErrors(); \/* Provide the message to be decrypted, and obtain the plaintext output. * EVP_DecryptUpdate can be called multiple times if necessary *\/<\/span> if<\/span> (1<\/span> != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) handleErrors(); plaintext_len = len; \/* Finalise the decryption. Further plaintext bytes may be written at * this stage. *\/<\/span> if<\/span> (1<\/span> != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors(); plaintext_len += len; \/* Clean up *\/<\/span> EVP_CIPHER_CTX_free(ctx); return<\/span> plaintext_len; } int<\/span> main(int<\/span> argc, char<\/span> const<\/span> *argv[]) { \/* A 256 bit key *\/<\/span> unsigned<\/span> char<\/span> *key = (unsigned<\/span> char<\/span> *)\"0\"<\/span>; \/* A 128 bit IV *\/<\/span> unsigned<\/span> char<\/span> *iv = (unsigned<\/span> char<\/span> *)\"02345\"<\/span>; \/* Message to be encrypted *\/<\/span> unsigned<\/span> char<\/span> *plaintext = \"The quick brown fox jumps over the lazy dog\"<\/span>; \/* 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> unsigned<\/span> char<\/span> ciphertext[128<\/span>]; \/* Buffer for the decrypted text *\/<\/span> unsigned<\/span> char<\/span> decryptedtext[128<\/span>]; int<\/span> decryptedtext_len, ciphertext_len; printf<\/span>(\"Plaintext is:\\n%s~\\n\"<\/span>, plaintext); \/* Encrypt the plaintext *\/<\/span> ciphertext_len = encrypt(plaintext, strlen<\/span>(plaintext), key, iv, ciphertext); \/* Do something useful with the ciphertext here *\/<\/span> printf<\/span>(\"Ciphertext is %d bytes long:\\n\"<\/span>, ciphertext_len); BIO_dump_fp(stdout, ciphertext, ciphertext_len); \/* Decrypt the ciphertext *\/<\/span> decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext); \/* Add a NULL terminator. We are expecting printable text *\/<\/span> decryptedtext[decryptedtext_len] = '\\0'<\/span>; \/* Show the decrypted text *\/<\/span> printf<\/span>(\"Decrypted text is:\\n\"<\/span>); printf<\/span>(\"%s~\\n\"<\/span>, decryptedtext); return<\/span> 0<\/span>; }<\/code><\/pre>\n

\u7f16\u8bd1<\/p>\n

gcc openssl-aes<\/span>-<\/span>128<\/span>-ctr<\/span>.<\/span>c -lssl<\/span> -lcrypto<\/span><\/code><\/pre>\n

\u8fd0\u884c\u6548\u679c<\/p>\n

Plaintext is<\/span>: The quick brown fox jumps over<\/span> the<\/span> lazy dog~ Ciphertext is<\/span> 43<\/span> bytes long: 0000<\/span> - 0<\/span>b 15<\/span> 95<\/span> 9<\/span>f 61<\/span> 4<\/span>f e5 29<\/span>-8<\/span>d da cf eb 69<\/span> db 55<\/span> 0<\/span>a ....aO.)....i.U. 0010<\/span> - 00<\/span> 58<\/span> 58<\/span> fa 7<\/span>c 6<\/span>f d0 52<\/span>-52<\/span> 53<\/span> 73<\/span> 61<\/span> 1<\/span>b 08<\/span> 2<\/span>e 03<\/span> .XX.|o.RRSsa.... 0020<\/span> - a2 d1 16<\/span> 0<\/span>c 98<\/span> 4<\/span>b f5 c7-43<\/span> 6<\/span>c ca .....K..Cl. Decrypted text<\/span> is<\/span>: The quick brown fox jumps over<\/span> 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;lt;stdlib.h&amp;gt;#include&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}]}}