Sort out common encryption iOS and Android encryption MD5-SHA1, androidmd5-sha1
1. MD5 Algorithm
Irreversible
A 128-bit or 64-Bit String. The length of a byte number is 16 or 8. Generally, if a byte is represented by a hexadecimal notation, one byte is converted to two 16bits, indicating a high position, respectively, therefore, the generated string is either 16-bit or 32-bit. The 16-Bit String is extracted from the middle part of the 32-Bit String.
The number of bits in the password is expressed as the number of bits. If it is converted to a byte array, it is divided by 8, but if the output is hexadecimal, it is divided by 4, because "1111 1111" = "FF ";
For example, the length of the 256-bit byte array or NSData is 256/8 = 32, and the hexadecimal output is 32*2 = 64.
MD5 Algorithm Java code:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class EncrypMD5 {
/ **
* TODO (description of this method)
* @param args
* @author 丶 贰 九 2015-4-29 5:33:52 PM
* @since v1.0
* /
public static void main (String [] args) throws NoSuchAlgorithmException {
String msg = "丶 贰 九";
EncrypMD5 md5 = new EncrypMD5 ();
byte [] resultBytes = md5.eccrypt (msg);
System.out.println ("The plaintext is:" + msg);
System.out.println ("The ciphertext is:" + EncrypMD5.hexString (resultBytes));
}
// byte byte converted to a hexadecimal 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 using srcBytes
md5.update (srcBytes);
// Complete the hash calculation and get result
byte [] resultBytes = md5.digest ();
return resultBytes;
}
}
MD5 iOSObjective-C code:
// md5 encryption
-(NSString *) md5: (NSString *) str
{
const char * cStrValue = [str UTF8String];
unsigned char theResult [CC_MD5_DIGEST_LENGTH];
CC_MD5 (cStrValue, (unsigned) strlen (cStrValue), theResult);
return [NSString stringWithFormat: @ "% 02x% 02x% 02x% 02x% 02x% 02x% 02x% 02x% 02x% 02x% 02x% 02x% 02x% 02x% 02x% 02x",
theResult [0], theResult [1], theResult [2], theResult [3],
theResult [4], theResult [5], theResult [6], theResult [7],
theResult [8], theResult [9], theResult [10], theResult [11],
theResult [12], theResult [13], theResult [14], theResult [15]];
}
The final result is:
Plaintext: Ma erjiu
Ciphertext: 203ecebd64a8440e58acf19bbb3148dd
2. SHA Algorithm
Irreversible
SHA1, SHA256, SHA384, and SHA512 correspond to 160 bits and 256 bits respectively import java. security. MessageDigest;
SHAJava code of the algorithm:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class EncrypSHA {
/ **
* TODO (description of this method)
*
* @param args
* @author 丶 贰 九 2015-4-29 5:12:17 PM
* @since v1.0
* /
// byte byte converted to a hexadecimal string MD5Utils.hexString
public byte [] eccrypt (String info, String shaType) throws NoSuchAlgorithmException {
MessageDigest sha = MessageDigest.getInstance (shaType);
byte [] srcBytes = info.getBytes ();
// Update summary using srcBytes
sha.update (srcBytes);
// Complete the hash calculation and get result
byte [] resultBytes = sha.digest ();
return resultBytes;
}
public byte [] eccryptSHA1 (String info) throws NoSuchAlgorithmException {
return eccrypt (info, "SHA1");
}
public byte [] eccryptSHA256 (String info) throws NoSuchAlgorithmException {
return eccrypt (info, "SHA-256");
}
public byte [] eccryptSHA384 (String info) throws NoSuchAlgorithmException {
return eccrypt (info, "SHA-384");
}
public byte [] eccryptSHA512 (String info) throws NoSuchAlgorithmException {
return eccrypt (info, "SHA-512");
}
public static void main (String [] args) throws NoSuchAlgorithmException {
String msg = "丶 贰 九";
EncrypSHA sha = new EncrypSHA ();
String sha1 = sha.hexString (sha.eccryptSHA1 (msg));
System.out.println ("clear text:" + msg);
System.out.println ("ciphertext:" + sha1);
}
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 ();
}
}
SHA AlgorithmIOSObjective-C code:
// sha1 encryption
-(NSString *) sha1: (NSString *) str
{
const char * cstr = [str UTF8String];
// Use the corresponding CC_SHA1, CC_SHA256, CC_SHA384, CC_SHA512 lengths are 20, 32, 48, 64
unsigned char digest [CC_SHA1_DIGEST_LENGTH];
// Use the corresponding CC_SHA256, CC_SHA384, CC_SHA512
CC_SHA1 (cstr, strlen (cstr), digest);
NSMutableString * result = [NSMutableString stringWithCapacity: CC_SHA1_DIGEST_LENGTH * 2];
for (int i = 0; i <CC_SHA1_DIGEST_LENGTH; i ++) {
[result appendFormat: @ "% 02x", digest [i]];
}
return result;
}
Plaintext: Ma erjiu
Ciphertext: 600c7ca56a913a86a501d683846752113ed65824