- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我使用 2 个库来加密密码,但使用相同的密码,我得到不同的值:。
这是我在android中使用的代码:
String dataEncrypted = new String();
try {
Cipher aesCipher = Cipher.getInstance("AES");
byte[] raw = hexToBytes(key);
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
aesCipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] byteDataToEncrypt = data.getBytes();
byte[] byteCipherText = aesCipher.doFinal(byteDataToEncrypt);
dataEncrypted = new BASE64Encoder().encode(byteCipherText);
return dataEncrypted;
} catch (Exception ex) {
//log.d(ex.getMessage());
}
我在 ios 中使用的这段代码:
const void *vplainText;
size_t plainTextBufferSize;
NSData *plainTextData = [data dataUsingEncoding: NSUTF8StringEncoding];
plainTextBufferSize = [plainTextData length];
vplainText = [plainTextData bytes];
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;
bufferPtrSize =(plainTextBufferSize + kCCBlockSizeAES128);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0001, bufferPtrSize);
const void *vkey = (const void *)[key UTF8String];
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
vkey,
kCCKeySizeAES128,
NULL,
vplainText,
plainTextBufferSize, /* input */
bufferPtr,
kCCKeySizeAES128, /* output */
&movedBytes);
NSString result;
if (cryptStatus== kCCSuccess)
{
result=[Base64 encode:(const void *)bufferPtr length:(NSUInteger)movedBytes];
free(bufferPtr);
}else{
result =@"False";
free(bufferPtr);
}
如何匹配ios和android的2个版本。请帮帮我!
最佳答案
Android 代码使用 AES256 加密字符串,然后对结果进行 base64 编码。 iOS 代码看起来只是加密,因此要使它们匹配,您必须对其进行 base64 编码。尝试使用以下内容:
- (NSString *)AES256EncryptWithKey:(NSString *)key
{
NSData *plainData = [self dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [plainData AES256EncryptWithKey:key];
//Use any decent base64 category support
NSString *encryptedString = [encryptedData base64Encoding];
return encryptedString;
}
. .对于 base64 编码,请在 NSString 上使用任何合适的类别,或者如果您还没有,请尝试 here .
加密方式(AES256是Android默认的AES):
//based on: AES Encrypt/Decrypt, Created by Jim Dovey and 'Jean'
//See http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html
- (NSData*)AES256EncryptWithKey:(NSString*)key
{
uint8_t iv[kCCBlockSizeAES128];
SecRandomCopyBytes(0, sizeof(iv), iv);
// 'key' should be 32 bytes for AES256, will be null-padded otherwise
char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
bzero(keyPtr, sizeof( keyPtr )); // fill with zeroes (for padding)
// fetch key data
[key getCString:keyPtr maxLength:sizeof( keyPtr ) encoding:NSUTF8StringEncoding];
NSUInteger dataLength = [self length];
//See the doc: For block ciphers, the output size will always be less than or
//equal to the input size plus the size of one block.
//That's why we need to add the size of one block here
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void* buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding, keyPtr, kCCKeySizeAES128,
iv /* initialization vector (optional) */, [self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesEncrypted);
if (cryptStatus == kCCSuccess)
{
NSMutableData* result = [NSMutableData dataWithBytes:iv length:kCCBlockSizeAES128];
[result appendBytes:buffer length:numBytesEncrypted];
free(buffer); //free the buffer
//the returned NSData takes ownership of the buffer and will free it on deallocation
return result;
}
free(buffer); //free the buffer
return nil;
}
我在 CocoaSecurity 上也取得了很好的成功框架,但是为了匹配Android的AES加密,我不得不退回到上面的方法。 (不再记得为什么)。
关于java - AESCipher(安卓)与 CCCrypt(ios),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23861877/
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 6 年前。 Improve t
我有一个使用 Java 加密的字符串 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.EN
我正在尝试使用 CCCrypt 来加密我的数据,但它出了点问题,加密后数据长度发生了变化,但在解密时没有发生,这是以下代码。 - (NSData *)AES256EncryptWithKeyGener
我正在尝试解码一些数据,但是当数据大于 520,000(字节?它是 [数据长度])时,我的应用程序将在 CCCrypt 上的调试器上崩溃行,或者如果与调试器分离,它只会卡住并且实际上不会正确崩溃(Sp
当我加密文件(doc、pdf 等)时,我使用: size_t bufferSize = dataLength + kCCBlockSizeAES128; CCCrypt( kCCEncrypt,
代码如下: - (NSData *) doCipher: (NSData *) plainData key: (NSData *) symmetricKey context: (CCOperati
当我加密一个字符串然后立即解密时,CCCryptorStatus 为 -4304 并且生成的解码字符串长度不正确。谁能看出我可能做错了什么? //Encrypt NSString *plainText
我正在获取 CCCryptorStatus as kCCSuccess。 这意味着加密已成功完成。 但是,我想获取加密数据,如何获取? 任何帮助将不胜感激。 谢谢, 法会拉托 最佳答案 好好看看 CC
在我的 iOS 应用程序中,我必须解密来自服务器的数据。我使用 CommonCrypto 框架,经过几次试验,我成功解密了 CCCrypt(kCCDecrypt, // operation
下面是我加密字符串的代码, NSString *token = @"us=foo;pw=bar;pwAlg=false;"; NSString *key = @"testtest"; const vo
我有一个使用 AES 加密的文件。 我使用以下 NSData 类别: #import @implementation NSData (AES) - (NSData *)AES256DecryptWi
//Stateless, one-shot encrypt operation CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt,
我尝试编写一个基于 CCCrypt 的加密 (AESCBC256) 函数,而 CCCrypt 正在生成一个随机值。 例如,当我将 1234567 传递给函数时,对于相同的 iv 和 key ,它将返回
对于 iPad 应用程序(ios5.0,arc),我需要加密传输到我正在使用的 Web 服务的数据。在阅读有关通过 Apple 提供的加密时,我遇到了两个 API 来执行此操作:CCCrypt 和 S
我使用 2 个库来加密密码,但使用相同的密码,我得到不同的值:。 这是我在android中使用的代码: String dataEncrypted = new String(); try { C
我在使用 SWIFT 3 进行加密和解密时遇到了一个奇怪的行为.我正在使用以下方法加密和解密字符串。加密时我生成随机盐并将其附加到加密数据的末尾,解密时我从数据中读取 IV 数据以解密并执行解密操作。
我正在尝试使用 DES 加密来加密密码(不要问为什么 DES,我知道它不太安全)。我是第一次在 iOS 中这样做,因此不得不依赖另一篇关于如何做的帖子。 当我运行加密时它返回空值,与解密一个已经加密的
我有一个使用 CCCrypt() 的解密/加密方法,它在 iOS5 上运行得非常好。现在我正在使用 iOS6 SDK 并且从未更改过我的代码,但似乎有什么地方坏了。我仍然可以用一个 key 加密一个字
我正在将一个有效的 Objective-C 类别 (NSData+AESCrypt.m) 移植到 Swift,我发现了一个使用指针的问题。 Swift 中加密部分的代码编译正确,但生成运行时 EXEC
我正在针对 iOS 的 Kotlin 多平台项目中研究 AES256 加密算法。 我检查了一些在纯 Kotlin 中实现这一点的现有库(例如 krypto ),但没有一个符合我对其余代码的要求(已经在
我是一名优秀的程序员,十分优秀!