c++ - 在 XCode 4.6.2 上使用 cryptopp 静态库在 iOS 应用程序中抛出异常

标签 c++ ios objective-c xcode crypto++

这是我所拥有的一些细节。

  • 单独编译的 crypto++ build 并有一个静态库 (libcryptopp.a)。
  • 创建了示例单 View 应用程序并链接到上述库,创建了包含 crypto++ header 的新组。这些 header 不会复制到应用程序的目标文件夹中。
  • 在应用程序中创建了一个新的 .mm 文件,我正在从中执行一些我现在想发送到控制台的示例代码。请注意,此示例代码与测试文件 SymmetricCipher.cpp 中提供的代码几乎没有任何修改。


  • 项目build设置下的设置:

    Apple LLVM 编译器 4.2 设置

  • C 语言方言 - GNU99
  • C++ 语言方言 - GNU++11
  • C++ 标准库 - libstdc++


  • 对现有项目进行完全相同的更改,并在现有文件中插入示例代码以测试输出。这没有任何问题。

  • 独立应用中的代码抛出异常“EXC_BAD_ACCESS (code=2, address=0x20)”

      #import "TestView.h"
    
      //Include C++ headers
      #ifdef __cplusplus
      #include "aes.h"
    
      // Includes all required Crypto++
      // Block Cipher Headers
      #include "SymmetricCipher.h"
    
      #include <iostream>
      #include <iomanip>
    
      // Crypto++ Includes
      #include "modes.h" // xxx_Mode< >
      #include "filters.h" // StringSource and
      // StreamTransformation
    
      #include "sha.h"
      #include "base64.h"
    
      #endif
    
    
      @implementation TestView
    
      - (id)initWithFrame:(CGRect)frame
      {
          self = [super initWithFrame:frame];
          if (self) {
              // Initialization code
             }
          return self;
      }
    
      - (void)testBlock
      {
    
      //Test code
      byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
    
      ::memset( key, 0x01, CryptoPP::AES::DEFAULT_KEYLENGTH );
      ::memset( iv, 0x01, CryptoPP::AES::BLOCKSIZE );
    
      // Message M
      std::string PlainText = "Yoda said,Do or Do Not. There is no try.";
    
      // Cipher Text Sink
      std::string CipherText;
    
      // Encryptor
      CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption
      Encryptor( key, sizeof(key), iv );
    
      // Encryption
      CryptoPP::StringSource( PlainText, true,
                       new CryptoPP::StreamTransformationFilter( Encryptor, new             CryptoPP::StringSink(CipherText )) // StreamTransformationFilter
                       ); // StringSource
    
      // example of hashing followed by base64 encoding, using filters
      std::string digest;
    
      CryptoPP::SHA256 hash;  // don't use MD5 anymore. It is considered insecure
    
      CryptoPP::StringSource foo(PlainText, true,
                           new CryptoPP::HashFilter(hash, new CryptoPP::Base64Encoder (new CryptoPP::StringSink(digest))));
    
      NSLog(@"SHA256 Hash %s", digest.c_str());
    
      }
    
      @end
    

最佳答案

Crypto++ 代码很好。你的问题出在别处。

与其尝试交叉编译 Crypto++,不如试试 cryptopp-5.6.2-ios on GitHub .它为 6.1 SDK (armv7, armv7s, i386) 预建了一个 fat 库;以及用于 7.0 SDK 的预构建胖库(armv7armv7sarm64i386)。

Crypto++/iOS test code

关于c++ - 在 XCode 4.6.2 上使用 cryptopp 静态库在 iOS 应用程序中抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16159363/

相关文章:

c++ - 当程序以 root 身份运行时,在运行时切换用户

c++ - 无法将 ‘const char*’ 转换为‘std::istream*

c++ - 如何将析构函数分配给指针?

ios - CKFetchRecordChangesOperation-更多即将到来

iphone - UIView:不透明 vs. alpha vs. 不透明

ios - 使用 AttributedString 立即格式化 UITextView

ios - 获取 iPhone 所在的国家(没有 CLLocationManager)

c++ - 在 C++ 中正确设置局部环境变量

iphone - Objective-C内存管理问题,NSMutableArray

iphone - 将数据复制到 iPhone 上的应用程序数据文件夹