1.MD5 algorithm
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
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("Clear text is:" + msg);
System.out.println("Ciphertext is:" + EncrypMD5.hexString(resultBytes));
}
//byte bytes are converted to 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();
/ / Use srcBytes update summary
Md5.update(srcBytes);
/ / Complete the hash calculation, get result
Byte[] resultBytes = md5.digest();
Return resultBytes;
}
}
MD5 IOS objective-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:
Clear text is: Nine
Ciphertext is: 203ECEBD64A8366E58ACF19BBB3148DD
2.SHA algorithm
Non-reversible
SHA1,SHA256,SHA384,SHA512 respectively corresponds to 160-bit, 256-bit import java.security.MessageDigest;
SHA algorithm Java code:
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 bytes are converted to hexadecimal string MD5Utils.hexString
Public byte[] eccrypt(String info, String shaType) throws NoSuchAlgorithmException {
MessageDigest sha = MessageDigest.getInstance(shaType);
Byte[] srcBytes = info.getBytes();
// Update the summary using srcBytes
Sha.update(srcBytes);
// Complete the hash calculation and get the 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("plain text:"+msg);
System.out.println("cryptography:"+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 algorithm iOS objective-c code:
//sha1 encryption
- (NSString *)sha1:(NSString *)str
{
Const char *cstr = [str UTF8String];
/ / Use the corresponding CC_SHA1, CC_SHA256, CC_SHA384, CC_SHA512 length is 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;
}
Clear text: Nine
Ciphertext: 600c7ca56a913a86a501d683846752113ed65824
Organize common encrypted IOS with Android encryption MD5-SHA1