skavinvarnan / cross-platform-aes Goto Github PK
View Code? Open in Web Editor NEWSimple cross-platform encryption and decryption using AES
License: MIT License
Simple cross-platform encryption and decryption using AES
License: MIT License
Hi, I have this snippet of code working with a sha256 with 32 length key
let plainText :String = "this is my plain text"
let key :String = "your key"
let cryptLib = CryptLib()
let iv = "5/LRE3jKOTCav8VB"
let size = 32
let sha = cryptLib.sha256(key, length: size)
let cipherText = cryptLib.encrypt(plainText.data(using: String.Encoding.utf8), key: sha, iv: iv)
print(cipherText!.base64EncodedString())
let elem = cryptLib.decrypt(cipherText, key: sha, iv: iv)
let salida = String.init(data: elem!, encoding: String.Encoding.utf8)
print(salida!)
But when i change size = 24, it fails the decrypt, is there any way to make the decryption it work ?, i am in a proyect that needs a 24 length key.
always getting this issue when i decrypt the encrypted text string
'CC_MD5' is deprecated: first deprecated in iOS 13.0 - This function is cryptographically broken and should not be used in security contexts. Clients should migrate to SHA256 (or stronger).
HI
When I use Arabic or persian letters in encryption، I can not decode ios side
I'm sure the problem is from Arabic or Persian words
Is there a solution to this problem?
thanks so mutch
While decrypting cypher text in Android using 'decryptCipherTextWithRandomIV', which was received from Php side, shows only a part of actual data or some character is missing. After a few tries randomly get the exact plain text as desired!
The code is mentioned below :
String cipherText = "WelKD2g4ThgJPeRiVz5FCcMoZG9pGCGgSjDlkD2hq3ee1SRPovlia0o9dEBwpLjg";
String key = "1122334455667788";
String decryptedString = cryptLib.decryptCipherTextWithRandomIV(cipherText, key);
Which may produce the plain text with some letters missing sometimes!!!.. Please help out.
Its a powerful encryption seen to be working working perfectly fine on the Native Application of Android and iOS both ..
Moving to other, we need this for the Flutter too.
Could you please check the IOS Decryption and Encryption it not working Properly. when I'am trying to decrypt a string which was encrypted on Android platform.
(or could be please remove CrossPlatform from the title..its misleading) Sorry for my Langauge.
Hi
I tried your code in Xcode 8.3.2 Swift 3.x, It crashes in the line print decryptedString. Please help.
let plainText = "this is my plain text"
let key = "simplekey"
let iv = "1234123412341234"
let cryptoLib = CryptLib();
let encryptedString = cryptoLib.encryptPlainText(with: plainText, key: key, iv: iv)
print("encryptedString \(encryptedString! as String)")
let decryptedString = cryptoLib.decryptCipherText(with: encryptedString, key: key, iv: iv)
print("decryptedString \(decryptedString! as String)")
Hello, I trying to encrypt test in Android Java and decrypt in php using encryptPlainTextWithRandomIV in Android and decryptCipherTextWithRandomIV in php. But php can't decrypt the crypted text from android. I use the same Key both.
--------------Android code--------------
String AES_CRYPT_KEY = "Awpj5scJh9gdeKBdnpl5df5d8sqPlfNe"; AESEncryption aesEncryption= new AESEncryption(); String text="2019/01/18 20:04:55#QNd7OpVZ4w56Z8VZ8j5HcHk7UdxinD#U_4752256_nsia#1234567890"; Log.d("AES crypt", aesEncryption.encryptPlainTextWithRandomIV(AES_CRYPT_KEY, text));
Android encryption result: jiAfm/9C+whU/EOG4MW52LNnBNQflM5Ajx2qSdncaL++tF2mpLl/oiPTuBwFCceftUAj9UoaEbE9HqYXVgh/ug==
-------------PHP code -----------------
<?php
require '../libs/vendor/autoload.php';
$secretyKey = "Awpj5scJh9gdeKBdnpl5df5d8sqPlfNe";
$encryption = new \MrShan0\CryptoLib\CryptoLib();
$crypted="jiAfm/9C+whU/EOG4MW52LNnBNQflM5Ajx2qSdncaL++tF2mpLl/oiPTuBwFCceftUAj9UoaEbE9HqYXVgh/ug==";
$plainText = $encryption->decryptCipherTextWithRandomIV($crypted, $secretyKey);
echo 'Decrypted: ' . $plainText . PHP_EOL;
?>
// OUTPUT is Empty...........
Decrypted:
Help please
Hi
I am getting below error while I am trying to run app on iOS device.
It's working for android properly.
Undefined symbols for architecture x86_64:
"OBJC_CLASS$_CryptlibPlugin", referenced from:
objc-class-ref in GeneratedPluginRegistrant.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
When i'm use small message than complete encrypt decrypt but when i'm decrypt long message then getting bad-base64 exception.
Hey!!
I have used the header as bridging header in iOS, encryption and decryption works fine in ios as well as android. But when I try to encrypt in iOS and Decrypt in Android or vice versa I do not get result.. Any solution ?
Hello Sir,
I need code for encryption decryption in Node JS for Files (Images, Text Files) using your code.
Please help me out.
Regards,
Vikrant
Hi,
when I try to decrypt in node.js I have this error:
digital envelope routine: EVP_DecryptFinal_ex:wrong final block length.
I'm using the code example.
Regards,
Michele
This solution is working fine in iOS & Android, but we also need C#.Net, so can anyone provide same with this.
I am using encryptPlainText
/decryptCipherText
functions for encryption/decryption. Encryption is working fine but decryption gives wrong result.
"#"S7\u{1F}\u{0B}03vB!'+c\n]"Message":"","ErrorNumber":0,"UserInfo":{"UserDetails":[]}"
But I expect a correct JSON.
I tried tried tried many things and at last I figure out the issue. The issue is in your objective c file. In decryptCipherText
method we pass iv in argument but you generate random iv.
- (NSString *) decryptCipherText:(NSString *)ciperText key:(NSString *)key iv:(NSString *)iv { return [[NSString alloc] initWithData:[[CryptLib alloc] decrypt:[[NSData alloc] initWithBase64EncodedString:ciperText options:NSDataBase64DecodingIgnoreUnknownCharacters] key:[[CryptLib alloc] sha256:key length:32] iv:[[CryptLib alloc] generateRandomIV16]] encoding:NSUTF8StringEncoding]; }
Please replace the above method with following
- (NSString *) decryptCipherText:(NSString *)ciperText key:(NSString *)key iv:(NSString *)iv { return [[NSString alloc] initWithData:[[CryptLib alloc] decrypt:[[NSData alloc] initWithBase64EncodedString:ciperText options:NSDataBase64DecodingIgnoreUnknownCharacters] key:[[CryptLib alloc] sha256:key length:32] iv:iv] encoding:NSUTF8StringEncoding]; }
java.lang.RuntimeException: Stub!
This library contained a vulnerability issue when audited by NPM.
Here the details:
Moderate Memory Exposure
Package bl
Patched in >=0.9.5 <1.0.0 || >=1.0.1
Dependency of @skavinvarnan/cryptlib
Path @skavinvarnan/cryptlib > bl
More info https://nodesecurity.io/advisories/596
Please update the dependencies to fix this issue.
Hi,
I found this library very useful, can you please add support for PHP?
I know its not an issue, but I just wanted to know how can i store the key in android app, if someone tries to reverse engineer the app, they can get the key and easily decrypt the data.
Hi I have used the following method to encrypt
(NSData *)encrypt:(NSData *)plainText key:(NSString *)key iv:(NSString *)iv {
char keyPointer[kCCKeySizeAES256+2],// room for terminator (unused) ref: https://devforums.apple.com/message/876053#876053
ivPointer[kCCBlockSizeAES128+2];
BOOL patchNeeded;
bzero(keyPointer, sizeof(keyPointer)); // fill with zeroes for padding
patchNeeded= ([key length] > kCCKeySizeAES256+1);
if(patchNeeded)
{
NSLog(@"Key length is longer %lu", (unsigned long)[[self md5:key] length]);
key = [key substringToIndex:kCCKeySizeAES256]; // Ensure that the key isn't longer than what's needed (kCCKeySizeAES256)
}
//NSLog(@"md5 :%@", key);
[key getCString:keyPointer maxLength:sizeof(keyPointer) encoding:NSUTF8StringEncoding];
[iv getCString:ivPointer maxLength:sizeof(ivPointer) encoding:NSUTF8StringEncoding];
if (patchNeeded) {
keyPointer[0] = '\0'; // Previous iOS version than iOS7 set the first char to '\0' if the key was longer than kCCKeySizeAES256
}
NSUInteger dataLength = [plainText length];
//see https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man3/CCryptorCreateFromData.3cc.html
// For block ciphers, the output size will always be less than or equal to the input size plus the size of one block.
size_t buffSize = dataLength + kCCBlockSizeAES128;
void *buff = malloc(buffSize);
size_t numBytesEncrypted = 0;
//refer to http://www.opensource.apple.com/source/CommonCrypto/CommonCrypto-36064/CommonCrypto/CommonCryptor.h
//for details on this function
//Stateless, one-shot encrypt or decrypt operation.
CCCryptorStatus status = CCCrypt(kCCEncrypt, /* kCCEncrypt, etc. /
kCCAlgorithmAES128, / kCCAlgorithmAES128, etc. /
kCCOptionPKCS7Padding, / kCCOptionPKCS7Padding, etc. /
keyPointer, kCCKeySizeAES256, / key and its length /
ivPointer, / initialization vector - use random IV everytime /
[plainText bytes], [plainText length], / input /
buff, buffSize,/ data RETURNED here */
&numBytesEncrypted);
if (status == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buff length:numBytesEncrypted];
}
free(buff);
return nil;
}
And Decrypt it using the following method,
char keyPointer[kCCKeySizeAES256+2],
ivPointer[kCCBlockSizeAES128+2];
BOOL patchNeeded;
patchNeeded = ([key length] > kCCKeySizeAES256+1);
if(patchNeeded)
{
NSLog(@"Key length is longer %lu", (unsigned long)[[[StringEncryption alloc] md5:key] length]);
key = [key substringToIndex:kCCKeySizeAES256];
}
[key getCString:keyPointer maxLength:sizeof(keyPointer) encoding:NSUTF8StringEncoding];
[iv getCString:ivPointer maxLength:sizeof(ivPointer) encoding:NSUTF8StringEncoding];
if (patchNeeded) {
keyPointer[0] = '\0';
}
NSUInteger dataLength = [encryptedText length];
size_t buffSize = dataLength + kCCBlockSizeAES128;
void *buff = malloc(buffSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus status = CCCrypt(kCCDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
keyPointer, kCCKeySizeAES256,
ivPointer,
[encryptedText bytes], [encryptedText length], //input
buff, buffSize,//output
&numBytesEncrypted);
if (status == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buff length:numBytesEncrypted];
}
free(buff);
return nil;
But the decryption is not working. Please help me resolve this issue.
Trying to use in angular 8 and here is some issues.
Hello
I found the following problem when performing a security analysis on your library
Uninitialized Memory Exposure
how to do it in PHP ?
I've created a sample project for testing. I encrypt a string with Obj-c and attempt to decrypt it on using Android. I'm able to Decrypt the string on iOS but not Android. Any help would be greatly appreciated... Many thanks I receive a 'bad padding exception'
Obj-C Code:
NSString *plainText = @"this is my plain text";
NSString *keyy = @"simplekey";
NSString *iiv = @"1234123412341234";
NSString *encryptedString = [[CryptLib alloc] encryptPlainText:plainText key:keyy iv:iiv];
Dear
first i would like to than you about your great cryptlib
But i'm using it in android and swift
in android i managed to decrypt the string returned from the server successfully
but in IOS it keeps giving me errors or strange output data like :
ÄľIßþÑÔ×Q0•‹z•lz·'ù\u{0E} ¤*]9\u{15}ï!�EF\nëåt<NxýaI\u{1D}‘\u{03}� N3\u{07}½lô·÷=\u{1D}?ì\u{05}ÂòÅ·#Ñèâ\u{05}’=0Ñ<‰á\u{1D}\t\u{1E}!»>k/t\rÄ\u{07}K{-êg&ZÓ?\0o�¸\u{06}ˆ)ŸÝSX7ÜF\u{0F}]€[ïÎX-‰TÌœâ]¿ü.‚\nS\u{19}ÂN3!\u{15}\u{10}К¥
ÍQUö„›'Ï~N\u{0E}€‚F;»ª:sÿÒZ°óD:¿\u{19}\u{1A}†ŽvL
here is an example to try to decrypt
IV = 509+QAilldg3dj3p
key string "sha256" = 2c8bc04829771e56a064e192425f0f98
Encrypted text =
LbA/hK1DfsyqvwPKuyvKH90cFh6u6WfUBvFgv7XLtjNB4s++iJFSdLbAAIYLsFwh4HpZLzWMA5VCZTNwPUztvflIdTfyZ99y9B6Il+KHUKWNsgSlw6WgPw9ZGy+3zpZkaYmI8KkJj9hudeiCODxknXIJ26hkfjbAl9BRJg2GHyNKN1or+DR82Gxtfx6srbHS1jV/vItcZEOKRdaWzdRhGZ1HwYOD5cVg6xdt8Gts3dU=
Can you please help me because i'm frustrated and i failed to decode it using all samples in we
this string is returned from VB.net service
Regards
plain text is changed everytime
is it possible to detect bad key exception in ios
I'm facing a smiley issue on enc dec. I use it in node js with MySQL. Table schema was BLOB type.
Hi!
The problem arises when I try to decrypt a string from the server (using the same library) with a custom iv and key.
Android [Kotlin]
`val cryptLib = CryptLib()
val iv = "iv"
val key = "key"
val cipherText = "7ESDDdYDbnUh2hqGhAKGbEZKpwa14mEiT553b3etgjc="
val resultText = cryptLib.decryptCipherText(cipherText, key, iv)`
The app crash and return this:
javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
Caused by: javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
at com.android.org.conscrypt.OpenSSLCipher$EVP_CIPHER.doFinalInternal(OpenSSLCipher.java:602)
at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:365)
at javax.crypto.Cipher.doFinal(Cipher.java:2055)
Thanks for the help.
I am noticing that each encryption ends with a newline char. Is this expected behavior?
Example: "bV4PAwa7zwiLTTsbPH2zHQ==\n"
Specially when I decrypt the data if i dont send the encrypted data instead send the plain text, the node js app crashes, how do I prevent it from crashing and send error response? I'm thinking what if the hacker sends plain text and the app crashes and uses this to crash the app again and again, help me with this as I am new to node js
On iOS 13 devices, Handling decryption with the library's encrypted strings has consistently failed
Can we use it in React-Native ? If yes, can you please make an example.
Please add plugin for unity.
decryption work fine pervious version but not working on IOS 13.please help
I would want to know is Cross-PLatffrom-AES is using CryptoJS.
Do you have the library with pure javascript?
Do you have the implementation in a stored procedure for some database?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.