ios--encryption, decryption

Source: Internet
Author: User

One, des Plus, decryption:

1. Principle: des encryption: Use the Cccrypt function to encrypt, and then use the Base64 code, pass the past

Des decryption: The received data according to Base64,decode, and then decrypted with the Cccrypt function, to get the original data

2. Need to include:

#import <CommonCrypto/CommonCryptor.h> (cccrypt file)

#import "GTMBase64.h" (base64 algorithm)

3. Specific implementation:

Const Byte iv[] = {1,2,3,4,5,6,7,8};

Encryption

+ (NSString *) Encryptusedes: (NSString *) plaintext key: (NSString *) key

{

NSString *ciphertext = nil;

NSData *textdata = [plaintext datausingencoding:nsutf8stringencoding];

Nsuinteger datalength = [TextData length];

unsigned char buffer[1024];

memset (buffer, 0, sizeof (char));

size_t numbytesencrypted = 0;

Cccryptorstatus cryptstatus = Cccrypt (Kccencrypt, Kccalgorithmdes,

Kccoptionpkcs7padding,

[Key utf8string], Kcckeysizedes,

iv

[TextData bytes], datalength,

Buffer, 1024,

&numbytesencrypted);

if (Cryptstatus = = kccsuccess) {

NSData *data = [NSData datawithbytes:buffer length: (Nsuinteger) numbytesencrypted];

ciphertext = [[NSString alloc] initwithdata:[gtmbase64 encodedata:data] encoding:nsutf8stringencoding];

}

return ciphertext;

}

Decrypt

+ (NSString *) Decryptusedes: (nsstring*) ciphertext key: (nsstring*) key

{

nsdata* CipherData = [GTMBase64 decodestring:ciphertext];

unsigned char buffer[1024];

memset (buffer, 0, sizeof (char));

size_t numbytesdecrypted = 0;

Byte iv[] = {1,2,3,4,5,6,7,8};

Cccryptorstatus cryptstatus = Cccrypt (Kccdecrypt,

Kccalgorithmdes,

Kccoptionpkcs7padding,

[Key utf8string],

Kcckeysizedes,

iv

[CipherData bytes],

[CipherData length],

Buffer

1024,

&numbytesdecrypted);

nsstring* plaintext = nil;

if (Cryptstatus = = kccsuccess) {

nsdata* data = [NSData datawithbytes:buffer length: (Nsuinteger) numbytesdecrypted];

plaintext = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];

}

return plaintext;

}

Attached: Java side of des plus decryption implementation method, the code is as follows:

  

public class DES {

private static byte[] IV = {1, 2, 3, 4, 5, 6, 7, 8};

public static string Encryptdes (String encryptstring, String encryptkey)
Throws Exception {
Ivparameterspec zeroiv = new Ivparameterspec (iv);
Secretkeyspec key = new Secretkeyspec (Encryptkey.getbytes (), "DES");
Cipher Cipher = cipher.getinstance ("des/cbc/pkcs5padding");
Cipher.init (Cipher.encrypt_mode, Key, ZEROIV);
byte[] EncryptedData = cipher.dofinal (Encryptstring.getbytes ());
Return Base64.encode (EncryptedData);
}
}

The code above uses a BASE64 encoding class whose code is implemented in the following way:

public class Base64 {
private static final char[] Legalchars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"
. ToCharArray ();

/**
* data[] to encode
*
* @param data
* @return
*/
public static String encode (byte[] data) {
int start = 0;
int len = data.length;
StringBuffer buf = new StringBuffer (data.length * 3/2);

int end = Len-3;
int i = start;
int n = 0;

while (I <= end) {
int d = ((((int) data[i]) & 0X0FF) << 16)
| (((((int) Data[i + 1]) & 0X0FF) << 8)
| (((int) Data[i + 2]) & 0X0FF);

Buf.append (legalchars[(d >>) & 63]);
Buf.append (legalchars[(d >>) & 63]);
Buf.append (legalchars[(d >> 6) & 63]);
Buf.append (Legalchars[d & 63]);

i + = 3;

if (n++ >= 14) {
n = 0;
Buf.append ("");
}
}

if (i = = start + len-2) {
int d = ((((int) data[i]) & 0X0FF) << 16)
| (((((int) Data[i + 1]) & 255) << 8);

Buf.append (legalchars[(d >>) & 63]);
Buf.append (legalchars[(d >>) & 63]);
Buf.append (legalchars[(d >> 6) & 63]);
Buf.append ("=");
} else if (i = = start + len-1) {
int d = (((int) data[i]) & 0X0FF) << 16;

Buf.append (legalchars[(d >>) & 63]);
Buf.append (legalchars[(d >>) & 63]);
Buf.append ("= =");
}

return buf.tostring ();
}

Second, MD5 encryption

Non-reversible

128-bit or 64-bit string, byte digit length is 16 and 8, the general expression is to use the 16 binary to represent, 1 byte is converted to 2 16bit, representing the high status, so the generated string is 16 bit or 32 bits, 16 bit is actually drawn from the middle part of the 32 bit.

The number of bits we call, which is how many bit, is converted into a byte array, is divided by 8, but if the output 16 is divided by 4, because "1111 1111" = "FF";

For example: the length of a 256-bit byte array or nsdata is 256/8=32 output 16 is 32*2=64 bit

Java code:

byte byte converted to 16 binary string md5utils.hexstring
public static String hexstring (byte[] bytes) {
StringBuffer hexvalue = new StringBuffer ();

for (int i = 0; i < bytes.length; i++) {
int val = ((int) bytes[i]) & 0xFF;
if (Val < 16)
Hexvalue.append ("0");
Hexvalue.append (Integer.tohexstring (Val));
}
return hexvalue.tostring ();
}

Public byte[] Eccrypt (String info) throws nosuchalgorithmexception{
MessageDigest MD5 = messagedigest.getinstance ("MD5");
byte[] srcbytes = Info.getbytes ();
Update Summary with Srcbytes
Md5.update (srcbytes);
Complete hash calculation to get result
byte[] resultbytes = Md5.digest ();
return resultbytes;
}

public static void Main (String args[]) throws nosuchalgorithmexception{
String msg = "Welcome to Jerryvon's Blog";
EncrypMD5 MD5 = new EncrypMD5 ();
byte[] resultbytes = Md5.eccrypt (msg);
System.out.println ("Ciphertext is:" + md5utils.hexstring (resultbytes));
System.out.println ("Clear text is:" + msg);
}

Objective-c Code: (usually 32-bit here)

16-bit MD5 encryption method
-(NSString *) getmd5_16bit_string: (NSString *) srcstring{
Extracts the middle 16 bits of a 32-bit MD5 hash
NSString *md5_32bit_string=[self getmd5_32bit_string:srcstring];
NSString *result = [[md5_32bit_string substringtoindex:24] substringfromindex:8];//is 9~25 bit

return result;
}


32-bit MD5 encryption method
-(NSString *) getmd5_32bit_string: (NSString *) srcstring{
const char *CSTR = [srcstring utf8string];
unsigned char digest[cc_md5_digest_length];
CC_MD5 (CStr, strlen (CSTR), Digest);
nsmutablestring *result = [nsmutablestring stringwithcapacity:cc_md5_digest_length * 2];
for (int i = 0; i < cc_md5_digest_length; i++)
[Result appendformat:@ "%02x", Digest[i]];

return result;
}

The final result is:

Clear text: Welcome to Jerryvon's Blog

Ciphertext (32-bit): 3635DC132BA0E49F17534E749B8A99D1
Ciphertext (16-bit): 2ba0e49f17534e74

As long as you compare the last hex value, you can see if the original string is equal.

MD5 can also be encrypted two times, that is, the original string 2 times MD5 Encryption

ios--encryption, decryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.