简单加解密
2015-07-13 19:03
323 查看
import android.util.Base64; public class Simplede { public static byte[] doencrypt(byte[] data, byte[] keyData) { int keyIndex = 0; for (int x = 0; x < data.length; x++) { data[x] = (byte) (data[x] ^ keyData[keyIndex]); if (++keyIndex == keyData.length) { keyIndex = 0; } } return data; } public static String encrypt(String strOld, String strKey) { byte[] data=doencrypt(strOld.getBytes(), strKey.getBytes()); return Base64.encodeToString(data, Base64.DEFAULT); } public static String decrypt(String strOld, String strKey) { byte[] mingwen=Base64.decode(strOld.getBytes(), Base64.DEFAULT); byte[] data=doencrypt(mingwen, strKey.getBytes()); return new String(data); } public static void main(String[] args) { String key="10241314"; String seed="test你好来自中国"; for(int i=0;i<30;i++) { seed=seed+i; System.out.println(seed); // 加密: String data=encrypt(seed,key); System.out.println(data); // 解密: System.out.println(decrypt(data,key)); } } }
Java for cpp
public class SimpledeForCPP { /* #include <stdlib.h> #include <conio.h> #include <stdio.h> #include <string.h> unsigned char* myobfuscate(unsigned char *s) { unsigned char key, mod, len; int i; unsigned char* d; key = s[0]; mod = 0x66; len = strlen((const char*)s); d = (unsigned char *)malloc(len + 1); // zero terminate the string memset(d, 0x00, len + 1); for (i = 0; i < len; i++) { d[i] = s[i] ^ mod; } d[len] = 0; return d; } //如果想用转义字符用十六进制表示应使用'\x12'这样的形式,注意应该是小写x,不是大写的X static unsigned char mystr[]="\xc\x7\x10\x7\x7\x8\x2\x14\x9\xf\x2"; static unsigned char mychinese[]="\xb0\xb6\xa8\xa2\x12\x3\x15\x12\xc\x7\x10\x7\x7\x8\x2\x14\x9\xf\x2\xa8\xb4\xa6\xd2\xb1\xb2\x2c\x7\x10\x7\x46\x5\x16\x16\xda\xb5\xa5\xba"; int main(int argc, char *argv[]) { printf("%s\n",deobfuscate(proc_kallsyms)); printf("%s\n",myobfuscate(mystr)); printf("%s\n",myobfuscate(mychinese)); getch(); return 0; } */ public static void main(String[] args) throws Exception { StringBuffer sb=new StringBuffer(); byte[] data="中文testjavaandroid我来自Java cpp加密".getBytes("GBK"); byte[] buffer=new byte[data.length]; for (int x = 0; x < data.length; x++) { buffer[x] = (byte) (data[x] ^ 0x66); int v = buffer[x] & 0xFF; sb.append("\\x"+Integer.toHexString(v)); } //System.out.println(new String(buffer)); System.out.println(sb.toString()); } }
cpp string
char* simpleed(char *str,const char *pwd) { int j0=0; int j=-1; int len=0; //获取密码长度 while(pwd[++j0]); len=strlen(str); char* code=new char[len+1]; for (unsigned int i=0;i<strlen(str);i++) { if(j>=j0) { j=0; }else{ j++; } printf("index=%d\n",j); code[i]=str[i]^pwd[j];/*异或后写入fp2文件*/ } code[len]='\0'; return code; } int main( int argc, char* argv[] ) { char* str="abcdefghijklmn密文test"; const char *pwd="1024key"; printf("%s\n",str); str=simpleed(str,pwd); printf("%s\n",str); OutputDebugStringA(str); str=simpleed(str,pwd); printf("%s\n",str); OutputDebugStringA(str); getch(); return 0; }
char* simpleed(char *str,const char *pwd) { int j0=0; int j=-1; int len=0; //获取密码长度 while(pwd[++j0]); len=strlen(str); char* code=new char[len+1]; for (unsigned int i=0;i<len;i++) { if(j>=j0) { j=0; }else{ j++; } //printf("index=%d\n",j); code[i]=str[i]^0x77;/*异或后写入fp2文件*/ } code[len]='\0'; return code; }
相关文章推荐
- CPaintDC 、CWindowDC、 CClientDC、 CDC的区别与联系
- 虚函数、虚指针和虚表
- 【leetcode-90】SubsetII(java 语言)
- SQL将汉字转化为拼音(首字母、全拼)
- 报表的制作
- 设计模式
- Volley源码分析
- 饭统崩溃:不创新、权力下放是不是作死
- 在linux中添加ftp用户,以及修改FTP默认端口号
- [leetcode] Remove Duplicates from Sorted List II
- HDU 4463 Outlets 最小生成树Kr~
- Zoj 2562 More Divisors (反素数)
- 6:ZigZag Conversion
- ubuntu 修改主机名
- commons-fileupload上传文件(1)
- Swiper使用方法(向前和向后按钮在swiper-container外面)
- [LeetCode] Number of 1 Bits
- Linux监控工具vmstat命令详解
- scikit-learn:CountVectorizer提取tf都做了什么
- 自定义文本横向渐变消失的控件