-
Notifications
You must be signed in to change notification settings - Fork 1
/
Steganography.cs
103 lines (70 loc) · 2.22 KB
/
Steganography.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using UnityEngine;
using System.Collections;
public class Steganongraphy : MonoBehaviour {
public static Texture2D Encode(Texture2D image, string message){
//Create a new texture to copy encoded pixels to
Texture2D newTexture = new Texture2D(image.width,image.height);
//This variable holds the total amount of bits in the message.
int totalBits = 0;
System.Text.Encoding encoding = System.Text.Encoding.ASCII;
byte[] strBytes = encoding.GetBytes (message);
BitArray strBits = new BitArray(strBytes);
totalBits = strBits.Length;
BitArray strLength = new BitArray(System.BitConverter.GetBytes(totalBits));
//Create a new BitArray to hold the length of the message + the message itself.
BitArray finalBits = new BitArray(strLength.Length + totalBits);
int index = 0;
for (int lb = 0;lb<strLength.Length;lb++){
finalBits[lb] = strLength[lb];
index++;
}
for (int sb = 0;sb<strBits.Length;sb++){
finalBits[index] = strBits[sb];
index++;
}
//Get the pixels for the image...
Color[] imagePixels = image.GetPixels();
for (int i=0;i<finalBits.Length;i++){
if (finalBits[i] == true){
imagePixels[i].a = 1.0f;
}
else {
imagePixels[i].a = 0.99f;
}
}
newTexture.SetPixels(imagePixels);
newTexture.Apply();
return newTexture;
}
public static string Decode (Texture2D image){
//Get the pixels for the image...
Color[] imagePixels = image.GetPixels();
//Go Through the First 32 Pixels and create a 4 byte array.
//This array should give us the message's length.
BitArray newBits = new BitArray(32);
for (int i=0;i<32;i++){
if(imagePixels[i].a == 1){
newBits[i] = true;
}
else {
newBits[i] = false;
}
}
int total = System.BitConverter.ToInt32(ToByteArray(newBits), 0);
BitArray messageBits = new BitArray(total);
for (int j=32;j<total + 32;j++){
if(imagePixels[j].a == 1){
messageBits[j-32] = true;
}
else {
messageBits[j-32] = false;
}
}
return System.Text.Encoding.ASCII.GetString(ToByteArray(messageBits));
}
public static byte[] ToByteArray(BitArray bits){
var bytes = new byte[bits.Length / 8];
bits.CopyTo(bytes,0);
return bytes;
}
}