C或C++程序:AES 算法程序

C或C++程序:AES 算法程序,能直接运行的,求小伙伴们帮助!

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
C或C++程序:AES 算法程序

C或C++程序:AES 算法程序,能直接运行的,求小伙伴们帮助!

使用AES算法加密任意磁盘文件,要求如下

实现一个命令行程序,加密任意的磁盘文件。要求如下: 1、使用AES算法; 2、加密的key自动随机生成,并存在一个文件中; 3、该程序可以进行加密、解密; 4、加密模式可选,包括CTR、CBC模式等。 5、底层算法不能调库,应自己实现。

Shiro认证产生的 java.security.InvalidKeyException: Invalid AES key length: 58 bytes

这个错误是咋回事呢? org.apache.shiro.crypto.CryptoException: Unable to init cipher instance. at org.apache.shiro.crypto.JcaCipherService.init(JcaCipherService.java:495) ~[shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.crypto.JcaCipherService.initNewCipher(JcaCipherService.java:598) ~[shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.crypto.JcaCipherService.crypt(JcaCipherService.java:444) ~[shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.crypto.JcaCipherService.encrypt(JcaCipherService.java:324) ~[shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.crypto.JcaCipherService.encrypt(JcaCipherService.java:313) ~[shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.mgt.AbstractRememberMeManager.encrypt(AbstractRememberMeManager.java:473) ~[shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.mgt.AbstractRememberMeManager.convertPrincipalsToBytes(AbstractRememberMeManager.java:362) ~[shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.mgt.AbstractRememberMeManager.rememberIdentity(AbstractRememberMeManager.java:346) ~[shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.mgt.AbstractRememberMeManager.rememberIdentity(AbstractRememberMeManager.java:321) ~[shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.mgt.AbstractRememberMeManager.onSuccessfulLogin(AbstractRememberMeManager.java:297) ~[shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.mgt.DefaultSecurityManager.rememberMeSuccessfulLogin(DefaultSecurityManager.java:206) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.mgt.DefaultSecurityManager.onSuccessfulLogin(DefaultSecurityManager.java:291) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:285) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:256) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.web.filter.authc.AuthenticatingFilter.executeLogin(AuthenticatingFilter.java:53) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.web.filter.authc.FormAuthenticationFilter.onAccessDenied(FormAuthenticationFilter.java:154) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.web.filter.AccessControlFilter.onAccessDenied(AccessControlFilter.java:133) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.web.filter.AccessControlFilter.onPreHandle(AccessControlFilter.java:162) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.web.filter.PathMatchingFilter.isFilterChainContinued(PathMatchingFilter.java:203) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.web.filter.PathMatchingFilter.preHandle(PathMatchingFilter.java:178) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:131) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) [shiro-all-1.2.3.jar:1.2.3] at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) [shiro-all-1.2.3.jar:1.2.3] at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343) [spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260) [spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.53] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.53] at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:180) [spring-orm-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.53] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.53] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) [spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-3.2.8.RELEASE.jar:3.2.8.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.53] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.53] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) [catalina.jar:7.0.53] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) [catalina.jar:7.0.53] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) [catalina.jar:7.0.53] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) [catalina.jar:7.0.53] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) [catalina.jar:7.0.53] at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) [catalina.jar:7.0.53] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) [catalina.jar:7.0.53] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) [catalina.jar:7.0.53] at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040) [tomcat-coyote.jar:7.0.53] at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607) [tomcat-coyote.jar:7.0.53] at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441) [tomcat-coyote.jar:7.0.53] at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430) [tomcat-coyote.jar:7.0.53] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_51] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_51] at java.lang.Thread.run(Thread.java:744) [na:1.7.0_51] Caused by: java.security.InvalidKeyException: Invalid AES key length: 58 bytes at com.sun.crypto.provider.AESCipher.engineGetKeySize(AESCipher.java:372) ~[sunjce_provider.jar:1.7.0_51] at javax.crypto.Cipher.passCryptoPermCheck(Cipher.java:1052) ~[na:1.7.0_51] at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1023) ~[na:1.7.0_51] at javax.crypto.Cipher.implInit(Cipher.java:790) ~[na:1.7.0_51] at javax.crypto.Cipher.chooseProvider(Cipher.java:849) ~[na:1.7.0_51] at javax.crypto.Cipher.init(Cipher.java:1348) ~[na:1.7.0_51] at javax.crypto.Cipher.init(Cipher.java:1282) ~[na:1.7.0_51] at org.apache.shiro.crypto.JcaCipherService.init(JcaCipherService.java:488) ~[shiro-all-1.2.3.jar:1.2.3] ... 56 common frames omitted

C语言AES求大佬帮助?

只有五天时间来写这个题目,然而我非常菜,已经写不完了,恳求大佬帮助一下! ![图片说明](https://img-ask.csdn.net/upload/201910/08/1570503767_829527.png) ![图片说明](https://img-ask.csdn.net/upload/201910/08/1570503778_610408.png) 万分感谢!

AES算法java字节替代的代码应如何理解?

AES算法的java实现中的字节替代代码,没太看懂,请大佬赐教。 ![图片说明](https://img-ask.csdn.net/upload/201810/12/1539325077_37480.jpg)

关于AES算法秘钥问题!!!!!!!!!

![图片说明](https://img-ask.csdn.net/upload/201612/28/1482934310_369927.gif) 这个算法的先后顺序,下面这段数字是秘钥么,是秘钥的明文该怎么解,如果不是秘钥秘钥在哪?

c++ 调用openssl库AES_cbc_encrypt加解密padding解密如何去除

与服务器http通信。加密:AES_set_encrypt_key(AesKey, 128, &aes); AES_cbc_encrypt(input_string, encrypt_string, len, &aes, AesIvp, AES_ENCRYPT); 过去的字段后面添加的padding补全没问题。服务器返回的密文解密: AES_set_decrypt_key(AesKey, 128, &aes); AES_cbc_encrypt((unsigned char*)Dncrypt_string, (unsigned char*)encrypt_string, nlen, &aes, AesIvp, AES_DECRYPT); 解密出来的后面会多出padding乱码。请问哪位大神知道如何安全的去除解密后的padding。

AES加密算法是否有些安卓手机不支持

我写的一个程序用到AES加密,每次就会在有些安卓机型上崩溃,报的什么找不到物理地址错误,但换个加密算法就好了,所以有些安卓手机是不支持AES加密的吗?

springmvc整合shiro框架时无法获取AES算法是什么原因?

1. 启动项目失败,主要报的错误是这个:java.lang.IllegalStateException: Unable to acquire AES algorithm. This is required to function.前面还有不合适的依赖无法识别,bean对象无法创建等错误 2. 尝试过重装jdk,依赖重新注入. 3. 注释掉相关shiro的代码就运行正常了.

(新人求助各位大佬)网络安全:已知明文和主密钥,依据AES算法求解问题

新人初来乍到不懂规矩,如有冒犯真的不是故意的,告诉我后我会改正的。 选修了网络安全的课,作为一个非专业的人真的不知道怎么学,老师出的这个题在网上实在是找不到答案了,来这里求助各位大佬。。ps:题目在上传的图片里![图片说明](https://img-ask.csdn.net/upload/201812/05/1544013258_546692.jpg)

Crytopp使用时发现的问题

我参考网上的资料配置了Crytopp。 .pro文件中我添加了如下内容 INCLUDEPATH += "$$PWD/crytopp820/include" LIBS += -L"$$PWD/crytopp820/lib" -lcryptlib 在工程文件夹下面的字文件夹中lib中有名为cryptlib.lib的动态链接库。 执行下面的程序 qDebug()<<"key length:"<<CryptoPP::AES::DEFAULT_KEYLENGTH; qDebug()<<"key min length:"<<CryptoPP::AES::MIN_KEYLENGTH; qDebug()<<"key max length:"<<CryptoPP::AES::MAX_KEYLENGTH; qDebug()<<"block size:"<<CryptoPP::AES::BLOCKSIZE; 显示的结果: key length: 16 key min length: 16 key max length: 32 block size: 16 毫无疑问很正确,说明Crytopp已经可以用了。 然后我又加了一句:CryptoPP::MD5 md5; 这下坏了,报了很多错误。![图片说明](https://img-ask.csdn.net/upload/201910/07/1570427305_328960.jpg) 我就不明白了,为什么我的Crytopp都可以用了,这里却弹出这些错误?该如何解决?

使用Java和PHP的AES CBC 128位加密

<div class="post-text" itemprop="text"> <p>I have recently used the AES CBC 128 algorithm in Java in order to cipher data. Now I need to rebuild that algorithm in PHP, but I have no idea how, because PHP algorithms on the internet return different results. Maybe you can help me. </p> <p>This is the Java-code to encrypt:</p> <pre><code>private SecretKeySpec secretKey; private IvParameterSpec ivSpec; public void setKey(String myKey) { MessageDigest sha = null; try { byte[] key = myKey.getBytes("UTF-8"); sha = MessageDigest.getInstance("SHA-1"); key = sha.digest(key); key = Arrays.copyOf(key, 16); secretKey = new SecretKeySpec(key, "AES"); byte[] iv = new String("1010101010101010").getBytes("UTF-8"); ivSpec = new IvParameterSpec(iv); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } public String encrypt(String strToEncrypt) { try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec); return Base64.encode(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))); } catch (Exception e) { e.printStackTrace(); } return null; } public String decrypt(String strToDecrypt) { try { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec); return new String(cipher.doFinal(Base64.decode(strToDecrypt))); } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) { AESText aes = new AESText(); final String secretKey = "com.secure.test.projectjasdS/FjkGkGhkGjhG786Vjfg=tjGFGH"; aes.setKey(secretKey); String originalString = "test set se ts et set s et se"; String encryptedString = aes.encrypt(originalString); String decryptedString = aes.decrypt(encryptedString); System.out.println("origin: " + originalString); System.out.println("encrypted: " + encryptedString); System.out.println("decrypted: " + decryptedString); } </code></pre> <p>This is my php code:</p> <pre><code> protected $key; protected $method = 'AES-128-CBC'; protected $iv = '1010101010101010'; protected $option = OPENSSL_CIPHER_AES_128_CBC; function __construct($key) { $this-&gt;key = $key; } public function encrypt($data) { if (is_null($data)) { return "Error " . INVALID_PARAMS_ENCRYPTIONS . ": Data is null "; } $enc = openssl_encrypt($data, $this-&gt;method, $this-&gt;key, $this-&gt;option, $this-&gt;iv); return base64_encode($enc); } public function decrypt($data) { if (is_null($data)) { return "Error " . INVALID_PARAMS_ENCRYPTIONS . ": Data is null "; } $data = base64_decode($data); $dec = openssl_decrypt($data, $this-&gt;method, $this-&gt;key, $this-&gt;option, $this-&gt;iv); return $dec; } </code></pre> <p>When I encrypted data from java encryption, This result cannot decrypt on Php decryption.</p> <p>Can you guys possibly help me with building a PHP script, that returns the same results with java encryption?</p> </div>

Java实现AES256位对称加密算法要替换什么包才能实现

Java语言实现AES256对称加密算法要替换掉什么包才能实现? 官方文档: http://java.sun.com/developer/technicalArticles/Security/AES/AES_v1.html 希望能得到各位朋友支援下

PHP AES 128位加密/解密

<div class="post-text" itemprop="text"> <p>I'm trying to understand how to AES encrypt a piece of text(16 bytes - 128 bits). This code is from <a href="http://php.net/manual/ro/function.openssl-encrypt.php" rel="nofollow noreferrer">php manual</a>:</p> <pre><code>$key = openssl_random_pseudo_bytes(32); $plaintext = "message to be encrypted"; $cipher = "aes-128-gcm"; if (in_array($cipher, openssl_get_cipher_methods())) { $ivlen = openssl_cipher_iv_length($cipher); $iv = openssl_random_pseudo_bytes($ivlen); $ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv, $tag); //store $cipher, $iv, and $tag for decryption later $original_plaintext = openssl_decrypt($ciphertext, $cipher, $key, $options=0, $iv, $tag); echo $original_plaintext." "; } </code></pre> <p>The only problem with this is that i don't really understand it. <code>$cipher</code> is <code>aes-128-gcm</code> but i'm getting a 32 bytes encryption. </p> <p>So my questions are: Can somebody help me understand how it actually works?</p> <p>Is it possible to make it 16 bytes/128 bites?</p> <p>And is it safe to store <code>$cipher</code>, <code>$iv</code>, <code>$key</code> and <code>$tag</code> into a MySQL database for later use? </p> <p>P.S: if i change <code>$key</code> length to <code>16</code> instead of <code>32</code> the final output of <code>$ciphertext</code> is still 32 bytes.</p> <p>Thank you!</p> </div>

利用C语言设计简单的文件加密解密程序

利用C语言设计简单的文件加密解密程序,并通过代码实现,希望得到源代码。

有全部源码,我调用openssl库的RSA和AES算法进行加密。加密字符串太长就会报错,具体如下。

报错信息是这样的: Error encrypting message: error:0406D06E:rsa routines:RSA_padding_add_PKCS1_type_2:data too large for key size Error decrypting message: error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is not 02 我用的Linux环境进行开发的,不过也应该没有太大关系啦! 另外代码上有什么问题也可以指出来,万分感谢!!! 具体代码 如下: ``` #include <openssl/rsa.h> #include <openssl/sha.h> #include <openssl/ssl.h> #include <openssl/aes.h> #include <openssl/evp.h> #include <openssl/pem.h> #include <openssl/err.h> #include <assert.h> #include <iostream> #include <string.h> #include <stdio.h> #include <string> #include <stdlib.h> #define RSA_KEYLEN 2048 #define AES_KEYLEN 128 #define AES_ROUNDS 6 #define KEY_SERVER_PRI 0 #define KEY_SERVER_PUB 1 #define KEY_CLIENT_PUB 2 #define KEY_AES 3 #define KEY_AES_IV 4 #define KEY_LENGTH 2048 #define PUB_EXP 3 //用AES进行加密 int EncodeAES(unsigned char* password,unsigned int pass_len, unsigned char* data,unsigned int data_len,unsigned char** out_data,unsigned int *out_len) { AES_KEY aes_key; if(AES_set_encrypt_key((const unsigned char*)password, 192, &aes_key) < 0) { assert(false); return -1; } std::string strRet; std::string data_bak((const char*)data); unsigned int data_length = data_len; int padding = 0; if (data_len % AES_BLOCK_SIZE > 0) { padding = AES_BLOCK_SIZE - data_len % AES_BLOCK_SIZE; } data_length += padding; while (padding > 0) { data_bak += '\0'; padding--; } for(unsigned int i = 0; i < data_length/AES_BLOCK_SIZE; i++) { std::string str16 = data_bak.substr(i*AES_BLOCK_SIZE, AES_BLOCK_SIZE); unsigned char out[AES_BLOCK_SIZE]; ::memset(out, 0, AES_BLOCK_SIZE); AES_encrypt((const unsigned char*)str16.c_str(), out, &aes_key); strRet += std::string((const char*)out, AES_BLOCK_SIZE); } *out_len=strRet.length(); *out_data=(unsigned char*)malloc(*out_len); *out_data=(unsigned char*)strRet.c_str(); return 0; } //用aes进行解密 int DecodeAES(unsigned char *password,unsigned int pass_len, unsigned char* data,unsigned int data_len,unsigned char** out_data,unsigned int *out_len ) { AES_KEY aes_key; if(AES_set_decrypt_key((const unsigned char*)password, 192, &aes_key) < 0) { assert(false); return -1; } std::string strRet; std::string strData((const char*)data); try { for(unsigned int i = 0; i < data_len/AES_BLOCK_SIZE; i++) { std::string str16 = strData.substr(i*AES_BLOCK_SIZE, AES_BLOCK_SIZE); unsigned char out[AES_BLOCK_SIZE]; ::memset(out, 0, AES_BLOCK_SIZE); AES_decrypt((const unsigned char*)str16.c_str(), out, &aes_key); strRet += std::string((const char*)out, AES_BLOCK_SIZE); } } catch (std::exception const &exc) { std::cerr << "Exception caught " << exc.what() << "\n"; } catch (...) { std::cerr << "Unknown exception caught\n"; } *out_len=strRet.length(); *out_data=(unsigned char*)malloc(*out_len); *out_data=(unsigned char*)strRet.c_str(); return 0; } //生成RSA需要的公钥和私钥 int generateRSAKeys(char ** pri_key, char ** pub_key) { RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL); // To get the C-string PEM form: BIO *pri = BIO_new(BIO_s_mem()); BIO *pub = BIO_new(BIO_s_mem()); PEM_write_bio_RSAPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL); PEM_write_bio_RSAPublicKey(pub, keypair); size_t pri_len = BIO_pending(pri); size_t pub_len = BIO_pending(pub); char* prikey = (char*)malloc(pri_len + 1); char* pubkey = (char*)malloc(pub_len + 1); if (prikey == NULL&&pubkey == NULL) { return -1; } BIO_read(pri, prikey, pri_len); BIO_read(pub, pubkey, pub_len); *pri_key = prikey; *pub_key = pubkey; RSA_free(keypair); BIO_free_all(pri); BIO_free_all(pub); return 0; } //用RSA公钥进行加密 int encryptWithPub(char *pub_key,char* msg, char **encrypt, int* encrypt_len ) { RSA *rsa = NULL; BIO* keybio = BIO_new_mem_buf((void *)pub_key, -1); char *err = (char*)malloc(130); if (keybio == NULL) { printf("failed to create key BIO\n"); return -1; } #ifdef RSAPUBKEY if ((rsa = PEM_read_bio_RSA_PUBKEY(keybio, NULL, NULL, NULL)) == NULL) #else if ((rsa = PEM_read_bio_RSAPublicKey(keybio, NULL, NULL, NULL)) == NULL) #endif if (!rsa){ printf(" rsa is null %s\n", ERR_error_string(ERR_get_error(), NULL)); return -1; } *encrypt_len = RSA_size(rsa); *encrypt = (char *)malloc(1024); if ((RSA_public_encrypt(strlen(msg) + 1, (unsigned char*)msg, (unsigned char*)*encrypt,rsa, RSA_PKCS1_PADDING)) == -1) { ERR_load_crypto_strings(); ERR_error_string(ERR_get_error(), err); fprintf(stderr, "Error encrypting message: %s\n", err); return -1; } RSA_free(rsa); free(err); BIO_free_all(keybio); // *encrypt_len=enstr.length(); return 0; } //用RSA私钥进行解密 int decryptWtihPri(char *pri_key,char* msg,char **decrypt, int encrypt_len){ RSA *rsa = NULL; BIO* keybio = BIO_new_mem_buf(pri_key, -1); if (keybio == NULL) { printf("failed to create key BIO\n"); return -1; } #ifdef RSAPRIVATE if ((rsa = PEM_read_bio_RSA_PRIVATE(keybio, NULL, NULL, NULL)) == NULL) #else if ((rsa = PEM_read_bio_RSAPrivateKey(keybio, NULL, NULL, NULL)) == NULL) #endif if (!rsa){ printf(" rsa null %s\n", ERR_error_string(ERR_get_error(), NULL)); return -1; } char *err = (char*)malloc(130); *decrypt = (char*)malloc(encrypt_len); if (RSA_private_decrypt(encrypt_len, (unsigned char*)msg, (unsigned char*)*decrypt, rsa, RSA_PKCS1_PADDING) == -1) { ERR_load_crypto_strings(); ERR_error_string(ERR_get_error(), err); fprintf(stderr, "Error decrypting message: %s\n", err); return -1; } RSA_free(rsa); free(err); BIO_free_all(keybio); return 0; } /** *测试函数部分 */ int test_aes(); int test_rsa(); int main() { test_rsa(); test_aes(); return 0; } int test_rsa() { printf("\n----------THE RSA TESTING -------- \n"); char * pri_key; char * pub_key; generateRSAKeys(&pri_key, &pub_key); //printf("\n%s\n%s\n", pri_key, pub_key); char *encrypt; char *decrypt; int encrypt_length; encryptWithPub(pub_key,(char *)"i havei have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a key a key",&encrypt, &encrypt_length); decryptWtihPri( pri_key,encrypt, &decrypt,encrypt_length); printf("decrypted message: %s\n", decrypt); return 0; } int test_aes() { printf("\n----------THE AES TESTING --------\n"); char* pass=(char*)"key"; int ret; char* data=(char*)"infocoei have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a key ei have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a key re big tree "; char *output=NULL; unsigned int out_len; char *newoutput=NULL; unsigned int new_len; std::cout<<"the length of key:"<<strlen(pass)<<std::endl; std::cout<<"before encryption:"<<data<<std::endl; ret=EncodeAES((unsigned char*)pass, strlen(pass),(unsigned char *)data,strlen(data),(unsigned char **)&output, &out_len); //std::cout<<"the en len:"<<out_len<<std::endl; ret=DecodeAES((unsigned char*)pass, strlen(pass),(unsigned char *)output,out_len,(unsigned char **)&newoutput, &new_len); //std::cout<<"the deenc len:"<<new_len<<std::endl; std::cout<<"after decryption:"<<newoutput<<std::endl; } ``` 代码的运行结果如下: ``` ----------THE RSA TESTING -------- Error encrypting message: error:0406D06E:rsa routines:RSA_padding_add_PKCS1_type_2:data too large for key size Error decrypting message: error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is not 02 decrypted message: 惙l= ----------THE AES TESTING -------- the length of key:3 before encryption:infocoei have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a key ei have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a keyi have a key re big tree Exception caught basic_string::substr after decryption:Ϝż§§<¾yK*. [root@localhost boost_encryption]# ``` 我的邮箱是fubohuauser@163.com 有啥问题也可以一起联系交流。谢谢

关于AES加密算法的问题

AES加密算法使用时,必须提供密码么,有没有无需密码的,直接解密的。我对这个不太懂,求教

64位编译的AES256加密算法,32位编译的能解密吗

采用加密算法AES256(libtomcrypt),加密报文的应用程序采用的64位编译的, 解密报文的应用程序是32位,结果解密不了,是AES256针对不同系统,32位和64位编译有啥区别吗

AES加密后,登录提示密码长度错误,请大神帮帮忙,谢谢!

1.先用md5加密: ``` public static String getMd5(String s) { char hexChar[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8' , '9', 'a', 'b', 'c', 'd', 'e', 'f','A', 'B', 'C', 'D', 'E', 'F'}; //md5加密算法的加密对象为字符数组,这里是为了得到加密的对象 byte[] b = s.getBytes(); try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(b); byte[] b2 = md.digest();// 进行加密并返回字符数组 char str[] = new char[b2.length << 1]; int len = 0; //将字符数组转换成十六进制串,形成最终的密文 for (int i = 0; i < b2.length; i++) { byte val = b2[i]; str[len++] = hexChar[(val >>> 4) & 0xf]; str[len++] = hexChar[val & 0xf]; } return new String(str); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } ``` 2.再用AES加密, ``` /** * AES 加密 * 密钥是apiSecret 的前16位 * 向量是apiSecret 的后16位 * @param password * @param apiSecret * @return * @throws Exception */ public static String getAesEncrypt(String password,String apiSecret)throws Exception{ String key = apiSecret.substring(0,16); String iv = apiSecret.substring(16); String md5Pass = getMd5(password); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes("UTF-8")); //System.out.println("密码的长度为:"+keyspec.getEncoded().length); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(md5Pass.getBytes("UTF-8")); return bytesToHexString(encrypted); } public static String bytesToHexString(byte[] bs) { StringBuffer sb = new StringBuffer(); String hex = ""; for (int i = 0; i < bs.length; i++) { hex = Integer.toHexString(bs[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex); } System.out.println("结果为:"+sb); return sb.toString(); } ``` 结果就提示:登录失败;加密后的密码长度错误 ![图片说明](https://img-ask.csdn.net/upload/201904/23/1556001375_850501.jpg)

pythonAES加密使用CBC模式与JAVA的CBC加密结果不同!求解!

JAVA的AES加密示例 import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.util.Base64; public class AesUtil { private static final String KEY_ALGORITHM = "AES"; private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding"; public static String encrypt(String strKey, String strIn) throws Exception { SecretKeySpec skeySpec = getKey(strKey); Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); IvParameterSpec iv = new IvParameterSpec(strKey.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(strIn.getBytes()); String aesResult = new String(Base64.getEncoder().encode(encrypted), StandardCharsets.UTF_8); return aesResult.replace("/", "_"); } private static SecretKeySpec getKey(String strKey) throws Exception { byte[] arrBTmp = strKey.getBytes(); byte[] arrB = new byte[16]; for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) { arrB[i] = arrBTmp[i]; } return new SecretKeySpec(arrB, KEY_ALGORITHM); } } 这个是我用python写的AES加密 from Crypto.Cipher import AES import base64 from time import sleep class aescrypt: def __init__(self, key, model, iv, encode_): self.encode_ = encode_ self.model = {'ECB': AES.MODE_ECB, 'CBC': AES.MODE_CBC}[model] self.key = self.add_16(key) if model == 'ECB': self.aes = AES.new(self.key, self.model) # 创建一个aes对象 elif model == 'CBC': self.aes = AES.new(self.key, self.model, iv, segment_size=128) # 创建一个aes对象 elif model == 'CFB': self.aes = AES.new(self.key, self.model) # 这里的密钥长度必须是16、24或32 def add_16(self, par): par = par.encode(self.encode_) while len(par) % 16 != 0: par += b'n' return par def aesencrypt(self, text): # 此处text传入的值为cleartext text = self.add_16(text) print(text) self.encrypt_text = self.aes.encrypt(text) return base64.encodebytes(self.encrypt_text).decode().strip() def aesdecrypt(self, text): text = base64.decodebytes(text.encode(self.encode_)) self.decrypt_text = self.aes.decrypt(text) return self.decrypt_text.decode(self.encode_).strip('\0') if __name__ == '__main__': keyy = input("输入密钥:") cleartext = input("输入明文:") pr = aescrypt(keyy, 'CBC', IV, 'utf8'),此处为加密模式及内容,IV en_text = pr.aesencrypt(cleartext) print('密文:', en_text) print('明文:', pr.aesdecrypt(en_text)) 这里我写了两个加密模式,上述代码已经换成同样的CBC模式加密

在中国程序员是青春饭吗?

今年,我也32了 ,为了不给大家误导,咨询了猎头、圈内好友,以及年过35岁的几位老程序员……舍了老脸去揭人家伤疤……希望能给大家以帮助,记得帮我点赞哦。 目录: 你以为的人生 一次又一次的伤害 猎头界的真相 如何应对互联网行业的「中年危机」 一、你以为的人生 刚入行时,拿着傲人的工资,想着好好干,以为我们的人生是这样的: 等真到了那一天,你会发现,你的人生很可能是这样的: ...

程序员请照顾好自己,周末病魔差点一套带走我。

程序员在一个周末的时间,得了重病,差点当场去世,还好及时挽救回来了。

和黑客斗争的 6 天!

互联网公司工作,很难避免不和黑客们打交道,我呆过的两家互联网公司,几乎每月每天每分钟都有黑客在公司网站上扫描。有的是寻找 Sql 注入的缺口,有的是寻找线上服务器可能存在的漏洞,大部分都...

点沙成金:英特尔芯片制造全过程揭密

“亚马逊丛林里的蝴蝶扇动几下翅膀就可能引起两周后美国德州的一次飓风……” 这句人人皆知的话最初用来描述非线性系统中微小参数的变化所引起的系统极大变化。 而在更长的时间尺度内,我们所生活的这个世界就是这样一个异常复杂的非线性系统…… 水泥、穹顶、透视——关于时间与技艺的蝴蝶效应 公元前3000年,古埃及人将尼罗河中挖出的泥浆与纳特龙盐湖中的矿物盐混合,再掺入煅烧石灰石制成的石灰,由此得来了人...

上班一个月,后悔当初着急入职的选择了

最近有个老铁,告诉我说,上班一个月,后悔当初着急入职现在公司了。他之前在美图做手机研发,今年美图那边今年也有一波组织优化调整,他是其中一个,在协商离职后,当时捉急找工作上班,因为有房贷供着,不能没有收入来源。所以匆忙选了一家公司,实际上是一个大型外包公司,主要派遣给其他手机厂商做外包项目。**当时承诺待遇还不错,所以就立马入职去上班了。但是后面入职后,发现薪酬待遇这块并不是HR所说那样,那个HR自...

女程序员,为什么比男程序员少???

昨天看到一档综艺节目,讨论了两个话题:(1)中国学生的数学成绩,平均下来看,会比国外好?为什么?(2)男生的数学成绩,平均下来看,会比女生好?为什么?同时,我又联想到了一个技术圈经常讨...

副业收入是我做程序媛的3倍,工作外的B面人生是怎样的?

提到“程序员”,多数人脑海里首先想到的大约是:为人木讷、薪水超高、工作枯燥…… 然而,当离开工作岗位,撕去层层标签,脱下“程序员”这身外套,有的人生动又有趣,马上展现出了完全不同的A/B面人生! 不论是简单的爱好,还是正经的副业,他们都干得同样出色。偶尔,还能和程序员的特质结合,产生奇妙的“化学反应”。 @Charlotte:平日素颜示人,周末美妆博主 大家都以为程序媛也个个不修边幅,但我们也许...

如果你是老板,你会不会踢了这样的员工?

有个好朋友ZS,是技术总监,昨天问我:“有一个老下属,跟了我很多年,做事勤勤恳恳,主动性也很好。但随着公司的发展,他的进步速度,跟不上团队的步伐了,有点...

我入职阿里后,才知道原来简历这么写

私下里,有不少读者问我:“二哥,如何才能写出一份专业的技术简历呢?我总感觉自己写的简历太烂了,所以投了无数份,都石沉大海了。”说实话,我自己好多年没有写过简历了,但我认识的一个同行,他在阿里,给我说了一些他当年写简历的方法论,我感觉太牛逼了,实在是忍不住,就分享了出来,希望能够帮助到你。 01、简历的本质 作为简历的撰写者,你必须要搞清楚一点,简历的本质是什么,它就是为了来销售你的价值主张的。往深...

外包程序员的幸福生活

今天给你们讲述一个外包程序员的幸福生活。男主是Z哥,不是在外包公司上班的那种,是一名自由职业者,接外包项目自己干。接下来讲的都是真人真事。 先给大家介绍一下男主,Z哥,老程序员,是我十多年前的老同事,技术大牛,当过CTO,也创过业。因为我俩都爱好喝酒、踢球,再加上住的距离不算远,所以一直也断断续续的联系着,我对Z哥的状况也有大概了解。 Z哥几年前创业失败,后来他开始干起了外包,利用自己的技术能...

C++11:一些微小的变化(新的数据类型、template表达式内的空格、nullptr、std::nullptr_t)

本文介绍一些C++的两个新特性,它们虽然微小,但对你的编程十分重要 一、Template表达式内的空格 C++11标准之前建议在“在两个template表达式的闭符之间放一个空格”的要求已经过时了 例如: vector&lt;list&lt;int&gt; &gt;; //C++11之前 vector&lt;list&lt;int&gt;&gt;; //C++11 二、nullptr ...

优雅的替换if-else语句

场景 日常开发,if-else语句写的不少吧??当逻辑分支非常多的时候,if-else套了一层又一层,虽然业务功能倒是实现了,但是看起来是真的很不优雅,尤其是对于我这种有强迫症的程序"猿",看到这么多if-else,脑袋瓜子就嗡嗡的,总想着解锁新姿势:干掉过多的if-else!!!本文将介绍三板斧手段: 优先判断条件,条件不满足的,逻辑及时中断返回; 采用策略模式+工厂模式; 结合注解,锦...

深入剖析Springboot启动原理的底层源码,再也不怕面试官问了!

大家现在应该都对Springboot很熟悉,但是你对他的启动原理了解吗?

离职半年了,老东家又发 offer,回不回?

有小伙伴问松哥这个问题,他在上海某公司,在离职了几个月后,前公司的领导联系到他,希望他能够返聘回去,他很纠结要不要回去? 俗话说好马不吃回头草,但是这个小伙伴既然感到纠结了,我觉得至少说明了两个问题:1.曾经的公司还不错;2.现在的日子也不是很如意。否则应该就不会纠结了。 老实说,松哥之前也有过类似的经历,今天就来和小伙伴们聊聊回头草到底吃不吃。 首先一个基本观点,就是离职了也没必要和老东家弄的苦...

为什么你不想学习?只想玩?人是如何一步一步废掉的

不知道是不是只有我这样子,还是你们也有过类似的经历。 上学的时候总有很多光辉历史,学年名列前茅,或者单科目大佬,但是虽然慢慢地长大了,你开始懈怠了,开始废掉了。。。 什么?你说不知道具体的情况是怎么样的? 我来告诉你: 你常常潜意识里或者心理觉得,自己真正的生活或者奋斗还没有开始。总是幻想着自己还拥有大把时间,还有无限的可能,自己还能逆风翻盘,只不是自己还没开始罢了,自己以后肯定会变得特别厉害...

为什么程序员做外包会被瞧不起?

二哥,有个事想询问下您的意见,您觉得应届生值得去外包吗?公司虽然挺大的,中xx,但待遇感觉挺低,马上要报到,挺纠结的。

当HR压你价,说你只值7K,你该怎么回答?

当HR压你价,说你只值7K时,你可以流畅地回答,记住,是流畅,不能犹豫。 礼貌地说:“7K是吗?了解了。嗯~其实我对贵司的面试官印象很好。只不过,现在我的手头上已经有一份11K的offer。来面试,主要也是自己对贵司挺有兴趣的,所以过来看看……”(未完) 这段话主要是陪HR互诈的同时,从公司兴趣,公司职员印象上,都给予对方正面的肯定,既能提升HR的好感度,又能让谈判气氛融洽,为后面的发挥留足空间。...

面试:第十六章:Java中级开发(16k)

HashMap底层实现原理,红黑树,B+树,B树的结构原理 Spring的AOP和IOC是什么?它们常见的使用场景有哪些?Spring事务,事务的属性,传播行为,数据库隔离级别 Spring和SpringMVC,MyBatis以及SpringBoot的注解分别有哪些?SpringMVC的工作原理,SpringBoot框架的优点,MyBatis框架的优点 SpringCould组件有哪些,他们...

面试阿里p7,被按在地上摩擦,鬼知道我经历了什么?

面试阿里p7被问到的问题(当时我只知道第一个):@Conditional是做什么的?@Conditional多个条件是什么逻辑关系?条件判断在什么时候执...

面试了一个 31 岁程序员,让我有所触动,30岁以上的程序员该何去何从?

最近面试了一个31岁8年经验的程序猿,让我有点感慨,大龄程序猿该何去何从。

【阿里P6面经】二本,curd两年,疯狂复习,拿下阿里offer

二本的读者,在老东家不断学习,最后逆袭

大三实习生,字节跳动面经分享,已拿Offer

说实话,自己的算法,我一个不会,太难了吧

程序员垃圾简历长什么样?

已经连续五年参加大厂校招、社招的技术面试工作,简历看的不下于万份 这篇文章会用实例告诉你,什么是差的程序员简历! 疫情快要结束了,各个公司也都开始春招了,作为即将红遍大江南北的新晋UP主,那当然要为小伙伴们做点事(手动狗头)。 就在公众号里公开征简历,义务帮大家看,并一一点评。《启舰:春招在即,义务帮大家看看简历吧》 一石激起千层浪,三天收到两百多封简历。 花光了两个星期的所有空闲时...

《经典算法案例》01-08:如何使用质数设计扫雷(Minesweeper)游戏

我们都玩过Windows操作系统中的经典游戏扫雷(Minesweeper),如果把质数当作一颗雷,那么,表格中红色的数字哪些是雷(质数)?您能找出多少个呢?文中用列表的方式罗列了10000以内的自然数、质数(素数),6的倍数等,方便大家观察质数的分布规律及特性,以便对算法求解有指导意义。另外,判断质数是初学算法,理解算法重要性的一个非常好的案例。

《Oracle Java SE编程自学与面试指南》最佳学习路线图(2020最新版)

正确选择比瞎努力更重要!

面试官:你连SSO都不懂,就别来面试了

大厂竟然要考我SSO,卧槽。

微软为一人收购一公司?破解索尼程序、写黑客小说,看他彪悍的程序人生!...

作者 | 伍杏玲出品 | CSDN(ID:CSDNnews)格子衬衫、常掉发、双肩包、修电脑、加班多……这些似乎成了大众给程序员的固定标签。近几年流行的“跨界风”开始刷新人们对程序员的...

终于,月薪过5万了!

来看几个问题想不想月薪超过5万?想不想进入公司架构组?想不想成为项目组的负责人?想不想成为spring的高手,超越99%的对手?那么本文内容是你必须要掌握的。本文主要详解bean的生命...

我说我懂多线程,面试官立马给我发了offer

不小心拿了几个offer,有点烦

自从喜欢上了B站这12个UP主,我越来越觉得自己是个废柴了!

不怕告诉你,我自从喜欢上了这12个UP主,哔哩哔哩成为了我手机上最耗电的软件,几乎每天都会看,可是吧,看的越多,我就越觉得自己是个废柴,唉,老天不公啊,不信你看看…… 间接性踌躇满志,持续性混吃等死,都是因为你们……但是,自己的学习力在慢慢变强,这是不容忽视的,推荐给你们! 都说B站是个宝,可是有人不会挖啊,没事,今天咱挖好的送你一箩筐,首先啊,我在B站上最喜欢看这个家伙的视频了,为啥 ,咱撇...

立即提问
相关内容推荐