/// <summary> /// 加密 /// </summary> /// <param name="text">明文</param> /// <param name="key">key</param> /// <returns>密文</returns> public static String encrypt(String text, String key) { //16的整数倍 int offset = 0; byte[] textBytes = Encoding.UTF8.GetBytes(text); if (textBytes.Length % 16 != 0) { offset = 16 - textBytes.Length % 16; } int length = textBytes.Length + offset; //key->byte byte[] keyBytes = Encoding.UTF8.GetBytes(key); byte[] MK = new byte[16]; for (int i = 0; i < keyBytes.Length; i++) { MK[i] = keyBytes[i]; } //输入 byte[] PlainText = new byte[length]; for (int i = 0; i < textBytes.Length; i++) { PlainText[i] = textBytes[i]; } //输出 byte[] CipherText = new byte[length]; SM4.SM4_Encrypt(MK, PlainText, CipherText); return(DataUtil.byteArrayToHexStr(CipherText)); }
public string Decrypt_ECB(string cipherText) { SM4_Context ctx = new SM4_Context(); ctx.isPadding = true; ctx.mode = SM4.SM4_DECRYPT; byte[] keyBytes; if (hexString) { keyBytes = Hex.Decode(secretKey); } else { keyBytes = Encoding.ASCII.GetBytes(secretKey); } SM4 sm4 = new SM4(); sm4.sm4_setkey_dec(ctx, keyBytes); byte[] decrypted = sm4.sm4_crypt_ecb(ctx, Hex.Decode(cipherText)); if (decrypted == null) { return(string.Empty); } else { return(Encoding.Default.GetString(decrypted)); } }
public string Encrypt_CBC(string plainData) { SM4_Context ctx = new SM4_Context(); ctx.isPadding = true; ctx.mode = SM4.SM4_ENCRYPT; byte[] keyBytes; byte[] ivBytes; if (hexString) { keyBytes = Hex.Decode(secretKey); ivBytes = Hex.Decode(iv); } else { keyBytes = Encoding.Default.GetBytes(secretKey); ivBytes = Encoding.Default.GetBytes(iv); } SM4 sm4 = new SM4(); sm4.sm4_setkey_enc(ctx, keyBytes); byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, Encoding.Default.GetBytes(plainData)); //return Hex.Encode(encrypted); //return encrypted; string cipherText = Encoding.Default.GetString(Hex.Encode(encrypted)); return(cipherText); }
public string Decrypt_CBC(string cipherData) { SM4_Context ctx = new SM4_Context(); ctx.isPadding = true; ctx.mode = SM4.SM4_DECRYPT; byte[] keyBytes; byte[] ivBytes; if (hexString) { keyBytes = Hex.Decode(secretKey); ivBytes = Hex.Decode(iv); } else { keyBytes = Encoding.Default.GetBytes(secretKey); ivBytes = Encoding.Default.GetBytes(iv); } SM4 sm4 = new SM4(); sm4.sm4_setkey_dec(ctx, keyBytes); byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, Hex.Decode(cipherData)); if (decrypted == null) { return(string.Empty); } else { return(Encoding.Default.GetString(decrypted)); } //return decrypted; }
/// <summary> /// 解密 /// </summary> /// <param name="text">密文</param> /// <param name="key">key</param> /// <returns>明文</returns> public static String decrypt(String text, String key) { //key->byte byte[] MK = new byte[16]; byte[] keyBytes = Encoding.UTF8.GetBytes(key); for (int i = 0; i < keyBytes.Length; i++) { MK[i] = keyBytes[i]; } int length = text.Length / 2; //输入 byte[] CipherText = new byte[length]; byte[] TCipherText = DataUtil.hexStringToByteArray(text); for (int i = 0; i < TCipherText.Length; i++) { CipherText[i] = TCipherText[i]; } //输出 byte[] PlainText = new byte[length]; SM4.SM4_Decrypt(MK, CipherText, PlainText); return(DataUtil.byteToString(PlainText)); }