C# TripleDESCryptoServiceProvider加密,以及MD5CryptoServiceProvider加密
using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Web;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
string lscryptoKey = "AAAAWECBBB";
string sIn = "AAAAWECBBB20181103121212";
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] keyArr = Encoding.UTF8.GetBytes(lscryptoKey);
for(int i= 0; i<keyArr.Length;i++){
Console.Write(keyArr[i]+" ");
}
Console.WriteLine("");
Console.WriteLine("------------");
des.Key = md5.ComputeHash(keyArr);
for(int i= 0; i<des.Key.Length;i++){
Console.Write(des.Key[i]+" ");
}
Console.WriteLine("------------");
des.Mode = CipherMode.ECB;
ICryptoTransform desEncrypt = des.CreateEncryptor();
byte[] buffer = Encoding.UTF8.GetBytes(sIn);
string str = Convert.ToBase64String(desEncrypt.TransformFinalBlock(buffer, 0, buffer.Length));
Console.WriteLine(str);
}
}
}
C#输出结果:
65 86 79 78 87 69 67 72 65 84
------------
167 4 22 251 138 30 226 254 131 50 142 158 160 94 215 238 ------------
AkJEUvWIpZ0+p+QgUQw7FjvvmHSS0dTpYN8498FULBg=
Java 代码如下:
import org.apache.commons.codec.binary.Base64;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
public class CryptoTools {
private static String ALGORITHM_3DES = "DESede";//加密方式
private static String ALGORITHM_MD5 = "MD5";//加密方式
private static String key = "AAAAWECBBB";//密钥
private static String str = "AAAAWECBBB20181103121212";//要加密的字符串
public static void main(String[] args) {
try {
byte[] keyBytes = key.getBytes("UTF-8");
byte[] srcBytes = str.getBytes("UTF-8");
// md5计算
MessageDigest digest = MessageDigest.getInstance(ALGORITHM_MD5);
digest.update(keyBytes);
byte[] digest1 = digest.digest();
SecretKey deskey = new SecretKeySpec(digest1, ALGORITHM_3DES);
Cipher c1 = Cipher.getInstance(ALGORITHM_3DES);
c1.init(Cipher.ENCRYPT_MODE, deskey);
String pwd = Base64.encodeBase64String(c1.doFinal(srcBytes));
System.out.println(pwd);
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=59948:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/lib/tools.jar:/Users/zl/IdeaWorkSpace/kingkong-parent/thirdparty/kingkong-saas-gateway-parent/gateway-edb/target/test-classes:/Users/zl/IdeaWorkSpace/kingkong-parent/thirdparty/kingkong-saas-gateway-parent/gateway-edb/target/classes:/Users/zl/IdeaWorkSpace/kingkong-parent/thirdparty/kingkong-saas-gateway-parent/gateway-sdk/target/classes:/Users/zl/.m2/repository/junit/junit/4.12/junit-4.12.jar:/Users/zl/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/zl/IdeaWorkSpace/kingkong-parent/kingkong-sdk/target/classes:/Users/zl/.m2/repository/com/dianping/cat/cat-client/1.3.9-kk-SNAPSHOT/cat-client-1.3.9-kk-20181012.031414-4.jar:/Users/zl/.m2/repository/org/unidal/framework/foundation-service/2.5.0/foundation-service-2.5.0.jar:/Users/zl/.m2/repository/org/codehaus/plexus/plexus-container-default/1.6/plexus-container-default-1.6.jar:/Users/zl/.m2/repository/org/codehaus/plexus/plexus-classworlds/2.5.1/plexus-classworlds-2.5.1.jar:/Users/zl/.m2/repository/org/apache/xbean/xbean-reflect/3.7/xbean-reflect-3.7.jar:/Users/zl/.m2/repository/org/codehaus/plexus/plexus-utils/3.0.24/plexus-utils-3.0.24.jar:/Users/zl/.m2/repository/org/aspectj/aspectjrt/1.5.4/aspectjrt-1.5.4.jar:/Users/zl/.m2/repository/io/netty/netty-all/4.0.24.Final/netty-all-4.0.24.Final.jar:/Users/zl/.m2/repository/org/apache/poi/poi-ooxml/3.17/poi-ooxml-3.17.jar:/Users/zl/.m2/repository/org/apache/poi/poi-ooxml-schemas/3.17/poi-ooxml-schemas-3.17.jar:/Users/zl/.m2/repository/org/apache/xmlbeans/xmlbeans/2.6.0/xmlbeans-2.6.0.jar:/Users/zl/.m2/repository/stax/stax-api/1.0.1/stax-api-1.0.1.jar:/Users/zl/.m2/repository/com/github/virtuald/curvesapi/1.04/curvesapi-1.04.jar:/Users/zl/.m2/repository/org/apache/poi/poi/3.17/poi-3.17.jar:/Users/zl/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar:/Users/zl/.m2/repository/org/apache/commons/commons-collections4/4.1/commons-collections4-4.1.jar:/Users/zl/.m2/repository/org/apache/poi/poi-scratchpad/3.17/poi-scratchpad-3.17.jar:/Users/zl/.m2/repository/commons-fileupload/commons-fileupload/1.3.1/commons-fileupload-1.3.1.jar:/Users/zl/.m2/repository/commons-io/commons-io/2.2/commons-io-2.2.jar:/Users/zl/.m2/repository/org/mybatis/mybatis/3.4.6/mybatis-3.4.6.jar:/Users/zl/.m2/repository/joda-time/joda-time/2.9.1/joda-time-2.9.1.jar:/Users/zl/.m2/repository/org/apache/commons/commons-lang3/3.3.1/commons-lang3-3.3.1.jar:/Users/zl/.m2/repository/org/projectlombok/lombok/1.16.8/lombok-1.16.8.jar:/Users/zl/.m2/repository/com/alibaba/fastjson/1.2.47/fastjson-1.2.47.jar:/Users/zl/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar:/Users/zl/.m2/repository/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar:/Users/zl/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.9.2/jackson-databind-2.9.2.jar:/Users/zl/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.9.0/jackson-annotations-2.9.0.jar:/Users/zl/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.9.2/jackson-core-2.9.2.jar:/Users/zl/.m2/repository/org/apache/httpcomponents/httpclient/4.3.6/httpclient-4.3.6.jar:/Users/zl/.m2/repository/org/apache/httpcomponents/httpcore/4.3.3/httpcore-4.3.3.jar:/Users/zl/.m2/repository/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.jar:/Users/zl/.m2/repository/redis/clients/jedis/2.9.0/jedis-2.9.0.jar:/Users/zl/.m2/repository/org/apache/commons/commons-pool2/2.4.2/commons-pool2-2.4.2.jar:/Users/zl/.m2/repository/org/springframework/spring-jdbc/5.0.5.RELEASE/spring-jdbc-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-beans/5.0.5.RELEASE/spring-beans-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-core/5.0.5.RELEASE/spring-core-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-jcl/5.0.5.RELEASE/spring-jcl-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-tx/5.0.5.RELEASE/spring-tx-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-orm/5.0.5.RELEASE/spring-orm-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/com/google/code/gson/gson/2.3.1/gson-2.3.1.jar:/Users/zl/.m2/repository/org/springframework/spring-oxm/5.0.5.RELEASE/spring-oxm-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-web/5.0.5.RELEASE/spring-web-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-webmvc/5.0.5.RELEASE/spring-webmvc-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-aop/5.0.5.RELEASE/spring-aop-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-context/5.0.5.RELEASE/spring-context-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-expression/5.0.5.RELEASE/spring-expression-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-jms/5.0.5.RELEASE/spring-jms-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-messaging/5.0.5.RELEASE/spring-messaging-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-test/5.0.5.RELEASE/spring-test-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-aspects/5.0.5.RELEASE/spring-aspects-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/aspectj/aspectjweaver/1.8.13/aspectjweaver-1.8.13.jar:/Users/zl/.m2/repository/org/springframework/spring-instrument/5.0.5.RELEASE/spring-instrument-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/spring-context-support/5.0.5.RELEASE/spring-context-support-5.0.5.RELEASE.jar:/Users/zl/.m2/repository/org/springframework/data/spring-data-commons/1.12.1.RELEASE/spring-data-commons-1.12.1.RELEASE.jar:/Users/zl/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.19/jcl-over-slf4j-1.7.19.jar:/Users/zl/.m2/repository/com/google/guava/guava/18.0/guava-18.0.jar:/Users/zl/.m2/repository/org/codehaus/groovy/groovy-all/2.4.12/groovy-all-2.4.12.jar:/Users/zl/.m2/repository/com/alibaba/druid/1.1.12/druid-1.1.12.jar CryptoTools
objc[5102]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/bin/java (0x10fce44c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10fd684e0). One of the two will be used. Which one is undefined.
java.security.InvalidKeyException: Wrong key size
at com.sun.crypto.provider.DESedeCrypt.init(DESedeCrypt.java:69)
at com.sun.crypto.provider.ElectronicCodeBook.init(ElectronicCodeBook.java:93)
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:582)
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:458)
at com.sun.crypto.provider.DESedeCipher.engineInit(DESedeCipher.java:166)
at javax.crypto.Cipher.implInit(Cipher.java:802)
at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
at javax.crypto.Cipher.init(Cipher.java:1249)
at javax.crypto.Cipher.init(Cipher.java:1186)
at CryptoTools.main(CryptoTools.java:27)
Process finished with exit code 0